1

我有 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;
}

自由标记模型

final StringWriter sw = new StringWriter();
Map model = new HashMap();
model.put("cars",theCarsMap);
Template tmpl = t.cfg.getTemplate("text.ftl");
tmpl.process(model, sw);

自由标记配置

        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 相关的特殊处理?

4

1 回答 1

0

它不适用于以 POJO 作为值的 Map,我必须更改为 List .... 并且原始类型的 java 对象包装器也不起作用,更改为原始定义。即长--->长

<#list cars as c>
 ${c.id!'Unknown'} - ${c.name!'Unknown'}

 <#list c.engineList as t>
    ${t.id!'Unknown'} - ${t.name!'Unknown'}
 </#list>
 </#list>
于 2013-02-21T22:52:23.803 回答