This is something for which I've tried to find an elegant solution on the last couple of projects I worked on.

Using DTO / POJOs from Hibernate for the Person and the Cats and Dogs, I end up using a sorted set of Cats or Dogs in the page context, which I then iterate over in the JSP.

This means the taglib for the checkbox / dropdown control points to the set / collection and names the methods for the label and the value:

<html-el:options collection="${dogList}" property="dogId"
  labelProperty="dogName" />

The form therefore has the dogId(s) getter and setter.

However handling the submit where you have to find the Dog pojo with the chosen ID and place it in the Person.setDog() or Person.getDogs().add() is frankly complex if not downright ugly (esp if doing deletes!)

There are also issues such as caching of the sets of Cats and Dogs, limiting the set where business rules apply, and internationalisation.

I intend to develop my caching mechanism soon, and to refactor my ugly submit helper method to make it handle this juggling of pojos better, so as you can see I am in the same boat as you.


Regards
Adam


Rick Reumann on 10/05/06 18:12, wrote:
Lets assume you want to be a good OO developer and you are designing
an application to handle CRUD stuff for a "Person." Lets say this
Person can own Cats and Dogs. So person might look like...

Person
------
int personId
String personName
List dogs; //list of Dog objects
List cats; //list of Cat objects

Your backend persistence layer of choice knows how to deal with this
Person. When it goes to insert/update a Person it knows how to update
the PersonDog and PersonCat tables with respective dog and cat ids.

Where I always run into problems is how to best handle this kind of
stuff on the front end in Struts for multi select options (and also
using multibox with checkboxes).

The dilemma first is "What should your PersonActionFrom hold in
relation to Cats and Dogs when all you need to capture is Dog/Cat Ids
on you form?"

The standard practice often espoused is your ActionForm should only be
interested in capturing the inputted data - so in this case it would
be String[] catIds,  String[] dogIds. This is what I'm currently
doing, but it then requires an extra conversion to convert these ids
into "Dog" and "Cat" objects so that I could pass a full "Person"
object to the backend/service layer.  (You also have to convert going
back the other way as well for when you want to do an update.)
Typically I use BeanUtils to do my copying of properties from
ActionForm --> ValueObject and back the other direction as well.
Currently I'm having to use special helper covert methods that use a
combination of BeanUtils and the custom conversions for stuff like
taking a String[] dogIDs and building Dog objects from them.

Just curious on approaches other people use and how do other
frameworks, like JSF, deal with this since they don't use ActionForms.
(For example if I have a "Person" backing bean with "Cats" and "Dogs"
in it, and my multiselect list allows me to choose dogs and cats, how
do these get updated in the backing bean.


---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to