struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stjepan Brbot" <stjepan.br...@ht.hr>
Subject Re: ActionForm not String type fields (properties)
Date Wed, 14 Apr 2004 15:48:58 GMT
Thanks Craig, to use BeanUtils and its copyProperties method was the reason why I wanted to
use FormBeans with fields other than String. After that I realized that BeanUtils has internal
conversion functionality. There are some problems when you have to convert date written in
String into java.sql.Date of my DTO but one cannot have everything!

Stjepan Brbot


"Craig McClanahan" <craigmcc@apache.org> wrote in message news:40778C3A.8050602@apache.org...
> 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: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message