PDA

View Full Version : Dynamic form and nested properties


HeideMeister
Jan 27th, 2005, 08:48 AM
Hi

I have a little problem with an application.

The submitted jsp contains a list of items and looks something like this:


<input type="text" name="myListObject[0].myProperty">
<input type="text" name="myListObject[1].myProperty">
<input type="text" name="myListObject[2].myProperty">


The number of items (and thus input fields) varies dynamically.
In my form/command I was planning to do something like this:


private List itemList = new ArrayList();
public MyClass getMyListObject(int index) {
if (itemList.size() <= index) {
for (int i = itemList.size(); i <= index; i++) {
itemList.add(new MyClass());
}
}
return (MyClass) itemList.get(index);
}


(+ getter/setter for the list)
So my form contains a List of MyClass objects, and I was anticipating that

myListObject[0].myProperty

when submitted would result in

getMyListObject(0).setMyProperty(......)

getting called. This does not seem to happen - when the controller (a SimpleFormController) is reached, only an empty list is present.

What am I doing wrong :?:

NOTE: Due to some limitation I'm stuck with Servlet spec. 2.2 and JSP spec. 1.1 - so I can't use any of the Spring tags. [/code]

HeideMeister
Mar 3rd, 2005, 09:04 AM
Hi again

I have solved my problem :P

On my form i have a default constructor that initializes the List with the following item:

new DynamicList(MyClass.class);

The DynamicList extends ArrayList, and is just an ArrayList that dynamically populates it self with objects of type MyClass, whenever a normal ArrayList would have thrown an ArrayIndexOutOfBoundsException.

Using this list, spring takes care of everything else, and I get a form with a nice List of the submitted objects :lol: