【freemaker】之循环,判断,对象取值

时间:2021-11-06 16:51:09

entity:

public class Employee {
  private Integer id;
  private String name;
  private Integer age;
  private Group group;
}

test测试代码

    @Test
public void test00(){
List<Employee>ems=Arrays.asList(
new Employee(1, "李四1", 15),
new Employee(2, "李四2", 20),
new Employee(3, "李四3", 80));
Employee employee=new Employee(1, "张三", 15);
root.put("emp", employee);
root.put("emps", ems);
freemakerUtil.fprint(root, "01.ftl", fn+"01.html");
}

ftl模版文件

<body>
  <h1>对象取值:${emp.name}---${emp.id}----${emp.age}</h1>
  <br/>
  <#--以下显示了如何使用判断结构,注意在判断的标签中不用加入${}-->
  <h1>if判断</h1>
  <#if emp.age lt 18>
    ${emp.name}是童工
  <#elseif emp.age gt 60>
    ${emp.name}应该退休了
  <#else>
    ${emp.name}好好工作
  </#if>
  <br/>
  <h1>循环取值</h1>
  <#list emps as emp>
    ${emp.id}-----${emp.name}---${emp.age}<br/>
  </#list>
</body>

【freemaker】之循环,判断,对象取值

pom文件

<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>

01.html效果

【freemaker】之循环,判断,对象取值