Freemarker常用方法

截取字符串

1
${record.date?substring(0,7)}

为null时取空字符串

如果属性可能为null

1
${name!}

如果对象和属性都可能为null

1
${(user.name)!}

不加小括号 如果对象为空就会报错

if else

if

1
2
3
<#if condition> 

</#if>

if-else

1
2
3
4
5
<#if condition> 

<#else>

</#if>

if-elseif-else

1
2
3
4
5
6
7
8
9
<#if condition> 

<#elseif condition2>

<#elseif condition3>

<#else>

</#if>

判断为空

1
2
3
4
5
<#if user??>
//user不为null
<#else>
//user为null
</#if>
1
2
3
4
5
<#if (user.name)??>
//userl和user.name都不为null
< #else>
//user为null或user.name为null
</#if>

比较字符串

1
2
3
<#if name=="1">

</#if>

html转义

1
${((article.content)!)?html}

日期格式化

1
${(mailVo.date)?string("yyyy-MM-dd HH:mm:ss")}