1. if else
<#if role.roleType == '1'> <td>管理角色</td>
<#elseif role.roleType=='2'><td>查询角色</td>
<#elseif role.roleType=='3'><td>业务角色</td>
<#else><td>其他</td>
</#if>
<#if r.manageType?? && (r.manageType=='basic' || r.manageType=='admin')>
</#if>
2. list
<#list roleList as role>
<td data-code="${role.code !}"> ${role.code !} </td>
</#list>
<#list userList as user>
<#if fields?size <= 0 > 无数据 </#if>
第${user_index+1}个用户
用户名:${user.userName}
密 码:${user.userPassword}
年 龄: ${user.age}
</#list>
<#list dataList as dto>
<div class="title">${dto.reason !}</div>
<table border="1">
<tr>
<th>code</th>
<th>名称</th>
<th>高度</th>
<th>涨停时间</th>
<th>原因</th>
<th>题材</th>
</tr>
<#list dataList[dto_index].emoTopStockList as stock>
<tr>
<td>${stock.code !}</td>
<td>${stock.name !}</td>
<td>$100</td>
<td>$100</td>
<td>$100</td>
<td>$100</td>
</tr>
</#list>
</table>
</#list>
3. null值处理
假设前提:userName为null
${userName} error
${userName!} 空白
${userName!'kevin'} kevin
4. ??
返回布尔值,表示是否存在
<#if name??>
……
</#if>
5. 三目运算
${item.enable?string("推荐","推荐") !}
${(1==1)?string('男','女')} 不支持
<#if user.sex ?? && user.sex=='1' >
男
<#else>
女
</#if>
${user.name?default('kevin')} kevin。
${user.name???string(user.name,'kevin')}, ??表示是否存在,存在则user.name,不存在则kevin。
6.日期
${createTime?datetime('yyyy-MM-dd hh:mm:ss')}
${createTime?string('yyyy-MM-dd hh:mm:ss')}
7.freemarker的变量空值报错
方法一、${xxx?'默认值'}
方法二、在类路径下 加入 freemarker.properties 文件,配置 classic_compatible=true。
方法三、通过源码freemarker.template.Configuration的 config.setClassicCompatible(true);
方法四、在ftl文件内引入 <!--#setting classic_compatible=true-->。
例如我在action中定义一个MyBean类的对象为 myBean,MyBean中有Comp属性。在页面上就要 用如下语句使用:${myBean.comp},这里myBean可能为null,comp也可能为null。
这时候 就要使用方法一了,${(myBean.comp)!} 或 ${(myBean.comp)?if_exists}。