FreeMarker错误:对于" - "左手操作数:预期一个数字,但这已经评估为一个字符串

时间:2016-01-21 13:40:07

标签: freemarker

我正在尝试计算" - " s的地图和打印行中的值的总和(此行的长度应为sum-1)。 这是我的功能代码:

<#function getSeparatorLine map>
    <#if !map?has_content>
        <#return "">
    </#if>
    <#local borderLength = 0>
    <#list map?keys as item>
        <#local borderLength = borderLength + item + 1>
    </#list>
    <#return ""?right_pad(borderLength - 1, "-")>
</#function>

我收到了一条freemarker错误说:

ERROR freemarker.runtime - Error executing FreeMarker template
freemarker.core.NonNumericalException: For "-" left-hand operand: Expected a number, but this has evaluated to a string (wrapper: f.t.SimpleScalar):
==> borderLength  [in template "negative_events_report.template" at line 38, column 27]

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

item是一个字符串,因此borderLength + item + 1实际上是字符串连接而不是算术加法。假设map包含数值,则应编写map?values,而不是map?keys