以下引用官方描述:
The FreeMarker template language doesn't know the Java language null at all. It doesn't have null keyword, and it can't test if something is null or not.
1.判断是否存在,通过exists关键字或者"??"运算符。都将返回一个布尔值
user.name?exists
user.name??
<#if user.name?exists>
//TO DO
</#if>
<#if user.age??>
//TO DO
</#if>
2.忽略null值
假设前提:user.name为null
${user.name},异常
${user.name!},显示空白
${user.name!'vakin'},若user.name不为空则显示本身的值,否则显示vakin
${user.name?default('vakin')},同上
${user.name???string(user.name,'vakin')},同上
本文介绍了FreeMarker模板语言中处理空值的方法,包括如何判断变量是否存在、如何忽略null值并提供默认显示值等实用技巧。
667

被折叠的 条评论
为什么被折叠?



