Re: Using Enums

Attila Szegedi <[email protected]>
Newsgroups gmane.comp.web.freemarker.user
Message-ID <[email protected]>
The problem is that FreeMarker uses JavaBeans naming convention for  
property-style accessors, and java.lang.Enum does not adhere to it.

In more detail:

Your Calculation class probably has a "getName()" method. JavaBeans  
standard says that referring to a property named "name" should invoke  
the "getName()" method. So far, so good.

However, java.lang.Enum defines a method named "name()", not  
"getName()". Thus, c.name is now a reference to a method, and not  
evaluation of the "name" property via "getName()" method. If you were  
to use enums, you'd have to use ${c.getName()}. Actually, you might  
try using ${c} on its own -- that should do an automatic toString(),  
which for enums happens to be equal to their name.

Attila.

On 2008.01.03., at 12:18, Luke Shannon wrote:

> Freemarker has been working fine so far writing out pojos. My pojo is
> called Calculation and a list of them is passed into the template:
>
> List<Calculation> calculations = new ArrayList<Calculation>();
>        calculations.add(instance1);
>        calculations.add(instance2);
>
>
> In the template I use <#list calculations as c> to be able to do
> something like this: c.name.
>
> No issues.
>
> I have attempted to go one step further and make the Calculation an
> enum. The reason for this is there is a set amount of possible
> Calculation instances (what I mean by this is a set amount of possible
> states for Calculation objects).
>
> Now I am doing something like this:
>
> List<Calculation> calculations = new ArrayList<Calculation>();
>        calculations.add(Calculation.NUMBER_ASSIGNMENTS);
>        calculations.add(Calculation.NUMBER_ASSIGNMENTS_EST);
>
> When I try and run the template I get the following:
>
> freemarker.core.NonStringException: Error on line 564, column 93 in
> claimsTemplate.ftl
> Expecting a string, date or number here, Expression c.name is instead
> a freemarker.ext.beans.SimpleMethodModel
>    at freemarker.core.Expression.getStringValue(Expression.java:126)
>    at freemarker.core.Expression.getStringValue(Expression.java:93)
>    at freemarker.core.DollarVariable.accept(DollarVariable.java:76)
>    at freemarker.core.Environment.visit(Environment.java:208)
>    at freemarker.core.MixedContent.accept(MixedContent.java:92)
>    at freemarker.core.Environment.visit(Environment.java:208)
>    at freemarker.core.IteratorBlock 
> $Context.runLoop(IteratorBlock.java:179)
>    at freemarker.core.Environment.visit(Environment.java:415)
>    at freemarker.core.IteratorBlock.accept(IteratorBlock.java:102)
>    at freemarker.core.Environment.visit(Environment.java:208)
>    at freemarker.core.MixedContent.accept(MixedContent.java:92)
>    at freemarker.core.Environment.visit(Environment.java:208)
>    at freemarker.core.Environment.process(Environment.java:188)
>    at freemarker.template.Template.process(Template.java:232)
>    at  
> com 
> .jaspersoft 
> .gates.claims.generator.ClaimsGenerator.main(ClaimsGenerator.java:167)
>
>
> When I do a search on FreeMarker and Enums I find myself reading about
> the TemplateHashModel. But I am not sure this is what I want.
>
> I basically want to put a list of enums and have FreeMarker access
> their members like the Pojos. Is this possible? I am pretty new to
> Enums, am I mis-using them in this case? Does anyone know a better way
> to do this?
>
> Thanks!




-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.