[英]Freemarker boolean attribute retrieval
I have a Customer object which has a boolean attribute letterActivated. 我有一个具有布尔属性letterActivated的Customer对象。 The getter method goes as below as per the standard java practices.
按照标准Java惯例,getter方法如下。
public Boolean isLetterActivated() {
return letterActivated;
}
But seems like Freemaker doesn't pickup customer.letterActivated
expression and i guess it might be cause of i don't have getLetterActivated method. 但是似乎Freemaker不会选择
customer.letterActivated
表达式,我想这可能是因为我没有getLetterActivated方法的原因。 (Note that other expressions of customer object works fine which all have get*** methods). (请注意,所有带有get ***方法的customer对象其他表达式都可以正常工作)。
How should i reference this boolean variable from FTL? 我应该如何从FTL引用此布尔变量? Note that i'm unable to change the getter method name (isMobileBillingAccount) as it is in existing code and referred in many places.
请注意,我无法更改getter方法名称(isMobileBillingAccount),因为它在现有代码中并在许多地方都已引用。 Also it is the correct way for a boolean getter i guess.
我猜这也是布尔型吸气剂的正确方法。
Either use public boolean isLetterActivated()
or public Boolean getLetterActivated()
. 使用
public boolean isLetterActivated()
或public Boolean getLetterActivated()
。 The Boolean
+ is
combination doesn't define a JavaBean property according the JavaBeans specification. Boolean
+ is
组合,未根据JavaBeans规范定义JavaBean属性。 You don't have to change the name, you can just add the correct getter. 您不必更改名称,只需添加正确的吸气剂即可。 Or as a last chance, you can use
customer.isLetterActivated()
in FTL. 或者最后一次机会,您可以在FTL中使用
customer.isLetterActivated()
。
a boolean getter can be name getXXX
also.. and as you said there are other references refering with the existing method isLetterActivated
. 布尔型getter也可以命名为
getXXX
..正如您所说的,还有其他引用引用了现有方法isLetterActivated
。 i would suggest doing this since the other references are out of scope to you. 我建议您这样做,因为其他参考资料超出了您的范围。
public boolean getLetterActivated() {
return this.isLetterActivated();
}
This way you dont mess up the existing code and the new code can use the new method... but i think this is a bad design. 这样,您就不会弄乱现有的代码,新的代码可以使用新的方法...但是我认为这是一个糟糕的设计。 but given the circumstance i would advice you to go for this.
但考虑到这种情况,我建议您这样做。
${customer.letterActivated?c}
Uses isLetterActivated()
and 使用
isLetterActivated()
和
Display "true" if the value is true
. 如果值为
true
则显示“ true”。
Following code also function without any problem for me ( tested in freemarker 2.3.20 ) 以下代码对我来说也没有任何问题( 在freemarker 2.3.20中进行了测试 )
<#if (customer.letterActivated)!false>
Great letter is activated! ${customer.letterActivated?c} man
<#/if>
()!false
part is only there to handle null
values. ()!false
部分仅用于处理null
值。
Even more information from freemarker compiler: 来自freemarker编译器的更多信息:
Freemarker Compiler output
Freemarker编译器输出
Can't convert boolean to string automatically, because the "boolean_format" setting was "true,false", which is the legacy default computer-language format, and hence isn't accepted.
无法将布尔值自动转换为字符串,因为“布尔值格式”设置为“真,假”,这是传统的默认计算机语言格式,因此不被接受。
Tip: If you just want "true"/"false" result as you are generting computer-language output, use "?c", like ${myBool?c}.
提示:如果在生成计算机语言输出时只希望结果为“ true” /“ false”,请使用$?myBool这样的$?myBool?c}。
Tip: You can write myBool?string('yes', 'no') and like to specify boolean formatting in place.
提示:您可以编写myBool?string('yes','no')并喜欢在适当位置指定布尔格式。
Tip: If you need the same two values on most places, the programmers should set the "boolean_format" setting to something like "yes,no".
提示:如果大多数地方都需要相同的两个值,则程序员应将“ boolean_format”设置设置为“ yes,no”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.