我在 eclipse 中使用 freemarker 2.3.23 来生成报告。 以下是数据模型的代码:
ArrayList<Cell> namelist=new ArrayList<Cell>();
Cell cell1=new Cell();
cell1.data.add("element1");
namelist.add(cell1);
Cell cell2=new Cell();
cell2.data.add("element2");
namelist.add(cell2);
data.put("namelist", namelist);
Cell 类的代码:
public class Cell {
public ArrayList<String> data;
public Cell(){
data=new ArrayList<String>();
}
}
模板代码:
<#list namelist as name>
<#list name.data as element>
${element}
</#list>
</#list>
但出现错误堆栈如下:
FreeMarker template error: The following has evaluated to null or missing: ==> name.data [in template "report.ftl" at line 33, column 16]
---- Tip: It's the step after the last dot that caused this error, not those before it. ---- Tip: If the failing expression is known to be legally refer to something that's sometimes null or missing, either specify a default value like myOptionalVar!myDefault, or use <#if myOptionalVar??>when-present<#else>when-missing. (These only cover the last step of the expression; to cover the whole expression,
use parenthesis: (myOptionalVar.foo)!myDefault, (myOptionalVar.foo)??
---- FTL stack trace ("~" means nesting-related):
- Failed at: #list name.data as element [in template "report.ftl" at line 33, column 9]
最佳答案
看来您必须为数据
创建一个getter。表格docs :
Note that public fields are not visible directly; you must write a getter method for them.
关于java - 无法使用Freemarker打印2层ArrayList的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31796068/