freemarker中的字符串比较不起作用

时间:2014-04-30 16:27:17

标签: java freemarker

<#if ${primaryName!}?has_content> && ${key?js_string!}=="Joe">
//do something

<#elseif ${primaryClass!}?has_content> && ${key?js_string!}=="Apple">
//do something

<#else>
//do something

</#if>

上面给出的错误是最终会出现一些问题。 {]&amp;&amp; 。上面有什么不对?

Encountered "{" at line 26, column 39 in abc.ftl. Was expecting one of: ">" ... "." ... "[" ... "(" ... "?" ... "!" ... <TERMINATING_EXCLAM> ... "??" ... "+" ... "-" ... "*" ... "/" ... "%" ... "!=" ... "=" ... "==" ... ">=" ... <ESCAPED_GTE> ... ">" ... <ESCAPED_GT> ... <LESS_THAN_EQUALS> ... <LESS_THAN> ... ".." ... <AND> ... <OR> ...

1 个答案:

答案 0 :(得分:2)

是的,绝对会给你这样的东西

Encountered "{" ...
Was expecting one of:
    ">" ...
    "." ...
    "[" ...
    "(" ...
    "?" ...
    "!" ...

因为if else语法不正确,所以有几件事情是错误的。

在下面突出显示是错误的。

&lt; #if $ {primaryName !}?has_content >&amp;&amp; $ {键js_string?!} ==&#34;乔&#34;&GT;

您将数据放在数据模型中,但是在if / else foreach的模板中,列表您可以直接访问它而不是使用$ {}

这是正确的,对于使用==

的不平等使用!=
<#if primaryName?has_content && key?is_string && key=="Joe">
 // do somthing
<#elseif primaryClass?has_content && key?is_string && key=="Apple">
// elseif do something
<#else>
// else do something
</#if>

has_content 返回true。见doc

Note that when your data-model implements multiple template model interfaces you may get unexpected results. However, when in doubt you can use always use expr!?size > 0 or expr!?length > 0 instead of expr?has_content.

There is no js_string method , but its is is_string return true, if value is string 

请参阅is_string的文档

进一步了解matches Built-ins for strings

相关问题