velocity判断空和null

来源:互联网 发布:鬼吹灯 盗墓笔记 知乎 编辑:程序博客网 时间:2024/06/05 18:58

判断是否为null有下面几种方法:

     1. #if (! $foo) 判断$foo为空,判断非空为 #if ($foo)

     2. 使用 #ifnull() 或 #ifnotnull()

     #ifnull ($foo)

     要使用这个特性必须在velocity.properties文件中加入:

userdirective = org.apache.velocity.tools.generic.directive.Ifnulluserdirective = org.apache.velocity.tools.generic.directive.Ifnotnull

     3. 使用null工具判断

     #if($null.isNull($foo))

     注意这种方式特别有用,尤其你在需要这个判断作为一个判断字句时,比如我要你判断一个集合为null或为空时只能使用这种方式了:

     $if ($null.isNull($mycoll) || $mycoll.size()==0)

=========================================

1、判断Velocity 是否为null
      方法一   、 #ifnull() 或 #ifnotnull()
     Eg: #ifnull ($XXX)


     Warn:要使用这个特性必须在velocity.properties 文件中加入:
     userdirective = org.apache.velocity.tools.generic.directive.Ifnull
     userdirective = org.apache.velocity.tools.generic.directive.Ifnotnull

      方法二   、$null.isNull()
     Eg:#if($null.isNull())

     推荐使用方法二,特别是在判断集合对象为null时,灰常有用。

2、判断Velocity是否为空

      判断空:#if (! $xxx)
      判断非空: #if ($xxx)

3、更详细的介绍请参考:
      http://wiki.apache.org/velocity/VelocityNullSupport   
      http://wiki.apache.org/velocity/CheckingForNull

0 0
原创粉丝点击