2012-02-20 41 views
2

我正在使用多选HTML表单输入来允许用户从所有可能的扩展列表中选择一个扩展集合。多选,集合和Spring 3 web MVC

扩展类是很简单 -

public class Extension { 

private String number; 
private String firstName; 
private String lastName; 

... getters and setters ... 

@Override 
public String toString() {  
    return new StringBuilder(number).append(" - ") 
       .append(firstName).append(" ").append(lastName) 
       .toString(); 
} 

} 

这里是我的表单对象 -

public class BusinessUnitForm { 

    private String name; 
    private Collection<Extension> extensions; 


    public Collection<Extension> getExtensions() { 
     return extensions; 
    } 

    public void setExtensions(Collection<Extension> extensions) { 
     this.extensions = extensions; 
    } 

    public String getName() { 
     return name; 
    } 

    public void setName(String name) { 
     this.name = name; 
    } 

} 

而且控制器 -

@Controller 
@RequestMapping("/businessunit") 
public class BusinessUnitController { 

    ... extension service & getters/setters ... 

    @RequestMapping(method = RequestMethod.GET) 
    public ModelAndView showForm(HttpServletRequest request, HttpServletResponse response) throws Exception { 

     Integer customerId = (Integer) request.getSession().getAttribute("customerId"); 
     ModelAndView mav = new ModelAndView("bu"); 

     // this is quite expensive... 
     Collection<Extension> allExtensions = extensionService.getAllExtensions(customerId); 

     BusinessUnitForm businessUnitForm = new BusinessUnitForm(); 

     mav.addObject("allExtensions", allExtensions); 
     mav.addObject("businessUnitForm", businessUnitForm); 

     return mav; 
    } 

    @RequestMapping(value="/create", method = RequestMethod.POST) 
    public ModelAndView create(HttpServletRequest request, HttpServletResponse response, BusinessUnitForm businessUnitForm, BindingResult result) throws Exception { 

     // *** BREAKPOINT HERE *** to examine businessUnitForm 

     Integer tenantId = (Integer) request.getSession().getAttribute("tenantId"); 

     // code to process submission 

     ModelAndView mav = new ModelAndView("bu"); 
     return mav; 
    } 
} 

最后的观点 -

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%> 
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> 

<html> 

... 

<form:form action="businessunit/create" method="POST" commandName="businessUnitForm" > 

<form:label path="name"></form:label><form:input path="name" /> 

<form:select path="extensions" cssClass="multiselect">      
    <form:options items="${allExtensions}" itemValue="number" /> 
</form:select> 

<input type="submit" value="Create" /> 

</form:form> 

... 

</html> 

在创建控制器方法中显示的断点处,businessUnitForm.extensions为null。 businessUnitForm.name被正确绑定。

我试过了,也许误导了,使businessUnitForm.extensions是一个LazyList,但这没有帮助。

如果我将BusinessUnitForm.extensions更改为未指定类型的Collection,则会使用包含所选值的LinkedHashSet成功填充字符串。

也许我在期待太多的Spring,但我希望能够使用select中的值以及allExtensions中的参考数据为businessUnitForm自动创建一个Collection<Extension>。我了解了CustomCollectionEditors的作用,但在印象中这可能不是Spring 3中需要的。

Spring 3可以在BusinessUnitForm上填充我的Collection<Extension>,而无需编写自定义集合编辑器?可能有某种诡计,或许?

在此先感谢...

回答

0

你需要实现一个自定义的转换器,能够将字符串从请求转换为Extension对象。

然后你必须使用具有泛型类型信息的Collection(或List或Set)。

查看此答案的一个转换器的例子:Submit Spring Form using Ajax where Entity has Foreign Entities

+0

谢谢你的回复拉尔夫。我意识到如何用PropertyEditor来做到这一点,但并没有真正关注新的转换内容。 – danw 2012-02-20 12:17:40