2014-05-23 28 views
-1

答案已被找到:$ {帳戶[道具]},而不是$ {account.get(丙)}工作Freemarker的遍歷HashMap的鍵列表中的

帳戶是圖有相似

類型的列表
//[{"id":"1","location":"warehouse1"},{"id":"2","location":"warehouse2"}] 
ArrayList<HashMap<String, String>> t = m.t(); 
root.put("accounts", t); 
Writer out = null; 
try { 
    out = context.response.getWriter(); 
    T.getViewTpl(tplName).process(root, out); 
} catch (IOException | TemplateException ex) { 
    T.logger.error(T.class.getName(), ex); 
} finally { 
    try { 
     out.close(); 
    } catch (IOException ex) { 
     T.logger.error(T.class.getName(), ex); 
    } 
} 

它被呈現爲表格。一個帳戶一列,一個帳戶一列。下面的代碼不起作用。它是如何完成的?提前致謝!

<table> 
    <#list accounts as account> 
    <tr> 
     <#list account?keys as prop> 
     <td>${account.get(prop)}</td> 
     </#list> 
    </tr> 
    </#list> 
</table> 

Freemarker的錯誤是:

FreeMarker template error 

The following has evaluated to null or missing: 
==> account.get [in template "account.tpl" at line 14, column 23] 

回答

2

這應該是更好的,你忘了更改用戶帳戶,可能同時從here複製;)

<table> 
<#list accounts as account> 
    <tr> 
    <#list account?keys as prop> 
     <td>${account[prop]}</td> 
    </#list> 
    </tr> 
</#list> 
</table> 
+0

謝謝您回答! '用戶'是一個錯字。它已被糾正。但仍然無法工作。錯誤通過freemarker是:以下評估爲空或丟失: ==> account.keys [模板「account.tpl」在第13行,第24列] – Peter

+0

如何將地圖傳遞到Java的ftl? – Niemand

+0

粘貼這裏的代碼很難看。你能否看到更新的問題? – Peter

0
Check null condition before iterating a list or map 

<table> 
<#if accounts??> 
<#list accounts as account> 
    <tr> 
    <#list account?keys as prop> 
     <td>${account[prop]}</td> 
    </#list> 
    </tr> 
</#list> 
</#if> 
</table> 
+0

不需要'#if',你可以寫'<#list accounts!作爲帳戶>'。儘管實際上你肯定想跳過整個表格,然後你可以使用'<#list accounts!>

... <#items as account> ... ...
'。 – ddekany