Velocity中如何判斷null

2021-08-22 18:36:46 字數 647 閱讀 5849

相信這個問題是很多使用velocity的朋友所經常碰到的,大家可能非常期望下面這樣的**能夠執行:

#if($foo == null)

...#end

實際上這段**會報錯。這是因為velocity中對null的支援比較特別。**上說,判斷是否為null有下面幾種方法:

1. #if (! $foo) 判斷$foo為空,判斷非空為 #if ($foo)

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

#ifnull ($foo)

要使用這個特性必須在velocity.properties檔案中加入:

userdirective = org.apache.velocity.tools.generic.directive.ifnull

userdirective = org.apache.velocity.tools.generic.directive.ifnotnull

3. 使用null工具判斷

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

注意這種方式特別有用,尤其你在需要這個判斷作為乙個判斷字句時,比如我要你判斷乙個集合為null或為空時只能使用這種方式了:

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

Velocity中如何判斷null

相信這個問題是很多使用velocity的朋友所經常碰到的,大家可能非常期望下面這樣的 能夠執行 if foo null end 實際上這段 會報錯。這是因為velocity中對null的支援比較特別。上說,判斷是否為null有下面幾種方法 1.if foo 判斷 foo為空,判斷非空為 if foo...

Velocity判斷null跟空

velocity判斷null和空 velocityapache 1 判斷velocity 是否為null 方法一 ifnull 或 ifnotnull eg ifnull warn 要使用這個特性必須在velocity.properties 檔案中加入 userdirective org.apach...

velocity判斷空和null

判斷是否為null有下面幾種方法 1.if foo 判斷 foo為空,判斷非空為 if foo 2.使用 ifnull 或 ifnotnull ifnull foo 要使用這個特性必須在velocity.properties檔案中加入 userdirective org.apache.velocit...