文档介绍:freemarker对null的处理
1 提供默认值
<#if mouse?>
Mouse found
<#else>
也可以直接${mouse?if_exists}
<#if ??>
/freemarker对null的处理
1 提供默认值
<#if mouse?>
Mouse found
<#else>
也可以直接${mouse?if_exists}
<#if ??>
//TO DO
</#if>
假设前提:userName为null
${userName} error
${userName!} 空白
${userName!'tivon'} tivon
假设前提:
${},异常
${()!},显示空白
${!'vakin'},,否则显示vakin
${?default('vakin')},同上
${???string(,'vakin')},同上
3 list
<#list userList as user>
…
</#list>
List指令还隐含了两个循环变量:
user_index:当前迭代项在所有迭代项中的位置,是数字值。
user_has_next:用于判断当前迭代项是否是所有迭代项中的最后一项。
这2个值也可以控制 null 或者0的出现。
4<#escape x as x!""></#escape>可以对所有的变量进行空值处理,这里是全部替换为空字符串。当然也可以替换为其它字符串。
如果其中某些变量不需要这种替换,可以加入<#noescape></#noescape>标签。
5 这种为全局配置方法
配置classic_compatible=true可以满足一般需要。默认情况变量为null则替换为空字符串,如果需要自定义,写上${empty!"EmptyValue of fbysss"}的形式即可
。Configuration cfg = new Configuration(); (true);//设置属性
。
Environment env = (root, out);
(true);
:在ftl前加入<!--#setting classic_compatible=true-->;
<bean id="freemarkerConfig"
class="">
<pr