1

Could someone please help me find out why my attempt to bind Collection to the form in Spring MVC is not working?

Here is how my object looks like -

public class TestObj {
   private Integer testNumber;
   private String home;
   private String destination;
}

Here is my form object that contains list of above object -

public class TestForm {
   private List<TestObj> testList;
   //contains getter and setter for testList
}

In my controller, I have implemented formBackingObject method -

public class MyController extends SimpleFormController {

    public MyController() {
        setCommandClass(TestForm.class);
        setCommandName("testForm");
    }

        protected Object formBackingObject(HttpServletRequest request) throws Exception {
           if (isFormSubmission(request)) {
              testForm = (TestForm) super.formBackingObject(request);
              //THIS ALWAYS RETURNS NULL ON FORM SUBMISSION 
              List<TestObj> testList = testForm.getTestList();
           } else {
              //load initial data using hibernate. TestObj is hibernate domain object.
              List<TestObj> testList = myService.findTestList();
              testForm = new TestForm(testList);
           }
           return testForm;
        }

Here is my JSP snippet -

<form:form commandName="testForm" method="post">
   <c:forEach items="${testForm.testList}" var="testvar" varStatus="testRow">
     <tr>
       <td>
          <form:hidden path="testList[${testRow.index}].home" />
          <c:out value="${testvar.home}" />
    </td>
    <td>
      <form:input path="testList[${testRow.index}].destination" />
    </td>
     </tr>
   </c:forEach>
   <tr><td><input type="submit"></td></tr>
</form:form>

While the first time I load the data shows up fine on the form, when I press submit button the control goes to the formBackingObject method and the isFormSubmission returns true. However, when I get the command object using super.formBackingObject(request), it returns the form object with the testList value as null. I am unable to figure out why this simple case is not working?

I will really appreciate any help in getting this to work.

JUG
  • 693
  • 9
  • 25

3 Answers3

0

Are you using Spring 3? If so, you should take a look at this post.

With respect to list processing and object binding, take a look at this post.

Community
  • 1
  • 1
skel625
  • 876
  • 3
  • 7
  • 17
  • Thank you for the reply. I just took over a application that is using Spring 2.0 so not yet in a position to use any of the Spring 3.0+ features. I also looked at the second link you posted with respect to binding and found the usage of AutoPopulatingList from another link mentioned there. However, that still does not help as I get the list back with all nulls. – JUG May 14 '12 at 04:35
  • My experience is exclusively with Spring 3. I'll do a bit of digging to see if I can help further, but hopefully someone else replies sooner for your sake! :) – skel625 May 14 '12 at 04:40
0

Try using the following code. May be that can solve your problem.

private List<TestObj> operationParameterses = LazyList.decorate(new ArrayList<TestObj>(), FactoryUtils.instantiateFactory(TestObj.class));

It won't return you all null list.

Hope that helps you.

Cheers.

Japan Trivedi
  • 4,445
  • 2
  • 23
  • 44
  • You will also need **commons-collection** library to achieve this. – Japan Trivedi May 14 '12 at 05:35
  • Thank you for your suggestion. I am going to try it out. Are you suggesting to put it in the TestForm in my code to initialize the testList with above code? – JUG May 14 '12 at 13:30
  • This still did not work. I don't understand why the items on the form are not getting mapped. The form TestForm is not coming back null but the List inside is always null on submit. I am suspecting somehow the setter on submit is not setting the elements from JSP to the form. I am running out of thoughts on this as to why it is not doing it. – JUG May 14 '12 at 14:03
0

I guess my understanding of formBackingObject method must be wrong. I removed that method from the implementation, used referenceData for initial form load and onSubmit to process it on submit. This works fine and does get collection in the form back as expected.

Thanks all for the help though.

JUG
  • 693
  • 9
  • 25