As Robert mentioned, the best practice is to declare all the ActionForm
properties as Strings or booleans. 

The purpose of the ActionForm is not to represent your business data. It
is to serve as a buffer for the HTML controls. HTTP is String-based, and
so the ActionForm properties need to be String-based too. Otherwise, as
you have discovered, invalid values disappear, and cannot be presented
to the user for correction. 

Once the String values have been validated, you can then transform them
to their proper type, and store them in a business bean designed for
this purpose. 

Another important function of the ActionForm is to serve as a firewall
between the Web users and the rest of your application. We have no
control over what insane values someone can toss at our actions via
HTTP. The ActionForm is a DMZ where we can accept whatever value is
tendered, but validate it before it passed along to business objects
(which tend to be more trusting). 

For more about the design of ActionForms, see also

http://www.mail-archive.com/struts-user@jakarta.apache.org/msg19338.html

http://www.mail-archive.com/struts-user@jakarta.apache.org/msg08070.html

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Java Web Development with Struts.
-- Tel +1 585 737-3463.
-- Web http://www.husted.com/struts/



cool dude wrote:
> 
> Hi,
>    Here's a small problem that I'm facing. I have a
> form which has an integer field, say a cost of some
> item. Now, I need to do the validation for this
> form-field to make sure that the user does not enter a
> non-numeric data in this field. I have the form
> containing this field as a primitive int. The problem
> is that whenever a non-numberic value is entered the
> value is reset to zero. This leads to two problems,
> one it doesn't give me a chance to find out that the
> user has entered a non-numeric value in my validate
> method & secondly, I end up losing the value that the
> user had entered & hence can't show him the value
> back. The rather crude solution to this is to make the
> numeric form-field value a String & do the check in
> the validate() to find if a non-numeric value is
> entered. But I'm not happy with this solution as it
> requires me to decalre a field as string even when it
> should have been an int or float ...
> 
> Is there any other way around this prob?
> 
> Thanx in advance,
> VD.
> 
> __________________________________________________
> Do You Yahoo!?
> Send FREE Valentine eCards with Yahoo! Greetings!
> http://greetings.yahoo.com
> 
> --
> To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

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

Reply via email to