数据模型:

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() {
}
}
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;
}
}
@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";
}

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、freemarker格式化日期防止为空导致异常。

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

【freemarker】渲染列表一系列操作的更多相关文章

  1. React.js 小书 Lesson13 - 渲染列表数据

    作者:胡子大哈 原文链接:http://huziketang.com/books/react/lesson13 转载请注明出处,保留原文链接和作者信息. 列表数据在前端非常常见,我们经常要处理这种类型 ...

  2. 问题:JFinal框架使用FreeMarker渲染视图报错

    本人用的是JFinal-3.4. 问题描述: 在JFinal框架中使用FreeMarker渲染视图时,报 Caused by: java.lang.ClassNotFoundException: fr ...

  3. itextpdf freemarker渲染

    现有需求涉及到打印pdf操作,简单找了俩种方式 在现有的模板上进行编辑,操作难度比较大 通过freemarker生成静态页面,在进行转换html,完美. 关于动态生成pdf,网上参考的挺多的,看来看去 ...

  4. 为什么你需要在用 Vue 渲染列表数据时指定 key

    本文改写整理自一篇博文,原文链接如下: Why you should use the key directive in Vue.js with v-for Application state and ...

  5. python学习04——列表的操作

    笨办法学python第38节 如何创建列表在第32节,形式如下: 本节主要是讲对列表的操作,首先讲了 mystuff.append('hello') 的工作原理,我的理解是,首先Python找到mys ...

  6. (转载)Python 列表(list)操作

    (转载)http://blog.csdn.net/facevoid/article/details/5338048 创建列表sample_list = ['a',1,('a','b')] Python ...

  7. Python基础学习8---list列表的操作

    a_list = ['hello','world',1,'shanghai',3.99] #列表添加操作的4种方法 #1. 通过+ 字符来拼接 a_list = a_list + [1,'wuhan' ...

  8. 用Vue实现状态列表的操作涵盖所有的知识点

    用Vue实现状态列表的操作涵盖所有的知识点

  9. python字符串,列表常用操作

    24天养成一个好习惯,第五天! 一.字符串需要掌握的操作 1.取值(索引取值)需要注意的是只能取,不能改 msg = 'hello world' print(msg[4]) 2.切片(顾头不顾尾) m ...

随机推荐

  1. oracle常用操作方法

    ---oracle创建数据库,基于plsqldev工具 1.创建表空间,创建空内容的物理文件create tablespace db_test --表空间名 datafile 'D:\test.dbf ...

  2. CSS3选择器 :read-only选择器 CSS3选择器 :read-write选择器

    CSS3选择器 :read-only选择器 “:read-only”伪类选择器用来指定处于只读状态元素的样式.简单点理解就是,元素中设置了“readonly=’readonly’” 示例演示 通过“: ...

  3. html+css+javascript学习记录1

    <p> 最近在学一部分前端,知识点很多,却没怎么系统地应用过,因而理解可能不够深吧.所以我想做点片段似的东西,不懂的再在网上搜一搜,这样可能会更有意思点,所以做了这个记录,希望自己坚持下去 ...

  4. Conservation Vs Non-conservation Forms of conservation Equations

    What does it mean? The reason they are conservative or non-conservative has to do with the splitting ...

  5. pip安装包出现timeout的解决办法

    今天安装django时老是出现timeout WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, sta ...

  6. xcode7 添加个人账户 is not on any development teams

    XCODE7已经可以免费真机测试, 但添加个人账户后,显示 is not on any development teams , 解决办法: 点击 “-” 删除当前账户,退出XCODE重新打开再添加即可 ...

  7. Bing Advanced Search Tricks You Should Know

    Bing is one of the world's most popular search engines that has gained many fans with its ease of us ...

  8. 史上最全USB HID开发资料

    史上最全USB HID开发资料 史上最全USB HID开发资料,悉心整理一个月,亲自测试. 涉及STM32 C51 8051F例子都有源码,VC上位机例子以及源码,USB协议,HID协议,USB抓包工 ...

  9. .NET Core:.Net Core 百科

    ylbtech-.NET Core:.Net Core 百科 .NET Core是适用于 windows.linux 和 macos 操作系统的免费.开源托管的计算机软件框架,是微软开发的第一个官方版 ...

  10. JS-让浏览器兼容ES6特性

    babel:将 ES6 翻译为 ES5 PS:ie 还不支持 import 和 export 还是用 gulp 打包一下吧