2010-09-08 132 views
0

让我通过提供一些有问题的代码来介绍我的问题。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-scanmvc: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上。

在此先感谢您的指点!

回答

0

我认为问题在于您试图将任意数量的表单字段绑定到ArrayList,它是一个具有预定大小的列表。

Spring有一个称为AutoPopulatingList的东西,它是为此目的而定制设计的。请看看这个链接了解如何使用它的更多信息:http://blog.richardadamdean.com/?p=12

0

我想你需要为你的类写PropertyEditorSupport。以下是供您参考的例子。

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(); 
} 

}

在控制器,你就用@InitBinder如下应绑定:

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

希望这将解决您的问题。