数据模型:

public class AddressVo implements Serializable {

    private static final long serialVersionUID = 1137197211343312155L;

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public AddressVo(String name) {
        this.name = name;
    }

    public AddressVo() {
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
public class UserVo implements Serializable {

    private String name;
    private Integer age;

    private List<AddressVo> addressVoList;


    private Date birthday;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public List<AddressVo> getAddressVoList() {
        return addressVoList;
    }

    public void setAddressVoList(List<AddressVo> addressVoList) {
        this.addressVoList = addressVoList;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.

 

@GetMapping("list")
    public String list(Model model){

        List<UserVo> userVoList=new ArrayList<>();
        for (int i=0;i<5;i++){
            UserVo userVo=new UserVo();
            userVo.setName("张三"+i);
            if(i!=3){
                userVo.setAddressVoList(Arrays.asList(new AddressVo("上海"+i),new AddressVo("北京"+i)));
            }
            userVo.setAge(i*5);
            if(i!=2){
                userVo.setBirthday(DateTime.now().plusDays(i).toDate());
            }
            userVoList.add(userVo);
        }
        model.addAttribute("userList",userVoList);
        return "userList";
    }
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.

 

 

freemarker模板

<table style="width: 1000px;height: auto" cellpadding="1" cellspacing="1">
        <thead>
            <tr>
                <th>姓名</th>
                <th>年龄</th>
                <th>地址</th>
                <th>生日</th>
                <th>操作</th>
            </tr>
        </thead>
        <#list userList as user >
            <tr>
                <#--防止user里没有name-->
                <td>${user.name!}</td>
                <td>${user.age!}</td>
                <td>
                    <#--防止user的addressVoList为空-->
                   <#list user.addressVoList!>
                       <#items as address>
                           ${address.name!} <#sep >,
                       </#items>
                    <#else >无地址
                   </#list>
                </td>
                <td>
                    ${(user.birthday?string("yyyy-MM-dd"))!"日期不存在"}
                </td>
                <td>
                    <#if user?is_even_item>偶数
                    <#else> 奇数
                    </#if>
                    ${user?counter}
        </td>
            </tr>
        </#list>
    </table>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.

 

展示:

【freemarker】渲染列表一系列操作_#if

 

 1、freemarker格式化日期防止为空导致异常。

 2、freemark遍历列表防止值为空导致异常。