让我通过提供一些有问题的代码来介绍我的问题。Spring 3 MVC:提交绑定到列表表单字段提交
首先我表单对象:
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
}
表单包含的OrgChartFormElement
小号
public class OrgChartFormElement {
private boolean selected;
private String elementLabel;
private Long id;
//default constructor, getters and setters
}
一个简单的列表,我使用context:component-scan
和mvc:annotation-driven
,所以我的控制器看起来像:
@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
}
问题在于POST处理程序。我意识到现在没有太多的工作,但一旦我开始工作,我会坚持提交的值。
目前,输出我从两个系统输出语句看到的是:
Found model attribute: true
[]
这里是我的JSP代码片段:
<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>
当我做的GET请求,JSP呈现,我看到了我的文本输入字段列表,其中包含期望值,这告诉我即时使用spring-form标签。但是,当我提交时,在POST处理程序方法中声明为参数(orgChartForm)的表单支持对象被初始化,但是一切都是空/默认初始化。我不知道提交的数据去了哪里! SpringMVC似乎没有它,只是简单地构造一个新的对象。
我在这个应用程序中广泛使用了这种模式,没有一个小故障。它只是不会在这里工作。我意识到这是我的应用程序中的一种特殊情况,其中表单字段不是原子的,而是一个列表,但它使我很困惑,数据绑定在GET请求中,而不是在POST上。
在此先感谢您的指点!