我有HashMap汽车,而pojo Car包含属性'engines',它又是HashMap。
public class Car implements Serializable{
private Long id;
private String name;
private Map<Long,Engine> engines = new HashMap<>();
..
..
}
public class Engine implements Serializable{
private Long id;
private String name;
}
Freemarker模型
final StringWriter sw = new StringWriter();
Map model = new HashMap();
model.put("cars",theCarsMap);
Template tmpl = t.cfg.getTemplate("text.ftl");
tmpl.process(model, sw);
Freemarker配置
cfg = new Configuration();
cfg.setCacheStorage(new freemarker.cache.MruCacheStorage(20, 250));
cfg.setClassForTemplateLoading(getClass(), ".");
cfg.setObjectWrapper(new DefaultObjectWrapper());
模板代码:
<#assign rKeys = cars?keys>
<#list rKeys as rKey>
Car Details:${cars[rKey].getName()}\n
--------------------------------------------------\n
<#assign engines = cars[rKey].getEngines()>
<#assign tKeys = engines?keys>
<#list tKeys as tKey>
------------------Engine Details-----------------\n
Name: ${engines[tKey].getName()}\n
</#list>
</#list>
我收到以下错误:
有问题的指示:
${cars[rKey].getName()} [on line 3, column 18 in text.ftl]
是否存在与包含pojos和另一个Map的HashMap相关的特殊处理?
答案 0 :(得分:0)
使用POJO作为值的Map不起作用,我必须更改为List ....而且基本类型的java对象包装器也不起作用,更改为原始定义。即长---&gt;长
<#list cars as c>
${c.id!'Unknown'} - ${c.name!'Unknown'}
<#list c.engineList as t>
${t.id!'Unknown'} - ${t.name!'Unknown'}
</#list>
</#list>