FreeMarker的怎么对空值的处理

来源:互联网 发布:淘宝信息管理系统 编辑:程序博客网 时间:2024/06/11 03:29

对于null,或者miss value,freemarker会报错

!:default value operator,语法结构为:unsafe_expr!default_expr,比如 ${mouse!"No mouse."} 当mouse不存在时,返回default value;
(product.color)!"red"  这种方式,能够处理product或者color为miss value的情况;
而product.color!"red"将只处理color为miss value的情况
??: Missing value test operator ,测试是否为missing value
unsafe_expr?? :product.color??将只测试color是否为null
(unsafe_expr)??:(product.color)??将测试product和color是否存在null
?exists:旧版本的用法
比如:<#if mouse??>
  Mouse found
<#else>
  No mouse found
</#if>
Creating mouse...
<#assign mouse = "Jerry">
<#if mouse??>
  Mouse found
<#else>
  No mouse found
</#if>

 

<#list listBlogPost as blogPost>
</#list>
如果 listBlogPost 为NULL 会报错.加上?exists(是否存在) 或者?default("")
如果为null时默认为空,例子:

<#if listBlogPost?exists && listBlogPost.size != 0 >

<#if Session["cartList"]?exists>       
<#list Session["cartList"] as item>

${pageTitle?default("")}

<#if s?exists>  
${s?if_exists}  
exists用在逻辑判断,而if_exists用来打印东西时用到,如果存在打印,不存在打印空字符串.  
exp1?exists将会被exp1??代替  
exp1?if_exists将会被exp1!代替  
exp1?default(exp2)将会被exp1!exp2.

原创粉丝点击