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

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

我正在尝试使用可以是复数的对象实现模板。

例如:"There is/are n dog(s)." 当我尝试 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: f.t.SimpleNumber): ==> dogNames?size

即用于比较的尖括号有问题。

this blog中据说双引号足以转义括号,但我在Java中没有做到这一点。当我这样声明时

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

并调用了 freemarker api,我收到了异常

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

我正在使用 freemarker 2.3.28,java 8

你可以写<#if dogNames?size gt 1>。 (参见 https://freemarker.apache.org/docs/dgui_template_exp.html#dgui_template_exp_comparison