【问题标题】:Freemarker - if else condition not workingFreemarker - 如果其他条件不起作用
【发布时间】:2021-09-16 22:08:36
【问题描述】:

在我的ftl 文件中,我正在写:

<#list myDataList as myData>
<p>
    <#if myData.action == 0>Added by
    <#else>Removed from
    </#if>
</p>
</#list>

在 java 代码中,action 的类型是 Integer。

我也试过myData.action == "0"

调试时可以看到action == 0

我遇到的错误:

freemarker.core.InvalidReferenceException: The following has evaluated to null or missing:
==> myData.action  [in template "email_template.ftl" at line 79, column 50]

【问题讨论】:

  • 尝试使用默认值myData.action!"0" == "0"(myData.action)!"0" == "0"
  • @user7294900 默认值不必是字符串。所以应该这样来避免任何数字格式问题(比如存在action,然后自动转换为0.0):myData.action!0 == 0

标签: java freemarker


【解决方案1】:

null 不同于 0(零)。改用“Missing value test”:

myData.action?? == false

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-12-03
    • 1970-01-01
    • 2023-03-30
    • 2017-02-18
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多