我正在使用多選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>
,而無需編寫自定義集合編輯器?可能有某種詭計,或許?
在此先感謝...
丹
謝謝你的回覆拉爾夫。我意識到如何用PropertyEditor來做到這一點,但並沒有真正關注新的轉換內容。 – danw 2012-02-20 12:17:40