在freemarker中的变量表达式中,“”是什么意思?

时间:2013-02-01 16:39:49

标签: freemarker templating ofbiz

我有来自apache开发书的以下代码:

<#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]!“”} (!“”变量内部或&lt; #assign keys = data?first?keys /&gt; 有人可以解释这两个表达式的语义含义。

1 个答案:

答案 0 :(得分:1)

错误的原因是data[key]的类型不合适。 !''部分在失败案例中没有任何作用,因为只有在data[key]null或{{1}}时才会启动。

否则请使用手册,它描述了这些运营商的意义以及此处的任何人都可以:http://freemarker.org/docs/dgui_template_exp.html#exp_cheatsheet