2013-10-02 42 views
2

我在這個主題上看到了一些相關的問題,但沒有一個看起來與我之後的完全一致。從彈簧窗體複選框元素接收EnumSet?

我有一個表單,我希望用戶能夠從複選框列表(由enum支持)中選擇多個項目,並將其作爲Set來接收。我有以下的(使用天爲例)

我的枚舉:

public enum Day { 
MONDAY, 
TUESDAY, 
WEDNESDAY, 
THURSDAY, 
FRIDAY, 
SATURDAY, 
SUNDAY 
} 

發送枚舉值頁面控制器顯示爲選項:

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> 

表單對象bac王形式:

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)

任何想法是什麼問題或有更好的方法來實現這一目標? 感謝

回答

2

我設法改變我的形式來解決這對使用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; 
    } 
} 
0

你只需要使用接口而不是實現。

你仍然可以使用EnumSet,但聲明需要使用接口(無論如何這是一個很好的習慣)。

換句話說,使用Abby's answer提供的代碼。然後,你可以做類似的東西:

filterForm.setDays(EnumSet.allOf(Day.class));