Announcement Announcement Module
No announcement yet.
Collection Data Binding to/from form issue? Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Collection Data Binding to/from form issue?

    Hi - Sorry this is a long post, but I'm not sure how else to explain the issue I'm seeing. I have a class that has a collection with a complex object. I'm using the AutoPopulatingList so I can presumably create dynamic entries on my form. I'm working with 3.1.M2

    Pseudo code is below excluding simple getter/setters:
    class Student {
      String name;
      AutoPopulatingList<Course> courses = new AutoPopulatingList<Course>(Course.class);
    class Course {
      String name;
      String booksNeeded;
    Here's the basic parts of the controller -
    public class AddStudentController {
    @RequestMapping(value = "/student/new")
    public class AddStudentController extends BaseStudentController {
        public void setAllowedFields(WebDataBinder dataBinder) {
        @RequestMapping(method = RequestMethod.GET)
        public String newForm(Model model) {
            Student student = new Student();
            Course course = new Course();
            model.addAttribute("student", student);
            return NEW_STUDENT_VIEW;
        @RequestMapping(method = RequestMethod.POST)
        public String processNew(@ModelAttribute Student student, BindingResult result, SessionStatus status) {
        return "redirect:" + BASE_URL + student.getId();
    Here's the JSP fragment that displays the course information:
    <c:forEach items="${}" varStatus="vs">
            <td><form:input path="courses[${vs.index}].name" class="required" /></td>
    	<td><form:input path="courses[${vs.index}].booksNeeded" class="required number" /></td>
    	<td><input type="button" value="Delete" class="rdelete" /></td>
    I have javascript that allows a user to delete an entire TR from the table, and add a TR to the bottom of the table.

    In the issue I'm having, I create 2 NEW rows with the javascript after the initial GET, and then DELETE the 2nd one. So I have the original row and the 3rd row. The first row (, courses0.booksNeeded) is what comes back from the GET request The HTML looks like this before the post:

    	<td><input id="" name="courses[0].name" value="Math" type="text"></td>
    	<td><input id="courses0.booksNeeded" name="courses[0].booksNeeded" value="1" type="text"></td>
    	<td><input value="Delete" type="button"></td>
    	<td><input id="" name="courses[2].name" value="Science" type="text"></td>
    	<td><input id="courses2.booksNeeded" name="courses[2].booksNeeded" value="3" type="text"></td>
    	<td><input role="button" value="Delete" type="button"></td>
    What happens when I post this is that the AutoPopulatingList has 3 entries. That would be fine since The AutoPopulatingList is supposed to make room, but the problem is that the value of booksNeeded from the 3rd Course is actually showing up in the booksNeeded of the SECOND Course, and the 3rd Course booksNeeded value is NULL.

    It mixed the 2 together somehow.

    I don't know if this is a bug or if this is not how I should be doing this. I haven't found any good documentation on what is the best way to do this.

    I'm not using the @SessionAttribute annotation and I saw that in the petclinic example, but when I tried that, it doesn't remove any of the rows that are deleted by the user from the form. Also, the petclinic example didn't add/delete rows using javascript.

    Also, if there's a better way to show my example, please let me know, maybe I should put it out on cloudfoundry or something to show a live example.
    Last edited by sbirnie; Aug 15th, 2011, 09:47 PM.