我看到了一些与此主题相关的问题,但似乎没有一个与我所追求的完全匹配。
我有一个表单,我希望用户能够从复选框列表中选择多个项目(由枚举支持),并将其作为集合接收。我有以下内容(以天为例)
我的枚举:
public enum Day {
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY,
SUNDAY
}
将枚举值发送到 Controller 中的页面以显示为选项:
model.addAttribute("allDays", Day.values());
将选项写入复选框并映射到正确的表单字段:
<form:form method="get" modelAttribute="filterForm" commandName="filterForm">
<c:forEach items="${allDays}" var="item">
<form:checkbox path="days" value="${item.name()}" label="${item.name()}"/>
</c:forEach>
</form:form>
支持表单的表单对象:
public class FilterForm {
private EnumSet<Day> days;
public EnumSet<Day> getDays() {
return days;
}
public void setDays(EnumSet<Day> days) {
this.days = days;
}
}
这可以正确显示选项,但是当我尝试提交时,出现错误:
org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 1 errors Field error in object 'filterForm' on field 'days': rejected value [0,1]; codes [typeMismatch.filterForm.days,typeMismatch.days,typeMismatch.java.util.EnumSet,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [filterForm.days,days]; arguments []; default message [days]]; default message [Failed to convert property value of type 'java.lang.String[]' to required type 'java.util.EnumSet' for property 'days'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type java.lang.String[] to type java.util.EnumSet for value '{0, 1}'; nested exception is java.lang.IllegalArgumentException: Could not instantiate Collection type: java.util.EnumSet] org.springframework.web.method.annotation.ModelAttributeMethodProcessor.resolveArgument(ModelAttributeMethodProcessor.java:111) org.springframework.web.method.support.HandlerMethodArgumentResolverComposite.resolveArgument(HandlerMethodArgumentResolverComposite.java:75) org.springframework.web.method.support.InvocableHandlerMethod.getMethodArgumentValues(InvocableHandlerMethod.java:156) org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:117) org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:96) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:617) org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:578) org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:80) org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:923) org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:778) javax.servlet.http.HttpServlet.service(HttpServlet.java:621) javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
知道问题是什么或者是否有更好的方法来实现这一目标? 谢谢
最佳答案
我设法通过将表单更改为使用 Set 而不是 EnumSet 来解决此问题,并且我还修改了标记,以便提交的值保持选中状态:
<form:form method="get" modelAttribute="filterForm" commandName="filterForm">
<form:checkboxes items="${allDays}" path="days" />
</form:form>
和表格:
public class FilterForm {
private Set<Day> days;
public Set<Day> getDays() {
return days;
}
public void setDays(Set<Day> days) {
this.days = days;
}
}
关于java - 从 Spring 表单复选框元素接收 EnumSet?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19134620/