简体   繁体   English

如何检查该数组在Apache FreeMarker中的大小> 1?

[英]How to check, that array has size > 1 in Apache FreeMarker?

I'm trying to implement template with object, that can be plural. 我正在尝试使用对象实现模板,该对象可以是复数。

For example: "There is/are n dog(s)." 例如:“有n条狗。” When I tried There <#if dogNames?size>1>is<#else>are</#if> dogNames?size dog<#if dogNames?size>1>s</#if> , I received exception 当我尝试There <#if dogNames?size>1>is<#else>are</#if> dogNames?size dog<#if dogNames?size>1>s</#if> ,我收到了异常

freemarker.core.NonBooleanException: For "#if" condition: Expected a boolean, but this has evaluated to a number (wrapper: ftSimpleNumber): ==> dogNames?size freemarker.core.NonBooleanException:对于“ #if”条件:预期为布尔值,但这已评估为数字(包装器:ftSimpleNumber):==> dogNames?size

ie there is a problem with angle bracket used for comparison. 也就是说,用于比较的尖括号存在问题。

In this blog it is said that double quotes is enough to escape the bracket, but I'm not managed to do that in Java. 这个博客中 ,据说双引号足以逃脱括号,但是我无法在Java中做到这一点。 When I declared it like this 当我这样宣布

String template = "There <#if dogNames?size\">\"1>is<#else>are</#if> dogNames?size dog<#if dogNames?size>1>s</#if>";

and called freemarker api, I received exception 并调用了freemarker api,我收到了异常

freemarker.core.ParseException: Syntax error in template ...:
Encountered "\">\"", but was expecting one of:
"."
".."
<DOT_DOT_LESS>
"..*"
"?"
"??"
"!"
"["
"("
">"
<TERMINATING_EXCLAM>

I'm using freemarker 2.3.28, java 8 我正在使用freemarker 2.3.28,java 8

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM