[英]Freemarker iterating over hashmap keys in a list
找到答案:$ {account [prop]}代替$ {account.get(prop)}有效
帳戶是具有以下類型的地圖列表
//[{"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]
這應該更好,您可能在從此處復制時忘記了將用戶更改為帳戶;)
<table>
<#list accounts as account>
<tr>
<#list account?keys as prop>
<td>${account[prop]}</td>
</#list>
</tr>
</#list>
</table>
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>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.