Thursday, February 03, 2005

Trying out formdef

I decided to try out Hubert Rabago's formdef Struts plugin. After working through some silly mistakes, like getting the correct version of the formdef library for Struts 1.1, I think I finally got it working.

The framework we originally put together did not address the issue of business objects and Struts ActionForms very well. We naively just added as a member variable to each ActionForm (ValidatorForm) a data transfer object (DTO). These DTOs were the same ones used by the data access layer, so the data types of the variables in these "beans" were data types that the database expected. This is not ideal, as web forms should typically only deal with strings and maybe booleans.

As we got further into implementation and development using this paradigm, we discovered that we had to worry about data type conversions for displaying the data on a web form. It got a little cumbersome as a developer.

I stumbled across the formdef Struts plugin and it sounded like a good option. It introduced us to Dyna forms, which we had read about, but never investigated much. They're actually quite nice and save the developer the time of having to create ActionForm classes for each form. formdef takes it one step further by creating your dyna ActionForms for you based on a DTO (bean) that you specify, so the ActionForms match the business objects.

formdef also provides built in conversion methods to move data from the DTO to the form and vice versa. It is a very flexible library, providing many hooks and overrideables, so it should serve us well.

There was a thread on the Struts user list Nov 2004 that talked about this subject a little. Here is the URL to the original post. You can step through the response using the "next in thread" option. http://marc.theaimsgroup.com/?l=struts-user&m=110001898604664&w=2

[UPDATE 27Aug05]
The past couple of days I have tried to use formdef in a real web application and have run into a few problems. First off, I don't think formdef supports multi-select lists, i.e., <select multiple="multiple">. Second, I'm not sure how to implement a one-to-many relationship using formdef. Let me explain...

I use iBATIS for the DAO and db connection functionality. One feature iBATIS provides is that I can populate a DTO object from a query and the DTO can have a List property that stores the "many" side of the relationship. iBATIS will populate that List property automatically for me with all the related db rows.

I have not had success in creating the DynaValidatorForm when I have a List object in my DTO. formdef seems to choke on it, because when I remove it, everything is fine. Maybe there is a better way to handle one-to-many relationships when using formdef, not sure ??

No comments: