Davin,

Your approach will certainly work, and is sometimes the only possible
solution.  However, if your model bean and the form bean have exactly the
same set of properties, and they are of the same types, you can also say:

  PersonBean person = new PersonBean();
  PropertyUtils.copyProperties(person, form);

This does the same sort of "copy through introspection" trick that Struts
does for populating a form bean, but it doesn't do any type conversions.

Craig McClanahan


On Fri, 27 Jul 2001, Davin M wrote:

>    Hi everyone. I have a general question about using the ActionForm beans 
> with regular javabeans. As my understanding goes, the ActionForm beans can 
> be populated automatically after the user submits that form. Now that the 
> ActionForm bean is populated, it runs the appropiate Action. So in this 
> action, I want to translate a ActionForm bean to a regular javabean. This is 
> because I read that the ActionForm bean is tied more to the view then the 
> model. Once I have this javabean, I'll use it to update/add/modify/delete a 
> Person in the db. Unfortunately the two beans' properties match one-to-one. 
> Like maybe for example both have these method prototypes:
> 
> public String getName()
> 
> public setName(String name)
> 
>   So my question (finally) is where should I convert these Form beans to my 
> state beans? I thought of either doing it one of two ways:
> 
> 1)in the Action class...
> 
>     ...
>     PersonBean person = new PersonBean();
>     person.setUsername(form.getUsername());
> 
>     //use person object to do something (save in session, update db,etc)
> 
> 2)in the PersonBean class...
> 
>     public static PersonBean convert(ActionForm form) {
>       PersonBean person = new PersonBean();
>       person.setUsername(form.getUsername());
>       return person;
>     }
> 
>   and do this in the Action class...
> 
>     ...
>     PersonBean person = PersonBean.convert(form);
>     //use person object to do something (save in session, update db,etc)
> 
> =============
> 
>    With one property, it seems sort of trivial. But with a dozen or so, it 
> can really clutter the Action class in option 1). Well if you guys have any 
> insights on this, I'll be really appreciative. Thanks in advance!
> 
> 
> --Davin
> 
> _________________________________________________________________
> Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp
> 
> 

Reply via email to