You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@velocity.apache.org by ve...@jakarta.apache.org on 2004/12/04 11:45:54 UTC
[Jakarta-Velocity Wiki] Updated: VelocityFAQ
Date: 2004-12-04T02:45:54
Editor: ShinobuKawai <sh...@ieee.org>
Wiki: Jakarta-Velocity Wiki
Page: VelocityFAQ
URL: http://wiki.apache.org/jakarta-velocity/VelocityFAQ
Add null FAQ.
Change Log:
------------------------------------------------------------------------------
@@ -6,10 +6,10 @@
=== Velocity ===
* Q: Adding ## at the end of a template causes a parsing exception, what do I do?
* A: For now, a workaround is: add a newline to the end of the file.
-[[BR]]
+[[BR]]
* Q: Can velocity render 'dynamic properties' like #set ($bar = "name") $foo.$bar ?
* A: Not directly, but possible using !RenderTool or !ViewRenderTool in VelocityTools, Or a custom tool can be written using Velocity.evaluate(...).
-[[BR]]
+[[BR]]
* Q: How do I output values from a hash when I do not know the keys?
* A: Easy - remember, the context has real java objects, all public methods can be used.
Here is a solution provided by Shinobu Kawai:
@@ -18,15 +18,60 @@
#set($value = $myMap.get($key))
$key = $value
#end
-
+}}}
or you can do
-
+{{{
#foreach($entry in $myMap.entrySet())
$entry.key = $entry.value
#end
}}}
+[[BR]]
+ * 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].)
+''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 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 a blank String. What this approach is actually checking is whether the reference is '''null or blank'''.
+BTW, just checking for '''blank''' 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).
+{{{
+#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.
+}
+}}}
+[[BR]]
=== Velocity Tools ===
---------------------------------------------------------------------
To unsubscribe, e-mail: velocity-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: velocity-dev-help@jakarta.apache.org