Domanda

I have the following code from the apache ofbiz development book:

<#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>

when I try to display something in the front end I get the following error:

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)

I would like to know two things in order that I may be able to debug this first I've never come across a ${record[key]!""} (the !"" inside the variable) or <#assign keys = data?first?keys/> could someone please explain the semantic meaning of these two expressions.

È stato utile?

Soluzione

The reason of the error is that the type of data[key] is not appropriate. The !'' part has no role in the failing case, as that only kicks in if data[key] is null or missing.

Otherwise please use the manual, it describes the meaning of those operators as well as anybody here could: http://freemarker.org/docs/dgui_template_exp.html#exp_cheatsheet

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top