牛骨文教育服务平台(让学习变的简单)
博文笔记

velocity判断是否为null和 “”

创建时间:2017-05-11 投稿人: 浏览次数:701

方法 1: 对null和false有效

#if( ! $car.fuel )


方法 2: 对null和""有效
#if( "$!car.fuel" == "" )
如果判断字段为空:
#if( "$car.fuel" == "" )


组合 方法 1 and 2,判断字段只能是null
#if ((! $car.fuel) && ("$!car.fuel" == ""))


方法 4: 使用工具检测,详见:http://wiki.apache.org/velocity/NullTool
#if( $null.isNull($car.fuel) )


方法 5: 使用自身方法检测
#if( $car.fuelEmpty )
car类实现isFuelEmpty()方法即可
public boolean isFuelEmpty()
{
  // return true if fuel is empty.
}


方法 6: 使用自定义指令. cf. IfNullDirective, IfNotNullDirective
#ifnull( $car.fuel )
#ifnotnull( $car.fuel )
必须在velocity.properties.中注册:
userdirective = org.apache.velocity.tools.generic.directive.Ifnull
userdirective = org.apache.velocity.tools.generic.directive.Ifnotnull

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。