Note that there are some explanatory texts on larger screens.

plurals
  1. POReceive an EnumSet from a spring form checkbox element?
    primarykey
    data
    text
    <p>I've seen a few related questions on this topic but none that seem to exactly match what I'm after.</p> <p>I have a form where I'd like the user to be able to select a number of items from a checkbox list (backed by an enum), and to receive that as a Set. I have the following (using days as an example)</p> <p>My enum:</p> <pre><code>public enum Day { MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY } </code></pre> <p>Sending the enum values to the page in the controller to be displayed as the options:</p> <pre><code>model.addAttribute("allDays", Day.values()); </code></pre> <p>Writing the options as checkboxes and mapping to correct form field:</p> <pre><code>&lt;form:form method="get" modelAttribute="filterForm" commandName="filterForm"&gt; &lt;c:forEach items="${allDays}" var="item"&gt; &lt;form:checkbox path="days" value="${item.name()}" label="${item.name()}"/&gt; &lt;/c:forEach&gt; &lt;/form:form&gt; </code></pre> <p>The form object backing the form:</p> <pre><code>public class FilterForm { private EnumSet&lt;Day&gt; days; public EnumSet&lt;Day&gt; getDays() { return days; } public void setDays(EnumSet&lt;Day&gt; days) { this.days = days; } } </code></pre> <p>This works as far as showing the options correctly, but when I try to submit, I get an error:</p> <blockquote> <p>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)</p> </blockquote> <p>Any idea what the problem is or if there is a better way to achieve this? Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload