Re: Enum as a model
Daniel Dekany <[email protected]>
| Newsgroups | gmane.comp.web.freemarker.user |
|---|---|
| Message-ID | <[email protected]> |
Tuesday, May 31, 2011, 12:46:11 PM, magic creative wrote: > The code was to help me clarify the problem. The real code where I > am using this has a value set for the enum. However what you are > saying is that Freemaker can directly work with enums as model values. Yes. It doesn't treat them specially (it isn't enum-aware), but it handles them just like it handles any other POJO-s. Maybe your ObjectWrapper is something special? It should work with any BeansWrapper descendants, like with the DefaultObjectWrapper. > On Tue, May 31, 2011 at 11:02 AM, Daniel Dekany <[email protected]> wrote: > Tuesday, May 31, 2011, 9:18:51 AM, magic creative wrote: > >> <code> >> //Enum: >> >> public enum X { >> FIRST, SECOND >> } >> >> //Class: >> >> public class Y { >> >> private X localEnum; >> >> public X getLocalEnum(){ >> return this.localEnum; >> } >> >> void setLocalEnum(X localEnum){ >> this.localEnum = localEnum; >> } >> >> } >> >> //Insance to serve as model: >> >> Y yInstance = new Y(); >> >> //Freemaker model: >> >> Map<String, Object> model = new HashMap<String, Object>(); >> model.put("yInst", yInstance); >> >> //Template: >> >> The value of X in Y: ${yInst.localEnum} >> >> </code> >> >> So what I have is a model which contains an enum and I want simply >> to show the assigned value. > > The problem above is that you haven't set the localEnum property, so > it's null. So when you write ${yInst.localEnum}, FreeMarker gets null, > so it assumes that the property wasn't set (see FAQ). If you add > > yInstance.setLocalEnum(X.FIRST); > > then it will print > > The value of X in Y: FIRST > > -- > Best regards, > Daniel Dekany > > > >> I have read what you have referenced but >> I do not see how to apply it to my quite simple case. On the other >> hand I read a post where it is written that the problem with enums >> is comming from the fact that instead of getName(), they are >> reference with name() and thus Freemaker does not recognize that as >> property but as function the return value of which it can not >> display. I have a hard time however to believe that an enum can not >> be used as a model for freemaker, there must be something I have missed. >> >> >> On Mon, May 30, 2011 at 7:17 PM, Daniel Dekany <[email protected]> wrote: >> Monday, May 30, 2011, 4:19:06 PM, magic creative wrote: >> >>> Hi guys, >>> >>> I can not seem to find an example how to solve my problem so let me ask directly. >>> >>> If there is an Enum X which is part of an object Y >> >> What does that mean in Java language? >> >> BTW, just in case you haven't red this, maybe this helps: >> http://freemarker.org/docs/pgui_misc_beanwrapper.html#jdk_15_enums >> >>> I put as model and I reference X in my template I am getting: >>> >>> >>> >>> Expression Y.X is undefined >>> >>> >>> The template has simply ${Y.X}. If X was a String there would be no >>> problem and it would be displayed. As an Enum however it does not >>> work. What should I do to allow enums to be parsed? >>> >>> Kind Regards >> -- >> Best regards, >> Daniel Dekany >> > -- Best regards, Daniel Dekany ------------------------------------------------------------------------------ Simplify data backup and recovery for your virtual environment with vRanger. Installation's a snap, and flexible recovery options mean your data is safe, secure and there when you need it. Data protection magic? Nope - It's vRanger. Get your free trial download today. http://p.sf.net/sfu/quest-sfdev2dev