Re: The validwhen validation's treatment of null and empty string

2006-06-02 Thread Scott Van Wart

Craig McClanahan wrote:
You need to test for both conditions in the latter case, but the 
particular

approach you suggest above is not necessarily going to work.  You cannot
reliably do "==" tests on strings if you are looking for equality -- you
need to use the equals() function instead.
Not sure if I communicated this correctly, as I referenced the 
'validwhen' only in the subject and didn't refer to it in my message 
body (just to set the record straight I've been using Java for 6 years, 
4 years as a full-time job).  I wasn't sure about the expression 
language that the "validwhen" validator uses.  It doesn't look exactly 
like JSP EL, but in the JSP expression language you can do stuff like:



 Hey, property is empty.


   and the test condition will be true if the following is true (in the 
Java language): ( property == null || property.equals( "" ) ).  Although 
this is only for java.lang.String objects.  Collection (List, Set, etc.) 
objects also work like collection.isEmpty().  BUT, the kind of 
expressions that the "validwhen" validator takes don't look like JSP EL, 
nor Java, and as all the examples seem to say stuff like


 
   
 test
 (property == null)
   
 

 the reference to "null" kind of concerns me, as it's possible for the 
bean to use the empty string "" in its properties (at least, that's what 
I use in all my beans, including the reset() methods).


- Scott

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Re: The validwhen validation's treatment of null and empty string

2006-06-02 Thread Craig McClanahan

On 6/2/06, Scott Van Wart <[EMAIL PROTECTED]> wrote:


If a bean's property is the empty string "", does a test like (property
== null) suffice?  Or do I have to say ((property == null) or (property
== ''))?



You need to test for both conditions in the latter case, but the particular
approach you suggest above is not necessarily going to work.  You cannot
reliably do "==" tests on strings if you are looking for equality -- you
need to use the equals() function instead.

When faced with the kind of test you're trying to do here, I would suggest
the following:

 if ((property == null) || property.equals("")) {
   ... it is either null or an empty string ...
 }

It is important to do the null test first, because if that test passes, the
expression evaluation rules guarantee that the application won't try to
evaluate property.equals("") as well.  That's a good thing ... because that
would generate a null pointer exception if property was indeed null.

If you want to learn more about Java basics things like this, I would
strongly recommend working your way through the Java Language Tutorial[1].
It has concise introduction to language concepts like this, plus it goes
over the basics of using many of the common Java APIs.

- Scott


Craig


[1] http://java.sun.com/docs/books/tutorial/


The validwhen validation's treatment of null and empty string

2006-06-02 Thread Scott Van Wart
If a bean's property is the empty string "", does a test like (property 
== null) suffice?  Or do I have to say ((property == null) or (property 
== ''))?


- Scott

-
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]