struts-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Rick Reumann" <ric...@gmail.com>
Subject Re: How do otherS deal with being OO and dealing with multi-selects on front end?
Date Thu, 11 May 2006 18:13:28 GMT
What I ended up doing is

1) A Helper class will generate my String[] conversions to and from a
List of "SomeObject"

2) My form beans stick with String[] properties for multiple
selects/multibox situations

3) Rather than use BeanUtils.copyProperties( myFormBean, Person ) in
my Action, I end up putting two methods in my FormBean...

//PersonActionForm
public void PopulatePerson( Person person ) {
    BeanUtils.copyProperties( person, this );
    person.setDogs( Helper.populateDogsFromStringArray( this.dogIds ) );
}

public void PopulateFormFromPerson( Person person ) {
    BeanUtils.copyProperties( this, person );
    this.setDogIds( Helper.populateDogIdsFromDogs( person.getDogs() ) );
}

When inserts or updates are done, all that is needed is typically an
"ID" so what the Helper  class methods do in building the List, of say
Dogs, is just build a Dog object with only the ID populated.

Of course the Person object can now still be used for retrievals when
you really want it to contain complete lists of Dogs and Cats (as an
example).

I'm not super happy with the solution, but it works. In other option
was maybe to just stuff some extra String[] array properties into the
value object but that seems sort of lame.

On 5/11/06, Adam Hardy <ahardy.struts@cyberspaceroad.com> wrote:
> 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: user-unsubscribe@struts.apache.org
> For additional commands, e-mail: user-help@struts.apache.org
>
>


-- 
Rick

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscribe@struts.apache.org
For additional commands, e-mail: user-help@struts.apache.org


Mime
View raw message