velocity 判断 变量 是否不是空或empty

原先的 #if($mobile) 这种写法是不准确的 ,请换成 "$!{ mobile}"!=""

说明 :   #if($mobile)   这种写法 只能 对null 起作用

也就是说

#if($mobile)

  or receiver_mobile =:mobile

  #end

如果 mobile 参数 是 null, 那么sql 不会拼接 里面的文字

但是 ,如果 传入的 是 ""( empty 字符串), 一样会拼接文字

(注:当然你可以在 manager类里面 将 "" 先转成null,不怕麻烦的话 )

如果 既要 判断 null 又要判断 empty ,怎么做呢?

应该这么写:

   #if("$!{mobile}"!="")

   or receiver_mobile =:mobile

   #end

其中   $!{mobile}  是VTL 的 (安静引用符Quiet Reference Notation)

http://velocity.apache.org/engine/devel/user-guide.html#quietreferencenotation

官方文档这么描述的: 

如果 mobile 变量为null,或者未赋值,  直接使用${mobile}的话,  界面会显示这个变量名称 ,即(${mobile})

而 用 $!{mobile}, 则显示 空白文本

这里 wiki ,值得大家看看

http://wiki.apache.org/velocity/CheckingForNull

引用:http://feitianbenyue.iteye.com/blog/1824792

原文地址:https://www.cnblogs.com/chencidi/p/3643281.html