Announcement Announcement Module
Collapse
No announcement yet.
Dynamic form and nested properties Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Dynamic form and nested properties

    Hi

    I have a little problem with an application.

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

    Code:
    <input type="text" name="myListObject&#91;0&#93;.myProperty">
    <input type="text" name="myListObject&#91;1&#93;.myProperty">
    <input type="text" name="myListObject&#91;2&#93;.myProperty">
    The number of items (and thus input fields) varies dynamically.
    In my form/command I was planning to do something like this:

    Code:
    private List itemList = new ArrayList&#40;&#41;;
    public MyClass getMyListObject&#40;int index&#41; &#123;
    	if &#40;itemList.size&#40;&#41; <= index&#41; &#123;
    		for &#40;int i = itemList.size&#40;&#41;; i <= index; i++&#41; &#123;
    			itemList.add&#40;new MyClass&#40;&#41;&#41;;
    		&#125;
    	&#125;
    	return &#40;MyClass&#41; itemList.get&#40;index&#41;;
    &#125;
    (+ getter/setter for the list)
    So my form contains a List of MyClass objects, and I was anticipating that

    Code:
    myListObject&#91;0&#93;.myProperty
    when submitted would result in

    Code:
    getMyListObject&#40;0&#41;.setMyProperty&#40;......&#41;
    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]

  • #2
    Hi again

    I have solved my problem :P

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

    Code:
    new DynamicList&#40;MyClass.class&#41;;
    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:

    Comment

    Working...
    X