title: Spring MVC: binding listBox selected(multiple) values to Controller class method
body: <p>I have a spring with hibernate web application. In one of the page I have List Box which is populated with multiple subjects. In the same page I have one <code>textarea</code> and 4 <code>text-box</code>'s where I will enter the QuestionText <code>textarea</code> and options for the question in 4 text-box's. My intention is when I click on submit button I want to store the question and it's 4 options along with the selected Subjects into the database.</p>
  
  <p><strong>Question Model Class</strong>:</p>
  
  <pre><code>@Entity
  @Table(name="QUESTION")
  public class Question 
  {
    @Id
    @Column(name="questionId")
    @GeneratedValue(strategy=GenerationType.IDENTITY)
    private long questionId;
    @Column(length=1000)
    private String questionText;
    private String choiceOne;
    private String choiceTwo;
    private String choiceThree;
    private String choiceFour;
    private String choiceFive;
    private String answer;
    private String explanation;
    private boolean status;
    @ManyToMany(fetch = FetchType.LAZY , cascade = CascadeType.ALL)
    @JoinTable(name = "QSUBJECTTAG", joinColumns = { @JoinColumn (name = "questionId")}, inverseJoinColumns = { @JoinColumn(name = "subjectId")})
    private Set&lt;Subject&gt; subjects = new HashSet&lt;Subject&gt;();
  
  //setter and getter methods for the fields
  }
  </code></pre>
  
  <p><strong>View: question.jsp:</strong></p>
  
  <pre><code>&lt;c:url var="addAction" value="/question/add" &gt;&lt;/c:url&gt;
  
  &lt;form:form action="${addAction}" commandName="question"&gt;
  
  &lt;table border="1"&gt;
  &lt;tr&gt;
      &lt;c:if test="${!empty listSubjects}"&gt;
          &lt;td colspan="2"&gt;
              &lt;form:label path="subjects"&gt;
                  &lt;spring:message text="Subjects List"/&gt;
              &lt;/form:label&gt;
          &lt;/td&gt;
          &lt;td colspan="4"&gt;
          &lt;form:select path="subjects" size="3" &gt;
              &lt;form:options items="${listSubjects}" itemValue="subjectId"  itemLabel="subjectDesc"/&gt;
          &lt;/form:select&gt;
          &lt;/td&gt;
      &lt;/c:if&gt;
      &lt;c:if test="${empty listSubjects}"&gt;
          &lt;td&gt;
              &lt;form:label path="questionId"&gt;
                  &lt;spring:message text="Question ID"/&gt;
              &lt;/form:label&gt;
          &lt;/td&gt;
          &lt;td&gt;
          &lt;form:select path="subjects"&gt;
              &lt;form:option value="NONE"&gt;--Select Subject--&lt;/form:option&gt;
          &lt;/form:select&gt;
          &lt;/td&gt;
      &lt;/c:if&gt;
  
    &lt;/tr&gt;
    &lt;c:if test="${!empty question.questionText}"&gt;
    &lt;tr&gt;
      &lt;td&gt;
          &lt;form:label path="questionId"&gt;
              &lt;spring:message text="Question ID"/&gt;
          &lt;/form:label&gt;
      &lt;/td&gt;
      &lt;td&gt;
          &lt;form:input path="questionId" readonly="true" size="8"  disabled="true" /&gt;
          &lt;form:hidden path="questionId" /&gt;
      &lt;/td&gt; 
    &lt;/tr&gt;
    &lt;/c:if&gt;
    &lt;tr rowspan="4"&gt;
      &lt;td&gt;
          &lt;form:label path="questionText"&gt;
              &lt;spring:message text="Question"/&gt;
          &lt;/form:label&gt;
      &lt;/td&gt;
      &lt;td colspan="6"&gt;
          &lt;form:textarea path="questionText" size="1000" rows="20" cols="50"/&gt;
      &lt;/td&gt;
  
    &lt;/tr&gt;
    &lt;tr&gt;  
      &lt;td&gt;
          &lt;form:label path="choiceOne"&gt;
              &lt;spring:message text="Choice One"/&gt;
          &lt;/form:label&gt;
      &lt;/td&gt;
      &lt;td&gt;
          &lt;form:textarea path="choiceOne" size="200"/&gt;
      &lt;/td&gt;
      &lt;td&gt;
          &lt;form:label path="choiceTwo"&gt;
              &lt;spring:message text="Choice Two"/&gt;
          &lt;/form:label&gt;
      &lt;/td&gt;
      &lt;td&gt;
          &lt;form:textarea path="choiceTwo" size="200"/&gt;
      &lt;/td&gt;
      &lt;td&gt;
          &lt;form:label path="choiceThree"&gt;
              &lt;spring:message text="Choice Three"/&gt;
          &lt;/form:label&gt;
      &lt;/td&gt;
      &lt;td&gt;
          &lt;form:textarea path="choiceThree" size="200"/&gt;
      &lt;/td&gt;
      &lt;td&gt;
          &lt;form:label path="choiceFour"&gt;
              &lt;spring:message text="Choice Four"/&gt;
          &lt;/form:label&gt;
      &lt;/td&gt;
      &lt;td&gt;
          &lt;form:textarea path="choiceFour" size="200"/&gt;
      &lt;/td&gt;
  &lt;/tr&gt; 
  &lt;tr&gt;
      &lt;td&gt;
          &lt;form:label path="answer"&gt;
              &lt;spring:message text="Select Answer"/&gt;
          &lt;/form:label&gt;
      &lt;/td&gt;
      &lt;td&gt;
          &lt;form:select path="answer"&gt;
              &lt;form:option value="NONE"&gt;--select the anser--&lt;/form:option&gt;
              &lt;form:option value="CHOICE_ONE"&gt;Choice One&lt;/form:option&gt;
              &lt;form:option value="CHOICE_TWO"&gt;Choice TWO&lt;/form:option&gt;
              &lt;form:option value="CHOICE_THREE"&gt;Choice Three&lt;/form:option&gt;
              &lt;form:option value="CHOICE_FOUR"&gt;Choice Four&lt;/form:option&gt;
          &lt;/form:select&gt;
      &lt;/td&gt;
   &lt;/tr&gt;
   &lt;tr&gt;
      &lt;td colspan="2"&gt;
          &lt;c:if test="${!empty question.answer}"&gt;
              &lt;input type="submit"
                  value="&lt;spring:message text="Edit Question"/&gt;" /&gt;
          &lt;/c:if&gt;
          &lt;c:if test="${empty question.answer}"&gt;
              &lt;input type="submit"
                  value="&lt;spring:message text="Add Question"/&gt;" /&gt;
          &lt;/c:if&gt;
      &lt;/td&gt;
   &lt;/tr&gt; 
   &lt;/table&gt;   
  &lt;/form:form&gt;
  </code></pre>
  
  <p><strong>Controller:QuestionController.java</strong></p>
  
  <pre><code>@Controller
  public class QuestionController {
  
  
  private QuestionService questionService;
  private SubjectService subjectService;
  
  @Autowired(required=true)
  @Qualifier(value="questionService")
  public void setQuestionService(QuestionService questionService)
  {
      this.questionService = questionService;
  }
  @Autowired(required=true)
  @Qualifier(value="subjectService")
  public void setSubjectService(SubjectService subjectService)
  {
      this.subjectService = subjectService;
  }
  
  @RequestMapping(value="/questions", method = RequestMethod.GET)
  public String listQuestions(Model model)
  {
      model.addAttribute("question",new Question());
      model.addAttribute("listSubjects", this.subjectService.listSubjects());
      return "questions";
  }
  
  //For add and update subject both
  @RequestMapping(value= "/question/add", method = RequestMethod.POST)
  public String addQuestion(@ModelAttribute("question") Question question){
  
      if(question.getQuestionId() == 0){
          //new Subject, add it
          this.questionService.addQuestion(question);
      }else{
          //existing subject, call update
          this.questionService.updateQuestion(question);
      }
  
      return "questions";
  
  }
  }
  </code></pre>
  
  <p><strong>Issue</strong>: I have inserted some Subjects into the <code>Subject</code> table and the view page is getting populated with a <code>ListBox</code> with the inserted subjects. Now When i select Multiple values from the ListBox and enter some data into the textarea and text-box's Then the selected Subjects are not getting inserted into the "subjects" fields of "Question" class. How to capture the selected Options and put them into my <code>Question</code> model Class field which is <code>Set&lt;Subject&gt; subjects = new HashSet&lt;Subject&gt;();</code> ?
  When I click on add Question Submit button in my view page I am getting <code>404 error with message "The request sent by the client was syntactically incorrect ()"</code></p>