Spring 3 MVC: Problem binding auf die Liste Formular-Felder senden

Lassen Sie mich Ihnen mein Problem durch die Bereitstellung einige der code in Frage stellen.

Zuerst meine form-Objekt:

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
}

Das Formular enthält eine einfache Liste von OrgChartFormElements

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

    //default constructor, getters and setters
}

Ich bin mit context:component-scan und mvc:annotation-driven, also mein controller sieht so aus:

@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
}

Das Problem mit der POST-handler. Ich begreife, dass es nicht viel jetzt, aber sobald ich es auf der Arbeit, werde ich beibehalten des vorgelegten Werte.

Im moment, die Ausgabe, die ich sehe, aus den beiden sysout Aussagen sind:

Found model attribute: true
[]

Hier ist meine JSP-Schnipsel:

<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>

Wenn ich die GET-Anforderung, die JSP rendert, und ich sehe meine Liste von text-input-Felder mit den erwarteten Werten, die mir sagt, dass ich mit dem spring-form-tags richtig. Allerdings, wenn ich behaupte, die form backing-Objekt deklariert, die als parameter (orgChartForm) in der POST-handler-Methode initialisiert wird, aber alles ist null/default initialisiert. Ich weiß nicht, wo die eingereichten Daten ging! Es scheint, dass springMVC, verliert es, und einfach errichtet ein neues Objekt.

Habe ich dieses Muster ausführlich in dieser Anwendung ohne Pannen. Es will einfach nicht funktionieren hier. Ich begreife, dass dies ein besonderer Fall in meiner Anwendung, wo das Formularfeld ist nicht atomar, sondern eine Liste, Aber es ist wirklich verwirrend mich, dass die Daten bindet in der GET-Anforderung, aber nicht auf die POST.

Vielen Dank im Voraus für jegliche Hinweise!

InformationsquelleAutor Kirit | 2010-09-08
Schreibe einen Kommentar