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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23393375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 22:47:31  来源:igfitidea点击:

String comparison in freemarker not working

javafreemarker

提问by user3582550

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

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

<#else>
//do something

</#if>

The above gives me error that something is expected at the end. { ] && . Whats wrong in the above?

以上给了我一个错误,即最终会发生一些事情。{ ] && 。以上有什么问题吗?

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> ...

采纳答案by Asif Bhutto

Yes, Absolutely it will give you some thing like this

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

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

Because if else syntax is not correct, there are couple of things wrong.

因为如果 else 语法不正确,则有几件事是错误的。

Highlighted are are wrong thing in below line.

突出显示的是以下行中的错误内容。

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

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

you put the data in data model but in template for if/else foreach, list you can access it direct not with the ${}

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

Here is correct , for equality use ==for uneqality use !=

这是正确的,对于不平等使用==的平等使用!=

<#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_contentreturn true. see doc

has_content返回真。看文档

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 

see doc for is_string

有关is_string 的信息,请参阅文档

Further see matchesBuilt-ins for strings

进一步参见 matches字符串的内置函数