You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by "Sobkowski, Andrej" <An...@emergis.com> on 2001/12/17 15:36:54 UTC

[ANN] Mapper ActionForm-to-Value Object and back

Hi All,

after reading a lot about problems related to mapping an ActionForm (somehow
a "set of String properties mapping the data on the Presentation layer") to
more business-oriented objects (Value Objects for example), I've written a
simple "mapper" that will allow you to:
- keep 'em separated (don't mix 'em up, they have different
responsibilities)
- move the data from one to the other and back in a couple of lines of code.

Here's the URL:

http://www.mycgiserver.com/~andrej/technical/struts/struts.jsp

Note: don't worry about the presentation, the site sucks.. I'll take care of
it later :). Also, the connection isn't really fast...

Here's an example:

ExampleForm
+getLastName:String
+setLastName(String)
+getDateOfBirth:String
+setDateOfBirth(String)
+getYearsOfJavaExperience:String
+setYearsOfJavaExperience(String)
+getEmailAddresses:String[]
+setEmailAddresses(String[])
+getChild:ChildForm
+setChild(ChildForm)

ExampleVO
+getLastName:String
+setLastName(String)
+getDateOfBirth:Date
+setDateOfBirth(Date)
+getYearsOfJavaExperience:Integer
+setYearsOfJavaExperience(Integer)
+getEmailAddresses:String[]
+setEmailAddresses(String[])
+getChild:ChildVO
+setChild(ChildVO)

To perform the ActionForm-VO mapping, simply write:

FormToVOPropertyMapper mapper = new FormToVOPropertyMapper(form, vo);
mapper.map(); 

And you're done! The mapper will take care of matching the getters and the
setters of the two objects, will loop on the array elements and will also
use recursion to convert the nested ActionForm to the corresponding nested
Value Objects!

Please note that it's not based on external configuration files but only on
method names (get/set matching). It is very useful when your ActionForm and
your Value Objects are almost identical and the main difference is that the
ActionForm can only deal with String while the Value Object should have more
business data (Date, Integer, ...) - which is usually the case for the
project I'm working on.

I've found this simple package pretty useful. There still are some
limitations but I wanted to make it available asap in case someone could
find it helpful. The code is well commented and the site above should
provide enough details to evaluate the utility.

Any feedback (really, any!) is appreciated.

Andrej