Matt RaibleMatt Raible is a Web Developer and Java Champion. Connect with him on LinkedIn.

The Angular Mini-Book The Angular Mini-Book is a guide to getting started with Angular. You'll learn how to develop a bare-bones application, test it, and deploy it. Then you'll move on to adding Bootstrap, Angular Material, continuous integration, and authentication.

Spring Boot is a popular framework for building REST APIs. You'll learn how to integrate Angular with Spring Boot and use security best practices like HTTPS and a content security policy.

For book updates, follow @angular_book on Twitter.

The JHipster Mini-Book The JHipster Mini-Book is a guide to getting started with hip technologies today: Angular, Bootstrap, and Spring Boot. All of these frameworks are wrapped up in an easy-to-use project called JHipster.

This book shows you how to build an app with JHipster, and guides you through the plethora of tools, techniques and options you can use. Furthermore, it explains the UI and API building blocks so you understand the underpinnings of your great application.

For book updates, follow @jhipster-book on Twitter.

10+ YEARS


Over 10 years ago, I wrote my first blog post. Since then, I've authored books, had kids, traveled the world, found Trish and blogged about it all.

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
Comments:

We use BeanUtils.copyProperties quite successfully in our project to map the trivial forms to value objects with no problems. We, however, did register several custom converters to ensure data is copied properly. I don' t have the bandwidth to do your use cases justice and figure out why you are having trouble with it, but I wanted to post a success story with copyProperties. Keep at it!

Posted by Erik Hatcher on December 27, 2002 at 06:18 PM MST #

How did you register the custom converters? That's what I'm having a problem figuring out. Do you use methods in BeanUtils.copyProperties() or a parent class? Maybe just a one-line piece of code will get me on my way ;-)

Posted by Matt Raible on December 27, 2002 at 06:48 PM MST #

you need to have this feedback thing e-mail me if you reply... otherwise i'll rarely check back :) But, here's how I do it: // register conversion utils for forms ConvertUtils.register(new StringConverter(), String.class); ConvertUtils.register(new BlankStringToNullIntegerConverter(), Integer.class); ConvertUtils.register(new SqlTimestampConverter(), Timestamp.class); ConvertUtils.register(new AddressConverter(), AddressesData.class); ConvertUtils.register(new AddressConverter(), AddressForm.class); RTJD (read the JavaDocs): http://jakarta.apache.org/commons/beanutils/apidocs/org/apache/commons/beanutils/ConvertUtils.html

Posted by Erik Hatcher on December 27, 2002 at 09:16 PM MST #

Matt, I saw your weblog in a little journey to try and get a java.sql.Date back into a model bean from a form bean using BeanUtils.copyProperties. I wasn't ready to tackle Hibernate yet, although that looks like a good thing at the moment since I'm rolling my own object factories.. Anyway, I digress - I did post a solution for the issue of registering a Converter as a part of a plugin in the struts-config.xml, and it allowed a dateFormat property to be supplied. I submitted it to the struts-user list yesterday. May be OBE at this point, but thought I'd mention it. current journey is to figure out if there's a clean way to do this for dynaforms as well... regards, -jeff

Posted by Jeff Kyser on March 30, 2003 at 09:26 AM MST #

I too have run into your weblog because I've been trying to find out the best way to map collections in a parent form to collections in a parent value object. Anyways, I've used the mapper you referred to in the initial post by Andrej. I was very pleased with this except when you get into non-trivial Forms. For example, if you want to support collections of Forms in the form you are mapping, than it'll barf. Does anyone do something similar with Beanutils? I've played with setProperties, copyProperties, copyProperty and it all chokes once I get to my collections. An example would be good if you have one.

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 #

Post a Comment:
  • HTML Syntax: Allowed