You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by David Boardman <db...@wagpaw.com> on 2001/09/06 00:15:00 UTC

EJB, Form beans, value objects, etc.

I am hoping that someone can clear up what has become a foggy picture in my
mind regarding the transfer of data between an ActionForm bean and
corresponding entity beans.

Ted Husted wrote:

"Using a nested bean on your ActionForm lets you use the form like a
wrapper, so you don't have to define everything twice, if your business
beans and ActionForm beans would otherwise be identical"

I take this to mean that if my ActionForm bean and entity bean are identical
then I can supply getters and setters on the ActionForm bean for the entity
bean and use the nested bean format in my jsp.  In this way my ActionForm
will be populated correctly from the entity bean and changes made in the jsp
will be reflected in the entity bean.

A second scenario occurs when properties on the ActionForm do not match
those on the entity bean.  In this situation I am a little unclear what to
do.  One suggestion I read was to create a toMap() method on the ActionForm
bean and then use this with the BeanUtils.populate() method to populate your
entity bean.  My question here does the data transformation take place in
the toMap() method, or does BeanUtils.populate() do the transformation for
you?  Also, this takes care of moving data from the ActionForm bean to the
entity bean, but how about doing the opposite (populating the ActionForm
bean from the entity bean), does the entity bean also need to expose a
toMap() method that can then be used with BeanUtils.populate() to populate
the ActionForm bean?

A couple final questions:

1)I have seen a lot written about value object beans.  From what I gather
these seem to be light weight proxies for entity beans.  Is this correct? If
so what is the advantage to using them over entity beans?  How are they
typically instantiated, is there a method provided by the entity bean?  How
does data get from a value object bean to its corresponding entity bean?

2)I have seen several references to PropertyUtils class.  Does this class
enter in any way to solutions to my questions above?

Thanks for your help.

Regards,

Dave



Re: EJB, Form beans, value objects, etc.

Posted by Ted Husted <hu...@apache.org>.
David Boardman wrote:
> Ted Husted wrote:
> 
> "Using a nested bean on your ActionForm lets you use the form like a
> wrapper, so you don't have to define everything twice, if your business
> beans and ActionForm beans would otherwise be identical"
> 
> I take this to mean that if my ActionForm bean and entity bean are identical
> then I can supply getters and setters on the ActionForm bean for the entity
> bean and use the nested bean format in my jsp.  In this way my ActionForm
> will be populated correctly from the entity bean and changes made in the jsp
> will be reflected in the entity bean.

The Struts tags support a nested syntax, so you don't actually need to
duplicate the getters and setters. 

For example, given another bean stored as "values, you can write 

<td align="right" nowrap>Email:</td>
<td align="left">&nbsp;<html:text property="values.email" size="20"
maxlength="40"/></td>

See Struts-simple for more.

http://husted.com/about/struts/resources.htm#new

> A second scenario occurs when properties on the ActionForm do not match
> those on the entity bean.  In this situation I am a little unclear what to
> do.  One suggestion I read was to create a toMap() method on the ActionForm
> bean and then use this with the BeanUtils.populate() method to populate your
> entity bean.  My question here does the data transformation take place in
> the toMap() method, or does BeanUtils.populate() do the transformation for
> you?  Also, this takes care of moving data from the ActionForm bean to the
> entity bean, but how about doing the opposite (populating the ActionForm
> bean from the entity bean), does the entity bean also need to expose a
> toMap() method that can then be used with BeanUtils.populate() to populate
> the ActionForm bean?

You pass BeanUtils.populate() a bean and a Map, and it uses reflect to
populate the bean from the Map. 

If the properties names did not match, you would write the Map so that
they match the target. 

To close the loop, you can add a toMap() method to your entity bean, or
use some other mechanism. If you did not want to alter the bean, another
approach would be a static utility class that took the bean and returned
a Map.

> 1)I have seen a lot written about value object beans.  From what I gather
> these seem to be light weight proxies for entity beans.  Is this correct? If
> so what is the advantage to using them over entity beans?  How are they
> typically instantiated, is there a method provided by the entity bean?  How
> does data get from a value object bean to its corresponding entity bean?

This is probably your best reference

http://java.sun.com/j2ee/blueprints/ejb_tier/design_guidelines/index.html#1046498

> 
> 2)I have seen several references to PropertyUtils class.  Does this class
> enter in any way to solutions to my questions above?

It's another utility class used by populate() to discover the accessors. 

The source for both is in the util package in Struts 1.0 (and in the
Jakarta Commons for 1.1). Good read really. 

(Just an aside -- Craig writes an truly outstanding JavaDoc, and the
rest of us have tried to follow his example. Any Struts developer who
doesn't spend some time with the JavaDocs is missing the other half of
the documentation. Really. I can't emphasize that enough.)

-- Ted Husted, Husted dot Com, Fairport NY USA.
-- Custom Software ~ Technical Services.
-- Tel +1 716 737-3463
-- http://www.husted.com/about/struts/