You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Matt Raible <ma...@raibledesigns.com> on 2002/12/27 23:24:46 UTC

Copying Properties: The Good, the Bad and the Ugly

I wrote the following post on my website today and would *love* to get some
feedback.  Please reply by commenting on my website or just replying to this
post.  I've had better luck finding things on my website than this mailing
list - that's why I posted it there.

http://raibledesigns.com/page/rd/20021227#copying_properties_the_good_the
or 
http://tinyurl.com/3v0h

---
I realize that having an ActionForm and a POJO with the same getters/setters
is ridiculous, but please bear with me for this example. I have a Form and a
POJO with Strings, Longs and Dates. The Longs and the Dates get converted into
Strings when I get the data from the database using BeanUtils.copyProperties.
This works great.

BeanUtils.copyProperties(userForm, user);

However, when going back, it's a different story - here's the ugly way:

SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

Date dateChanged = format.parse(userForm.getDateChanged());
Date dateCreated = format.parse(userForm.getDateCreated());

user = new User(userForm.getUserId(), userForm.getPassword(), 
                Long.valueOf(userForm.getDesktopId()),
                Long.valueOf(userForm.getCubeId()), 
                userForm.getCreatedBy(), dateCreated,
		userForm.getChangedBy(), dateChanged);

While this works, it's ugly and a pain. I want something like the
ActionForm-Value Object mapper. This mapper allows you to easily copy
properties between VOs (or Hibernate Objects) and Forms, and vise-versa.

vo = FormToVOMapper.map(form, new ExampleVO());

So I could do something as simple as user = FormToVOMapper.map(userForm, new
User()); I like this mapper and I used it on my last project, where it works
great. However, I get the feeling that developers in the Struts Community are
using something better - and I want to use it. So please, tell me what it is
and lets figure out the best way to do this. Another method I've used in the
past is to set the VO (or object) on the form itself, allowing for setting of
Strings without copying - and setting dates on the form, to be manipulated by
the setter. This framework worked great, and I was actually the happiest with
it out of any of the above. Chime in and give me your opinions!

Thanks,

Matt

--
To unsubscribe, e-mail:   <ma...@jakarta.apache.org>
For additional commands, e-mail: <ma...@jakarta.apache.org>