entity

public class Employee {
    private Integer id;
    private String name;
    private Integer age;
    private Group group;
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.

test测试代码

@Test
    public void test02(){
        try {
            List<Employee>ems=Arrays.asList(new Employee(1, "张三1", 15),
                    new Employee(2, "张三2", 20),new Employee(3, "张三3", 80));
            root.put("emps", ems);
            root.put("name", "李四");
            root.put("emp", new Employee(1, "张三4", 15));
            freemakerUtil.fprint(root, "02.ftl",fn+"02.html");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.

 ftl模版

<body>
    <#--自定义变量-->
    <#assign name="张三">
    <h1>获取自定义变量值</h1>
    ${name}
    <h1>globals获取数据模型中的变量值</h1>
    ${.globals.name}<br/>
    <h1>循环变量</h1>
    <#list emps as emp>
        ${emp.name}<br/>
    </#list>
    <h1>对象取值</h1>
    ${emp.name}
</body>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

 效果

【freemaker】之自定义变量,特殊变量 globals ,循环对象取值_自定义

  

当自定义变量和数据变量重名时可以使用特殊变量globals,将数据模型中的变量值取出来