views:

119

answers:

1

Without using DynaForm and it's kin.

I would like to use a POJO data transfer object, e.g., Person:

public class Person {
   private Long id;
   private String firstName;
   private String lastName;
   // ... getters / setters for the fields
}

In the struts live action form we would have:

public class PersonUpdateForm extends SLActionForm {
   String organization;
   Person[] persons; // all the people will be changed to this organization; they're names and so forth can be updated at the same time (stupid, but a client might desire this)

   // getters / setters + index setters / getters for persons

}

What would the corresponding html:text tags look like in the JSP to allow this? If I switch to a List persons field and use a lazy-loading list (in commons-collections) how would that change thinsg?

There seems to be no good way to do this in struts-1.2(.9?)

All help is greatly appreciated!!! If you need more context let me know and I can provide some.

+1  A: 

Okay, I believe I've figured it out! The trick is to have your indexed getter create an element each time the getPersons() method is called by the populate method of BeanUtils. The code is completed yet, but I got a positive looking result. It's 3:30 and I've been stuck on this a while. Nobody seemded to know the answer, which makes me want to smack them in the head with a trout. As for my own ignorance ... I only have them to blame!

public List<Person> getPersons() {
   persons.add(new Person()); // BeanUtils needs to know the list is large enough
   return persons;
}

Add your indexed getters and setters too, of course.

I remember how I actually did this. You must pre-initialize the persons List above to the maximum size you expect to transfer. This is because the List is first converted to an array, the properties then set on each element of the array, and finally the List set back using setPersons(...). Therefore, using a lazy-loading List implementation or similar approach (such as that show above) will NOT work with struts live. Here's what you need to do in more detail:

private List<Person> persons = new ArrayList<Person>(MAX_PEOPLE);
public MyConstructor() { for(int i = 0; i < MAX_PEOPLE; i++) persons.add(new Person()); }

public List<Person> getPeopleSubmitted() {
    List<Person> copy = new ArrayList<Person>();
    for(Person p : persons) {
        if(p.getId() != null) copy.add(p); 
        // id will be set for the submitted elements;
        // the others will have a null id
    }
    return copy; // only the submitted persons returned - not the blank templates
}

That's basically what you have to do! But the real question is - who's using struts live anymore?!

LES2