'Spring 3 MVC: Issue binding to list form fields on submit

Let me introduce my issue by providing some of the code in question.

First my form object:

public class OrgChartForm {

    List<OrgChartFormElement> orgChartFormElements;

    public OrgChartForm() { 
        orgChartFormElements = new ArrayList<OrgChartFormElement>();
    }

    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<MprsStructureYear> structureYears) {
        orgChartFormElements = new ArrayList<OrgChartFormElement>();
        for(MprsStructureYear structureYear:structureYears){
            orgChartFormElements.add(createOrgChartFormElementFromMprsStructureYear(structureYear));
        }
    }

    // expected getters and setters
}

The form contains a simple list of OrgChartFormElements

public class OrgChartFormElement {
    private boolean selected;
    private String elementLabel;
    private Long id;

    //default constructor, getters and setters
}

I am using context:component-scan and mvc:annotation-driven, so my controller looks like:

@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<MprsStructureYear> 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<OrgChartFormElement> elements = orgChartForm.getOrgChartFormElements();
        System.out.println(elements);
        return "redirect:/spring/finyear/" + finyearId + "/organisationstatus";
    }

    // expected getters and setters
}

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.

At the moment, the output i see from the two sysout statements are:

Found model attribute: true
[]

Here is my JSP snippet:

<sf:form modelAttribute="orgChartForm" method="post">
    <c:forEach items="${orgChartForm.orgChartFormElements}" var="org" varStatus="status">
        <sf:hidden id="${org.id}field" path="orgChartFormElements[${status.index}].id"/>
        <sf:input id="${org.id}hidden" path="orgChartFormElements[${status.index}].selected"/>
        <c:out value="${org.elementLabel}"/>(<c:out value="${org.id}"/>) - <c:out value="${status.index}"/>
    </c:forEach>
    <input type="submit" value="Submit" />
</sf:form>

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.

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.

Thanks in advance for any pointers!



Solution 1:[1]

I think the problem is that you are trying to bind an arbitrary number of form fields to an ArrayList, which is a list that has a predetermined size.

Spring has something called an AutoPopulatingList that is custom designed for this purpose. Please have a look at this link for more info on how to use it: http://blog.richardadamdean.com/?p=12

Solution 2:[2]

I think you will need to write PropertyEditorSupport for your class. Following is the example for your reference.

public class SampleEditor extends PropertyEditorSupport {

private final SampleService sampleService;

public SampleEditor(SampleService sampleService, Class collectionType) {
    super(collectionType);
    this.sampleService = sampleService;
}

@Override
public void setAsText(String text) throws IllegalArgumentException {
    Object obj = getValue();
    List list = (List) obj;
    for (String str : text.split(",")) {
        list.add(sampleService.get(Long.valueOf(str)));
    }
}

@Override
public String getAsText() {
    return super.getAsText();
}

}

In controller, you should bind it using @InitBinder as follows:

@InitBinder
protected void initBinder(HttpServletRequest request, WebDataBinder binder) {
    binder.registerCustomEditor(List.class, "list", new SampleEditor(this.sampleService, List.class));
}

Hope this will solve your problem.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Daniel Alexiuc
Solution 2 sshah