我尝试从 Freemarker 模板调用 java 方法:
我的数据类:
public class SimpleObject {
private String id;
private String name;
public SimpleObject(String id, String name) {
setId(id);
setName(name);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
我的服务等级:
public class MainService {
List<SimpleObject> getAll() {
SimpleObject so = new SimpleObject("1", "Maksim");
List<SimpleObject> sol = new ArrayList<SimpleObject>();
sol.add(so);
return sol;
}
}
转换代码:
MainService ms = new MainService();
Map<String, Object> root = new HashMap<String, Object>();
root.put("results", ms.getAll());
StringWriter out = new StringWriter();
Configuration cfg = new Configuration();
cfg.setObjectWrapper(new DefaultObjectWrapper());
Template temp = cfg.getTemplate(".test.ftl");
temp.process(root, out);
System.out.println(out.getBuffer().toString());
.test.ftl
<#list mc.getAll() as result>
result.getName()
</#list>
错误:
Expression mc is undefined on line 3, column 8 in .test.ftl.
The problematic instruction:
----------
==> list mc.getAll() as result [on line 3, column 1 in .test.ftl]
----------