Announcement Announcement Module
Collapse
No announcement yet.
why isn't this list of objects in a form-backing bean binding to the bean? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • why isn't this list of objects in a form-backing bean binding to the bean?

    I've read related questions here and elsewhere and thought I had the jsp form binding syntax right, but it's not working. The usage is a little odd. In one form, I want to capture basic information about a family member, including the number of children. Upon first submission, the controller saves the new family member. If the numChildren > 0, the controller creates new Individual objects for the children and adds them to the children collection of the form bean. Then the controller returns to the same form, where it displays the info just entered for the new family member, and empty forms for each of the children. That much is working.

    What's not working is the 2nd submission of the form. In the controller, in the form bean, the children Individual objects are still empty. The data from the child forms is not bound to the child Individual objects in the form bean.

    I've seen related answers refer to Spring's AutoPopulatingList, but from what I've read, that's for when the list of items are dynamically added by javascript, which is not what I'm doing.

    Here's my form-backing bean:

    Code:
    public class PersonChildrenFormBean {
        
        Individual mainPerson = new Individual();
        int numChildren;
        String personId;
        List<Individual> children = new ArrayList<Individual>();
        	
        //  .. various other getters and setters
        
        public List<Individual> getChildren() {
        	return children;
        }
        
        public void setChildren(List<Individual> children) {
        	this.children= children;
        }
    }
    here's the jsp snippet:

    Code:
    <c:forEach items="${personChildrenFormBean.children}" var="newChildren" varStatus="status">
        <div class="onePersonFormContainer">						
            <label for="firstName" id="firstName_label">First Name:</label>
            <input name="personChildrenFormBean.children[${status.index}].firstName" size="65" /><br>
            <label for="lastName" id="lastName_label">Last Name:</label>
            <input name="personChildrenFormBean.children[${status.index}].lastName" size="65" /><br>
            <label for="alive" id="alive_label">Alive?</label>
            <input type="radio" name="personChildrenFormBean.children[${status.index}].alive" value="true" />Yes &nbsp;&nbsp;&nbsp; <input type="radio" name="personChildrenFormBean.children[${status.index}].alive" value="false" />No, deceased <br>
            <label for="age" id="age_label">Current Age:</label>
            <input name="personChildrenFormBean.children[${status.index}].age" size="5" /><br>					
        </div>
    </c:forEach>

  • #2
    Try to use <form:form commandName="yourBackingObject"> tag.

    And use:
    Code:
    <form:input path="children[${status.index}].firstName>
    inside <form:form ... > tag instead of yours <input name...> tags.
    Last edited by PiratDrogowy; Feb 16th, 2012, 02:31 AM.

    Comment

    Working...
    X