title: Spring 3 MVC: Issue binding to list form fields on submit
body: <p>Let me introduce my issue by providing some of the code in question.</p>
  
  <p>First my form object:</p>
  
  <pre><code>public class OrgChartForm {
  
      List&lt;OrgChartFormElement&gt; orgChartFormElements;
  
      public OrgChartForm() { 
          orgChartFormElements = new ArrayList&lt;OrgChartFormElement&gt;();
      }
  
      private OrgChartFormElement createOrgChartFormElementFromMprsStructureYear(MprsStructureYear structureYear){
          OrgChartFormElement element = new OrgChartFormElement();
          element.set.... // populate element based on attribute values from structureYear param
          return element;
      }
  
      public void createOrgChartFormElements(List&lt;MprsStructureYear&gt; structureYears) {
          orgChartFormElements = new ArrayList&lt;OrgChartFormElement&gt;();
          for(MprsStructureYear structureYear:structureYears){
              orgChartFormElements.add(createOrgChartFormElementFromMprsStructureYear(structureYear));
          }
      }
  
      // expected getters and setters
  }
  </code></pre>
  
  <p>The form contains a simple list of <code>OrgChartFormElement</code>s</p>
  
  <pre><code>public class OrgChartFormElement {
      private boolean selected;
      private String elementLabel;
      private Long id;
  
      //default constructor, getters and setters
  }
  </code></pre>
  
  <p>I am using <code>context:component-scan</code> and <code>mvc:annotation-driven</code>, so my controller looks like:</p>
  
  <pre><code>@Controller
  public class OrganisationStatusController{
  
      @Autowired(required=true)
      // dependencies here 
  
      @RequestMapping(value="/finyear/{finyearId}/organisationstatus", method=RequestMethod.GET)
      public String createRootOrg(@PathVariable(value="finyearId") Long finyearId, Model model) throws Exception {
          List&lt;MprsStructureYear&gt; orgStructuure = getOrganisationService().getOrganisationStructureForFinyear(finyearId);
  
          OrgChartForm orgChartForm = new OrgChartForm();
          orgChartForm.createOrgChartFormElements(orgStructuure);
          model.addAttribute("orgChartForm", orgChartForm);
  
          return "finyear/organisationchart/view";
      }
  
      @RequestMapping(value="/finyear/{finyearId}/organisationstatus", method=RequestMethod.POST)
      public String createRootOrg(@PathVariable(value="finyearId") Long finyearId,@ModelAttribute("orgChartForm") OrgChartForm orgChartForm, BindingResult result, Model model) throws Exception {
          System.out.println("Found model attribute: " + model.containsAttribute("orgChartForm"));
          List&lt;OrgChartFormElement&gt; elements = orgChartForm.getOrgChartFormElements();
          System.out.println(elements);
          return "redirect:/spring/finyear/" + finyearId + "/organisationstatus";
      }
  
      // expected getters and setters
  }
  </code></pre>
  
  <p>The issue is with the POST handler. I realise that it isn't doing much now, but once I get it to work, I will be persisting the submitted values.</p>
  
  <p>At the moment, the output i see from the two sysout statements are:</p>
  
  <pre><code>Found model attribute: true
  []
  </code></pre>
  
  <p>Here is my JSP snippet:</p>
  
  <pre><code>&lt;sf:form modelAttribute="orgChartForm" method="post"&gt;
      &lt;c:forEach items="${orgChartForm.orgChartFormElements}" var="org" varStatus="status"&gt;
          &lt;sf:hidden id="${org.id}field" path="orgChartFormElements[${status.index}].id"/&gt;
          &lt;sf:input id="${org.id}hidden" path="orgChartFormElements[${status.index}].selected"/&gt;
          &lt;c:out value="${org.elementLabel}"/&gt;(&lt;c:out value="${org.id}"/&gt;) - &lt;c:out value="${status.index}"/&gt;
      &lt;/c:forEach&gt;
      &lt;input type="submit" value="Submit" /&gt;
  &lt;/sf:form&gt;
  </code></pre>
  
  <p>When i make the GET request, the JSP renders, and i see my list of text input fields, with the expected values, which tells me that im using the spring-form tags properly. However, when i submit, the form backing object declared as a parameter (orgChartForm) in the POST handler method is initialised, but everything is null/default initialised. I don't know where the submitted data went! It seems that springMVC looses it, and simply constucts a new object.</p>
  
  <p>I have used this pattern extensively in this application without a glitch. It just wont work here. I realise this is a special case in my application where the form field is not atomic but a list, However its really confusing me that the data binds in the GET request, but not on the POST.</p>
  
  <p>Thanks in advance for any pointers!</p>