title: How do I get the selected value from a drop-down menu with Thymeleaf?
body: <p>My user is adding an exam object which is then added to the subject object. Subject and exam have a one to many relationship.
  The user is selecting the subject in the drop down menu.This menu contains string not actual subject objects.
  In this form, how do I send an exam object and the selected item (String) to the controller?</p>
  
  <p>My HTML file</p>
  
  <pre><code>            &lt;form action="#" th:action="@{/addExam}" th:object="${exam}" 
       method="post"&gt;
               &lt;div th:object="${subject}"&gt;
      &lt;select th:field="*{option}" class="form-control" id="subjectOrder" 
  name= "subjectOrder"&gt;
      &lt;option value=""&gt;Select subject&lt;/option&gt;
  
  &lt;option 
      th:each="Subject : ${subjects}" 
      th:value="${Subject}" 
      th:text="${Subject}"&gt;&lt;/option&gt;
   &lt;/div&gt;
   &lt;div&gt;
      &lt;table&gt;
          &lt;tr&gt;
                Exam Title
              &lt;td&gt;&lt;input type="text" th:field="*{examTitle}" /&gt;&lt;/td&gt;
  
          &lt;/tr&gt;  
          &lt;tr&gt;
              &lt;td&gt; Exam grade worth &lt;/td&gt;
              &lt;td&gt;&lt;input th:field="*{examGradeWorth}" /&gt;&lt;/td&gt;
  
              &lt;/tr&gt;  
              &lt;tr&gt;
                  &lt;td&gt;&lt;button type="submit"&gt;Submit post&lt;/button&gt;&lt;/td&gt;
                  &lt;/tr&gt;
        &lt;/table&gt;
      &lt;/div&gt;
      &lt;/form&gt;
  </code></pre>
  
  <p>The controller, I want to set subject Name to equal the subject the user selected in the drop down box.</p>
  
  <pre><code>    @GetMapping("/addexam")
  public String showExamForm(Model model) {
  
      Authentication loggedInUser = 
    SecurityContextHolder.getContext().getAuthentication();
      String email = loggedInUser.getName();   
  
      User user = userRepository.findByEmailAddress(email);
  
      ArrayList&lt;String&gt; subjects = new ArrayList&lt;String&gt;();
  
      for(Subject sub:user.getSubject())
      {
          subjects.add(sub.getSubjectName());
      }
      model.addAttribute("subjects", subjects);
  
  return "addExam";
  }
  
  @PostMapping("/addexam")
  public String addNewExam(@ModelAttribute("exam") @Valid @RequestBody Exam 
      exam,UserRegistrationDto userDto, BindingResult result, Model model) {
  
      examRepository.save(exam);
      model.addAttribute("examTitle", exam.getExamTitle());
      model.addAttribute("examGradeWorth", exam.getExamGradeWorth());
      String subjectName = (); 
  
   //I want to set subjectName to equal the selected option.
  
      Subject subject = subjectRepository.findBySubjectName(subjectName);
      subject.addExam(exam);
      subjectRepository.save(subject);
  
  
  return "userProfile1";
  
  
  }
  </code></pre>