次のコードがあります-
public class Result {
private Map<String, String> dataPoints = new LinkedHashMap<String, String>();
public Map<String, String> getData() {
return Maps.newHashMap(data);
}
public Set<Map.Entry<String, String>> getDataEntries() {
return data.entrySet();
}
public void addData(final String key, final String value) {
this.data.put(key, value);
}
}
挿入順序を維持したいので、LinkedHashMap を使用しています。以下のように、freemarker コードでマップを反復処理しようとしています。ただし、例外があります。
<#if (result.dataPoints?keys?size > 0) >
<#list result.getDataEntries() as entry>
<tr>
<td width="35%">
<div>${entry.key}</div>
</td>
<td width="45%">${entry.value}</td>
<td width="19%"> </td>
</tr>
</#list>
</#if>
例外:
Expression result.getDataEntries is undefined on line 50, column 24 in settings/settings-
diagnostics.ftl. The problematic instruction: ---------- ==> list result.getDataEntries()
as entry [on line 50, column 17 in settings/settings-diagnostics.ftl] in user-directive
printDiagnosticResult [on line 64, column 25 in settings/settings-diagnostics.ftl] in
user-directive printDiagnosticResult [on line 76, column 13 in settings/settings-
diagnostics.ftl] in user-directive layout.landingbase [on line 1, column 1 in
settings/settings-diagnostics.ftl] ---------- Java backtrace for programmers: ----------
freemarker.core.InvalidReferenceException: Expression result.getDataEntries is undefined
on line 50, column 24 in settings/settings-diagnostics.ftl. at
freemarker.core.TemplateObject.assertNonNull(TemplateObject.java:124) at
freemarker.core.TemplateObject.invalidTypeException(TemplateObject.java:134) at
freemarker.core.MethodCall._getAsTemplateModel(MethodCall.java:114) at
freemarker.core.Expression.getAsTemplateModel(Expression.java:89) at
freemarker.core.IteratorBlock.accept(IteratorBlock.java:94) at
freemarker.core.Environment.visit(Environment.java:208) at
上記のコードを次のように置き換えると:
<#if (result.dataPoints?keys?size > 0) >
<#list result.dataPoints?keys as key>
<tr>
<td width="35%">
<div>${key}</div>
</td>
<td width="45%">${result.dataPoints[key]}</td>
<td width="19%"> </td>
</tr>
</#list>
</#if>
同じ順序になるようにマップを反復処理するにはどうすればよいですか?