Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Freemarker: an enum in an if statement

In my if statement, I want to compare a variable, which is a JDK 1.5 enum, to an enum literal. For example:

<#if type == ProblemStatisticType.BEST_SOLUTION_CHANGED>
  ...
</#if>

But I get this exception:

freemarker.core.InvalidReferenceException: Expression ProblemStatisticType is undefined on line 430, column 87 in index.html.ftl.
at freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:125)
at freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:135)

How can I do that?

like image 821
Geoffrey De Smet Avatar asked Sep 14 '12 09:09

Geoffrey De Smet


2 Answers

Unfortunately, the FreeMarker language doesn't have the concept of classes... but you can do this:

<#if type.name() == "BEST_SOLUTION_CHANGED">
  ...
</#if>

Or if you trust the toString() for the enum type, the .name() part can be omitted.

like image 111
ddekany Avatar answered Sep 29 '22 16:09

ddekany


If you want to compare enums you should specify a constant enum value in double quotes like:

<#if type == "BEST_SOLUTION_CHANGED">
  ...
</#if>
like image 2
michael Avatar answered Sep 29 '22 18:09

michael