【问题标题】:Freemarker returns a null when using ?eval on string variableFreemarker 在字符串变量上使用 ?eval 时返回 null
【发布时间】:2019-12-29 19:43:03
【问题描述】:

在 Freemarker 中使用 时,如果我只是引用变量,但在使用 ?eval 时,我得到一个空值或缺失错误。

这行得通,newOwnerName 获取输入值

<#assign newOwnerName = jsonEventData.newOwnerName>

但这不起作用并导致异常:

<#assign newOwnerName = jsonEventData.newOwnerName?eval>

例外:

The following has evaluated to null or missing: jsonEventData.newOwnerName?eval

我尝试使用 ?eval 的原因是因为输出包含引号:新所有者名称是“Peter”。但我希望它不带引号:新所有者名称是 Peter

ftl 模板类似于:

<#assign newOwnerName = jsonEventData.newOwnerName>
The new owner name is ${newOwnerName}

我希望 ?eval 不应该导致 null 或丢失错误,因为值是在输入中发送的

【问题讨论】:

  • 我在online parser 中使用jsonEventData_newOwnerName = "\"Peter\"" 尝试了您的代码(将dot 替换为underscore 以进行平面存储)并输出The new owner name is Peter . ?eval 正确删除引号。再次检查jsonEventData.newOwnerName 的内容 - 缺少某些内容。
  • 工作数据:newOwnerName = "Peter"。模板: 新所有者名称为 ${newOwnerName} 无效数据:newOwnerName = "Peter" 模板: 新所有者名称为 ${newOwnerName} @youka

标签: java freemarker


【解决方案1】:

您可以使用replace freemarker 的内置删除双引号:

jsonEventData_newOwnerName?replace("\"","")

用于用另一个字符串替换原始字符串中所有出现的字符串。它不处理单词边界。例如:

【讨论】:

    【解决方案2】:

    你不能?eval 一个字符串值在其内容中没有引号。 Peter 不是 Freemarker 变量。如果您的 java 输入偶尔有或没有引用此错误是可以预料的。更简洁的解决方案是在 java 端检测和删除引号(子字符串)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-09
      • 2018-04-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-30
      • 2012-10-09
      • 2017-03-05
      相关资源
      最近更新 更多