Friday December 27, 2002
Copying Properties: The Good, the Bad and the Ugly 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!
Posted in Java
at Dec 27 2002, 03:14:29 PM MST
6 Comments
Search This Site
Recent Entries
- What's Next
- Jack's Mohawk
- LinkedIn Cuts 10% (a.k.a. The Journey is Over)
- Happy Birthday Abbie!
- Moving from Spring's XML to Annotations in AppFuse
- Free Maven Training in New Orleans on Election Day
- AppFuse Light ยป AppFuse, Maven Archetypes and Shared Web Assets
- Great Weekend in Montana
- Colorado Software Summit 2008 Wrapup
- RESTful Web Applications with Subbu Allamaraju
Posted by Erik Hatcher on December 27, 2002 at 05:18 PM MST #
Posted by Matt Raible on December 27, 2002 at 05:48 PM MST #
Posted by Erik Hatcher on December 27, 2002 at 08:16 PM MST #
Posted by Jeff Kyser on March 30, 2003 at 08:26 AM MST #
Posted by Roland Chan on May 02, 2003 at 08:33 AM MDT #
What I'm going to ask is completely unrelated to this thread, but I happened to run into this page and I looked at the code above and wondered whether this code actually works in reality. I copied the code and pasted it in a test class and it didn't work. I was never able to parse a date that has the format yyyy-MM-dd. I always had to replace the dashes with slashes so that it would work. SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); System.out.println(format.parse("1976-09-24")); java.text.ParseException: Unparseable date: "1976-09-24" at java.text.DateFormat.parse(DateFormat.java:319) at test.TestMisc.testDateParse(TestMisc.java:8) at test.TestMisc.main(TestMisc.java:14) Exception in thread "main" Process exited with exit code 1.Posted by Tarek Nabil on April 27, 2005 at 07:29 AM MDT #