Home > Back-end >  How properly check boolean value in freemarker
How properly check boolean value in freemarker

Time:01-28

So, I'am trying to make simple condition:

<#if documentData.isOtherInsurance>
<p>&#10004 Да</p>
<#else> <p>&#10008;Нет </p></#if>

and I'm getting mistake like:

For "#if" condition: Expected a boolean, but this has evaluated to a method sequence (wrapper: f.e.b.SimpleMethodModel):
==> documentData.isOtherInsurance!false  [in template "InsuranceNotification" at line 1, column 1445]

----
Tip: Maybe using obj.something instead of obj.isSomething will yield the desired value.
----

----
FTL stack trace ("~" means nesting-related):
    - Failed at: #if documentData.isOtherInsurance!false  [in template "InsuranceNotification" at line 1, column 1440]
----

So I tried almost everything

1)

<#if ${documentData.isOtherInsurance>} <p>&#10004 Да</p> ...
Syntax error in template "InsuranceNotification" in line 1, column 1445:
You can't use ${...} (an interpolation) here as you are already in FreeMarker-expression-mode. Thus, instead of ${myExpression}, just write myExpression. (${...} is only used where otherwise static text is expected, i.e., outside FreeMarker tags and interpolations, or inside string literals.)

CodePudding user response:

Try this:

<#if documentData.isOtherInsurance> 
    <@displayRow label="isOtherInsurance" value="&#10004 Да" />
<#else>
    <@displayRow label="isOtherInsurance" value="&#10008;Нет" />
</#if>

CodePudding user response:

isOtherInsurance is a method, as the error messages says, not a boolean. Try documentData.otherInsurance (no "is"), which is the Java Bean property that the boolean isOtherInsurance() Java method automatically defines.

Note that sometimes developers incorrectly declare such methods as Boolean isOtherInsurance() (with capital Boolean). Then they should change that to Boolean getOtherInsurance(), or to boolean isOtherInsurance(). But if they won't, you can still use documentData.isOtherInsurance() (note that () which will call the method, so then then the return value of the method will be used, instead of the method itself).

  • Related