At 11:17 AM 2/28/01 +1100, Nick Pellow wrote:
>I am very new to struts as is, but as I far as I can tell from reading
>the implementation of the above method,
>the ActionForm can only have String properties. Is this correct?
>
>If so, it would be nice if the ActionForm could support types other than
>String and the
>struts engine would convert these in a similar fashion as Ant does to
>the attributes in the xml build file.

Actually, Struts will attempt to convert values as it populates a form 
bean. This is done in BeanUtils.populate(), which is called right at the 
end of RequestUtils.populate(). However, the problem is what to do if it 
can't be converted. For example, if I define a form bean property as an 
int, and the request contains "abc", what should Struts do?

In 1.0, it will set the property to a (configurable) default value, but 
that's not a good solution when there's no obvious candidate meaning 
"invalid" (e.g. for a boolean). It also destroys the original user input, 
so when validate() fails and returns the user to the input form, you can no 
longer display to them the mistake they made. (By default, in the situation 
I described above, the user would see "0" where they entered "abc".)

So it's really best if form bean properties are all strings. What you *can* 
do, though, is have your validate() method check that the value can be 
converted to what you want, and return an error if it can't. For example, 
you might use Integer.parseInt() to ensure that a valid integer was entered.

--
Martin Cooper
Tumbleweed Communications


Reply via email to