Stjepan Brbot wrote:

Hi all,

Does STRUTS ActionForm/DynaForm have to consist only of String object
fields!? Although I saw this strong recommendation in some books and
articles on Internet (since all parameters in Request are Strings) I use
Action and DynaForms with java.lang.Integer fields and everything works
fine! (Integer fields are very convenient when one deals with EJBs where IDs
are of Integer type.) It seems that STRUTS framework automatically converts
Strings from request into defined fields types of Form. Why should one use
only String fields and avoid Form fields other than Strings?

Stjepan Brbot



Consider what would happen in the following scenario:

* You have a form bean property of type "int".

* The corresponding field on the page is an <html:text> that creates an input text field.

* Your user types "1a3" by mistake instead of "123".

What does your user expect to see? They expect to see an error message telling them that they typed something invalid, and they expect to see this input field redisplayed with "1a3" in it, so they can just fix the wrong part. That's exactly what you get if you use a String field in your form bean.

What happens if you use an "int" property, though? Answer: runtime exception because the conversion failed. This is why the correct design pattern for Struts is to use String fields in your form bean, coupled with validations that ensure that the characters typed in satisfy the requirements of being an integer. Then, in your Action, use something like BeanUtils.copyProperties() to copy these fields to your real Java object that uses the real data types -- this will only get called if the validations all succeeded, so you won't run the risk of conversion errors.

Craig McClanahan


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



Reply via email to