国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

Velocity CheckingForNull

2019-11-15 00:25:38
字體:
來源:轉載
供稿:網友
Velocity CheckingForNull

Q: I want to check for null, something like this:

#if ($car.fuel == null)

A: There are several apPRoaches. Select the one most suitable depending on what you really want to do.

Approach 1:Use the fact that null is evaluated as a false conditional. (cf.http://velocity.apache.org/engine/devel/user-guide.html#Conditionals)

#if( ! $car.fuel )
  • Note:The conditional will also pass if the result of $car.fuel is the boolean false. What this approach is actually checking is whether the reference isnull or false.

Approach 2:Use the fact that null is evaluated as an empty string in quiet references. (cf.http://velocity.apache.org/engine/devel/user-guide.html#quietreferencenotation)

#if( "$!car.fuel" == "" )
  • Note:The conditional will also pass if the result of $car.fuel is an empty String. What this approach is actually checking is whether the reference isnull or empty.

BTW, just checking foremptycan be achieved by:

#if( "$car.fuel" == "" )

Approach 3:Combine Approach 1 and 2. This will check fornull and null only.

#if ((! $car.fuel) && ("$!car.fuel" == ""))
  • Note:The logic underlying here is that: "(null or false) and (null or > empty-string)" => if true, must be null. This is true because "false and empty-string and not null" is never true. IMHO, this makes the template too complicated to read.

Approach 4:Use a Tool that can check for null (NullTool,ViewNullTool).

#if( $null.isNull($car.fuel) )
  • Note:Of course,NullToolmust be in the Context as $null in this case.

Approach 5:Don't check for null directly, use a self-explaining method.

#if( $car.fuelEmpty )
  • Note:This is my (Shinobu Kawai's) recommended solution. You have to implement the method, but it makes the template so easy-to-read.

public boolean isFuelEmpty(){  // return true if fuel is empty.}

Approach 6:Use a custom directive.cf.IfNullDirective,IfNotNullDirective

#ifnull( $car.fuel )#ifnotnull( $car.fuel )
  • Note:You will have to register the directive in your velocity.properties.

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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 万州区| 江山市| 区。| 江达县| 定结县| 大英县| 农安县| 石渠县| 历史| 丁青县| 阳春市| 绥芬河市| 白银市| 南昌市| 江北区| 沾益县| 金沙县| 岳阳市| 正蓝旗| 延边| 荣成市| 怀安县| 始兴县| 靖江市| 离岛区| 康平县| 蚌埠市| 祁东县| 安宁市| 防城港市| 察哈| 潼关县| 民勤县| 赤水市| 罗源县| 河北省| 民权县| 万源市| 晋城| 泰兴市| 巴彦淖尔市|