At line 1 changed 1 line. |
''This is from a [discussion|http://www.mail-archive.com/struts-user%40jakarta.apache.org/msg69836.html] on the struts-user mailing list. I'm continuing it here because I want to propose a new idea: __only converting ActionForms to POJOs__.'' |
This is from a [discussion|http://www.mail-archive.com/struts-user%40jakarta.apache.org/msg69836.html] on the struts-user mailing list. I'm continuing it here because I want to propose a new idea: __only converting ActionForms to POJOs__. By this, I mean to say that I'd like to retrieve and display POJOs on the UI, and capture their information (as ActionForms) when saving the form. The reason I want to do this is because of Hibernate's Lazy Loading feature. Basically, Hibernate allows you to load children of an object, i.e. Resumes of a User, when the {{getResumes()}} method is called on User. |
At line 3 changed 1 line. |
When I first started developing with Struts (pre 1.0), I'd just have a simple form with a DTO (VO back then) as a getter/setter on my form. In the last year, I've changed to have a DTO and a Form, where the DTO has the true data types (Long, Date, etc) and the form has only Strings. After doing it both ways, it seems like the DTO as a getter/setter is really the better way to go. I'm not trying to say it's the *right* way according to the design patterns, I'm just offering my $.02 from experience. I continue to do it the BeanUtils.copyProperties() way, but the projects I work on that do it the other way seem cleaner. |
When using BeanUtils.copyProperties() to convert a POJO to an ActionForm, the {{getResumes()}} method is called. However, I don't want this. I'd rather the getResumes() method is called when I call it in my code. Therefore, I'm thinking that passing the POJO to the view is the best solution. Because I'm using the open-session-in-view pattern, those lazy collections are still available while the JSP is being rendered. |
At line 5 changed 1 line. |
For an example of a DTO -> BeanUtils.copyProperties() -> Form, check out the following links: |
This solves another issue too. It's nice to format dates on the UI, and you need a real date on your form to do this, rather than a String value of a date that's already been converted. |
At line 7 changed 3 lines. |
DTO -> User.java (source: http://tinyurl.com/emo7) |
Conversion -> UserManagerImpl.java (source: http://tinyurl.com/emof - see convert() method). This extends BaseManager (http://tinyurl.com/emok), which registers custom converters. No DateConverter in this example, but I've done it at my day job, so I know it's fairly easy. |
Form -> UserForm.java (generated via XDoclet from User.java) |
One issue might be with checkboxes, but I can just use the Form for that, right? I haven't tried this yet, but I wanted to write my thoughts down and stir up some discussion on it beforehand. |