[简单]Freemarker日期字段为空时格式化出错处理记录
最近温习下 Freemarker用法,定义的Ftl模版如下:
<!DOCTYPE html>
<html>
<head>
<title>FreeMarker Spring MVC Practice</title>
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css">
</head>
<body>
<div class="jumbotron">
<h2>FreeMarker Spring MVC Practice</h2>
</div>
<div class="col-md-6 col-md-offset-1">
<table class="table table-striped">
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>BirthDate</th>
</tr>
</thead>
<tbody>
<#list model["userList"] as user>
<tr>
<td>${user.firstname}</td>
<td>${user.lastname}</td>
<td>${user.birthDate?string("yyyy-MM-dd")}</td>
</tr>
</#list>
</tbody>
</table>
</div>
</body>
</html>
User对象时间字段可以为空,访问时候报错,如下:
解决方法:
使用默认值!,如下:
<td>${(user.birthDate?string("yyyy-MM-dd"))!}</td>
或者:
<td>${(user.birthDate?string("yyyy-MM-dd"))!'无数据 '}</td>
结果如下:
全文完。