struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Craig R. McClanahan" <craig...@apache.org>
Subject Re: form bean types
Date Thu, 15 Mar 2001 19:19:37 GMT


On Thu, 15 Mar 2001, Young, Wayne wrote:

> I'm working on a "standards" document for struts development at work and
> have a question. 
> 
> I've seen references on the mailing list that fields on form beans
> (descendants of ActionForm) should only be java.lang.String if you want them
> to automatically populate your form fields on your .jsp page.
> 
> Is that a true statement? I'm thinking, NO.
> 

I would view this as a guideline, rather than a rule, for the following
reason:  one of the main goals of an ActionForm is to allow the
application to redisplay exactly what the user entered, in the case of a
validation error, so that the user can fix it.  This is a user
expectation because interactive applications have operated this way for
decades.

Now, consider that you have a field that you want the user to enter an
integer into, and you have it backed by a bean property that is an
"int".  Imagine that the user types "123c".  What happens?  The conversion
from string (which is the way a field comes in from the browser) to int
fails, and you cannot faithfully redisplay it.  All you can do is show the
default value (typically zero), which violates the user's expectations.

Using Strings for these kinds of things in your ActionForm allows you to
perform the conversion in your validate() method, create an application
error message if the conversion fails, and redisplay the page exactly as
the user typed it -- so that they can delete the mistaken "c" character
without having to type the "123" over again (assuming that was what they
really meant to enter).

Note that it's quite reasonable to use boolean properties in the form
bean, when the corresponding UI display is a checkbox.  That's because
there is no possible value the user can enter that is not a valid boolean.


> Wayne
> wyoung@metasolv.com
> 

Craig



Mime
View raw message