Date: 2004-12-10T08:41:56 Editor: ShinobuKawai <[EMAIL PROTECTED]> Wiki: Jakarta-Velocity Wiki Page: VelocityFAQ URL: http://wiki.apache.org/jakarta-velocity/VelocityFAQ
Make it a bit more easier to read. Change Log: ------------------------------------------------------------------------------ @@ -35,13 +35,13 @@ [http://issues.apache.org/bugzilla/show_bug.cgi?id=20999 Bugzilla #20999], [http://issues.apache.org/bugzilla/show_bug.cgi?id=27741 Bugzilla #27741]. -''Approach 1:'' Use the fact that null is evaluated as a false conditional. (cf. http://jakarta.apache.org/velocity/user-guide.html#Conditionals) +'''''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) +'''''Approach 2:''''' Use the fact that null becomes an empty string is quiet references. (cf. http://jakarta.apache.org/velocity/user-guide.html#Quiet%20Reference%20Notation) {{{ #if( "$!car.fuel" == "" ) }}} @@ -51,19 +51,19 @@ #if( "$car.fuel" == "" ) }}} -''Approach 3:'' Combine Approach 1 and 2. This will check for '''null and null only'''. +'''''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). +'''''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. +'''''Approach 5:''''' Don't check for null directly, use a self-explaining method. {{{ #if( $car.fuelEmpty ) }}} @@ -75,10 +75,11 @@ } }}} -''Approach 6:'' Use a custom directive. +'''''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. {{{ @@ -91,6 +92,7 @@ === Velocity Tools === * Q: The !MessageTool is throwing a !NullPointerException, why doesn't VelocityTools test for NPE's? * A: This is because Struts' !ActionServlet needs to be initialized before trying to use the Struts tools. Have a look at your web.xml file and make sure it starts up first. (Nathan Bubna) +[[BR]] * Q: Why is the !VelocityServlet deprecated in favor of Tools !VelocityViewServlet? * A: Some time back, the Velocity Developers decided it might be better to separate web functionality from the core Velocity engine. They decided that VelocityTools would be a better place to drop this functionality. Nobody has had the guts to remove !VelocityServlet from the Core, maybe in 2.0? <grin> (Tim Colson) --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
