java - 无法使用Freemarker打印2层ArrayList的数据

标签 java templates 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);

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/

相关文章:

javascript - 将 freemarker 与 javascript ES6 模板字符串一起使用

java - XMLSerializer 和 OutputFormat 已弃用

java - 无法访问内部类中的变量而不将其设为 Final

c++ - 返回对象的类型;

c++ - 有没有办法为容器类型创建运算符/函数重载

Freemarker - 变量的默认值可能丢失或为空?

java - 如何检查字符串是否可以转换为数字

java - 我的程序中出现 "OutOfMemoryError: Java Heap Space"错误

java - Clojure:Java Interop IBM watson 对话服务

c++ - 模板特化 : Is not a Template