"Kan Leung, MK" wrote:
> Sorry if you find if this is a bit off topic. ;)
>
> Hi all,
>
> Hope this is not a FAQ. (I spent 2 hours to do net search but didn't
> get a good answer)
>
It is actually a pretty subtle point, and on-topic in my book :-)
>
> 8<
> <jsp:useBean name="abc" scope="session" .... />
> <jsp:setProperty name="abc" property="*"/>
> 8<
>
> Introspection magic makes the property setters called if they match the
> input parameters by GET or by POST.
>
> e.g. URL?address1=a&address2=&address3=
>
> There are exceptions: setters of some parameters which have no value
> won't get called. In the above string, the later two are not called
> because they have no values assigned. Suppose my bean has session scope.
> Values kept unchanged.
>
The exception you mention is specifically required in the JSP
specification. For any property that would be set to a zero-length string,
and you are using <jsp:setProperty name="xxx" property="*"/>, the setter
method is *not* called.
A very similar problem occurs with checkboxes -- if the checkbox is checked,
the value is included and the corresponding boolean setter is called. But,
if it is not checked, *nothing* is sent to the server, so your boolean
property will remain set :-(.
>
> My question is: how can the web users clear a text field if the field is
> incorrectly filled?
>
Struts deals with these issues via a combination of features:
* First, you should do your submits to an action defined in the
controller servlet, rather than back to the JSP page. This makes
the behavior of <jsp:setProperty> irrelevant, because it will never
be utilized.
* The Struts configuration file allows you to configure a form bean
for each input form. If you do so, the Struts controller will
automatically
populate the bean's properties based on your request parameters,
similar to what <jsp:setProperty> does.
* However, you would seem to still have a problem with boolean properties
and with setting String properties to zero length. This is resolved by
requiring your bean to implement a reset() method, which will set
all of the bean properties to default values (normally false for booleans,
and null for Strings).
Now, if your user erases the contents of a String field, the following
things happen:
* The reset() method of your form bean is called, setting the string
property to null.
* The setXxxxx() method is *not* called (to be consistent with
<jsp:setProperty/> semantics).
* The end result is that your property has been set to a null string,
which is what you normally want. You can also reset to a zero
length String constant if you prefer.
>
> I suppose the introspection magic is a life-saver. I just don't want to
> check the return value of request.getParameter("address2) for each input
> to determine if it's empty.
>
> Can anybody give me a good alternative to clear unwanted fields easily?
>
> --
> Kan LEUNG, M K
> email: [EMAIL PROTECTED]
> Digital Empires Company Limited
Craig McClanahan