无法使用 Freemarker 从 2 层 ArrayList 打印数据

Can't print data from 2 layer ArrayList using Freemarker

我在 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);

class 单元格的代码:

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]

看来您必须为 data 创建一个 getter。表格 docs:

Note that public fields are not visible directly; you must write a getter method for them.