gpt4 book ai didi

java - 多选、集合和 Spring 3 web MVC

转载 作者:行者123 更新时间:2023-11-29 09:16:03 25 4
gpt4 key购买 nike

我正在使用多选 HTML 表单输入,以允许用户从所有可能的扩展列表中选择一组扩展。

扩展类非常简单——

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 -

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

最后是 View -

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

在上面显示的创建 Controller 方法的断点处,businessUnitForm.extensions 为空。 businessUnitForm.name 绑定(bind)正确。

我已经尝试过(也许是误入歧途)将 businessUnitForm.extensions 设为 LazyList,但这没有帮助。

如果我改变 BusinessUnitForm.extensions成为Collection未指定类型,它被成功填充为包含所选值的字符串的 LinkedHashSet。

也许我对 Spring 的期望太高了,但我希望它能够使用 select 中的值以及 allExtensions 中的引用数据自动创建一个 Collection<Extension>对我来说,在 businessUnitForm 上。我了解 CustomCollectionEditors 的作用,但我的印象是 Spring 3 可能不需要这样做。

Spring 3 可以填充我的 Collection<Extension>在 BusinessUnitForm 上没有我编写自定义集合编辑器?也许是 View 的某种技巧?

提前致谢...

最佳答案

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

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

有关转换器的示例,请参阅此答案:Submit Spring Form using Ajax where Entity has Foreign Entities

关于java - 多选、集合和 Spring 3 web MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9360291/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com