找到答案:$ {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]
答案 0 :(得分:2)
这应该更好,您忘记将用户更改为帐户,可能是在从here复制时;)
<table>
<#list accounts as account>
<tr>
<#list account?keys as prop>
<td>${account[prop]}</td>
</#list>
</tr>
</#list>
</table>
答案 1 :(得分: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>