0

我从 apache ofbiz 开发书中获得了以下代码:

<#macro displayData data>
  <#if data?is_sequence>
    <#assign keys = data?first?keys/>
  <#else>
    <#assign keys = data?keys/>
</#if>
  <#-- Header -->
  <tr>
    <#list keys as key>
      <td class="dark-grid"><b>${key}</b></td>
    </#list>
  </tr>
  <#-- Data -->
  <#if data?is_sequence>
    <#list data as record>
        <tr>
            <#list keys as key>
            <td class="light-grid">${record[key]!""}</td>
            </#list>
        </tr>
    </#list>
  <#else>
        <tr>
          <#list keys as key>
          <td class="light-grid">${data[key]!""}</td>
          </#list>
         <h1>Processed script: "${parameters.scriptName}"</h1>
          <#if data?has_content && (data?is_sequence || data?is_hash)>
        </tr> </#if>
  </#if>
</#macro>

当我尝试在前端显示某些内容时,出现以下错误:

Error on line 31, column 38 in component://crmsfa/webapp/crmsfa/tests/displayData.ftl Expecting a string, date or number here, Expression record[key]!"" is instead a freemarker.ext.beans.SimpleMethodModel The problematic instruction: ---------- ==> ${record[key]!""} [on line 31, column 36 in component://crmsfa/webapp/crmsfa/tests/displayData.ftl] in user-directive displayData [on line 7, column 9 in component://crmsfa/webapp/crmsfa/tests/displayData.ftl] ---------- Java backtrace for programmers: ---------- freemarker.core.NonStringException: Error on line 31, column 38 in component://crmsfa/webapp/crmsfa/tests/displayData.ftl Expecting a string, date or number here, Expression record[key]!"" is instead a freemarker.ext.beans.SimpleMethodModel at freemarker.core.Expression.getStringValue(Expression.java:126) at

freemarker.core.Expression.getStringValue(Expression.java:93)

我想知道两件事,以便我可以首先调试这个我从来没有遇到过${record[ key ]!""}(变量中的 !"" )或data?first?keys/>有人可以解释一下这两个表达式的语义。

4

1 回答 1

1

错误的原因是类型data[key]不合适。该!''部分在失败的情况下没有任何作用,因为只有在存在data[key]null缺失时才会起作用。

否则请使用手册,它描述了这些运算符的含义以及这里的任何人都可以:http: //freemarker.org/docs/dgui_template_exp.html#exp_cheatsheet

于 2013-02-02T08:57:29.057 回答