title: Multiple select, Collections and Spring 3 web MVC
body: <p>I'm using a multiple select HTML form input to allow a user to pick a collection of Extensions from a list of all possible Extensions.</p>
  
  <p>The Extension class is quite simple - </p>
  
  <pre><code>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();
   }  
  
  }
  </code></pre>
  
  <p>Here's my form object - </p>
  
  <pre><code>public class BusinessUnitForm {
  
      private String name;
      private Collection&lt;Extension&gt; extensions;
  
  
      public Collection&lt;Extension&gt; getExtensions() {
          return extensions;
      }
  
      public void setExtensions(Collection&lt;Extension&gt; extensions) {
          this.extensions = extensions;
      }
  
      public String getName() {
          return name;
      }
  
      public void setName(String name) {
          this.name = name;
      }
  
  }
  </code></pre>
  
  <p>And the controller - </p>
  
  <pre><code>@Controller
  @RequestMapping("/businessunit")
  public class BusinessUnitController {
  
      ... extension service &amp; 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&lt;Extension&gt; 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;
      }
  }
  </code></pre>
  
  <p>And finally the view - </p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%&gt;
  &lt;%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%&gt;
  &lt;%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%&gt;
  &lt;%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %&gt;
  
  &lt;html&gt;
  
  ...
  
  &lt;form:form action="businessunit/create" method="POST" commandName="businessUnitForm" &gt;
  
   &lt;form:label path="name"&gt;&lt;/form:label&gt;&lt;form:input path="name" /&gt;
  
   &lt;form:select path="extensions" cssClass="multiselect"&gt;                      
    &lt;form:options items="${allExtensions}" itemValue="number" /&gt;
   &lt;/form:select&gt;
  
   &lt;input type="submit" value="Create" /&gt;
  
  &lt;/form:form&gt;
  
  ...
  
  &lt;/html&gt;
  </code></pre>
  
  <p>At the breakpoint shown above in the create controller method, businessUnitForm.extensions is null.  businessUnitForm.name is bound correctly.  </p>
  
  <p>I've tried, perhaps misguidedly, making businessUnitForm.extensions a LazyList but this doesn't help.</p>
  
  <p>If I change <code>BusinessUnitForm.extensions</code> to be a <code>Collection</code> of unspecified type, it is populated successfully with a LinkedHashSet of Strings, containing the values selected.</p>
  
  <p>Maybe I'm expecting too much of Spring, but I was hoping it would be able to use the values from the select, and also the reference data in allExtensions to automagically create a <code>Collection&lt;Extension&gt;</code> for me on the businessUnitForm.  I understand the role of CustomCollectionEditors, but was under the impression that this might not be required in Spring 3.  </p>
  
  <p>Can Spring 3 populate my <code>Collection&lt;Extension&gt;</code> on the BusinessUnitForm without me writing a custom collection editor ?  Some kind of trick with the view, perhaps ?</p>
  
  <p>Thanks in advance ...</p>
  
  <p>Dan</p>