Differences between revisions 1 and 2
Revision 1 as of 2005-04-26 18:48:05
Size: 2731
Editor: adsl-64-167-198-184
Comment: extract page : CheckingForNull
Revision 2 as of 2005-04-26 18:50:23
Size: 2751
Editor: adsl-64-167-198-184
Comment: add reference
Deletions are marked like this. Additions are marked like this.
Line 9: Line 9:
[http://issues.apache.org/bugzilla/show_bug.cgi?id=27741 Bugzilla #27741].   [http://issues.apache.org/bugzilla/show_bug.cgi?id=27741 Bugzilla #27741],
VelocityNullSupport
.

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. (Thanks, everybody, for all the [http://nagoya.apache.org/eyebrowse/BrowseList?listName=velocity-user@jakarta.apache.org&by=thread&from=949954 feedback on the user list].) See also: [http://issues.apache.org/bugzilla/show_bug.cgi?id=20999 Bugzilla #20999], [http://issues.apache.org/bugzilla/show_bug.cgi?id=27741 Bugzilla #27741], VelocityNullSupport.

Approach 1: Use the fact that null is evaluated as a false conditional. (cf. http://jakarta.apache.org/velocity/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 is null or false.

Approach 2: Use the fact that null is evaluated as an empty string in quiet references. (cf. http://jakarta.apache.org/velocity/user-guide.html#Quiet%20Reference%20Notation)

#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 is null or empty.

BTW, just checking for empty can be achieved by:

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

Approach 3: Combine Approach 1 and 2. This will check for null 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, NullTool must 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.Ifnull
userdirective = org.apache.velocity.tools.generic.directive.Ifnotnull

CheckingForNull (last edited 2012-12-11 14:51:05 by 195)