title: dropdown lists with Thymeleaf and SpringBoot
body: <p>I have a Spring Boot 1.3 app that uses Thymeleaf on the HTML.  I have  page of users and when I edit them, I would like the user institution selected from a list.  I have the window showing everything properly, but when I select the institution, the controller does not show the selected value.</p>
  
  <p>Here is my HTML:</p>
  
  <pre><code> &lt;div class="form-group"&gt;
       &lt;label class="col-md-3 control-label"&gt;Institution&lt;/label&gt;
           &lt;div class="col-md-5"&gt;
           &lt;div th:if="${institutionList != null and not #lists.isEmpty(institutionList)}"&gt;
               &lt;select&gt;
                   &lt;option th:each="dropDownItem : ${institutionList}"
                                   th:value="${dropDownItem.name}"
                                   th:text="${dropDownItem.name}" /&gt;
               &lt;/select&gt;
          &lt;/div&gt;
          &lt;div th:if="${institutionList == null or lists.isEmpty(institutionList)}"&gt;
              &lt;div&gt;"No Institutions were found, please create some first"&lt;/div&gt;
          &lt;/div&gt;
      &lt;/div&gt;
   &lt;/div&gt;
  </code></pre>
  
  <p>And here is my controller</p>
  
  <pre><code>@Controller
  @PreAuthorize("hasRole('Admin')")
  @RequestMapping("/admin")
  public class AdminController {
      @Transactional
      @PreAuthorize("hasRole('ADMIN')")
      @RequestMapping(value = "/addUser", method = RequestMethod.POST)
      public String checkPersonInfo(@ModelAttribute User user, Model model, Authentication authentication) {
          // user does not have the selected institution set
          customUserDetailsService.createNewUser(user);
          updateModelWithAllUsers(model);
          return "admin";
      }
  
      private void updateModelWithAllUsers(Model model) {
          model.addAttribute(USERLIST, customUserDetailsService.findAllUsers());
          model.addAttribute(INSTITUTION_LIST, institutionService.findAll());
          model.addAttribute("user", new User());
      }
  
  ...
  }
  </code></pre>
  
  <p>Here is my User:</p>
  
  <pre><code>@Entity
  @Table(name = "Users")
  public class User implements UserDetails {
   @Id
      private String username;
  
      @Column(nullable = false)
      private String password;
  
      private boolean enabled;
      private boolean accountNonExpired;
      private boolean accountNonLocked;
      private boolean credentialsNonExpired;
      private String companyName;
      private String email;
  
      @ElementCollection(fetch=FetchType.EAGER)
      @CollectionTable(name="Authorities", joinColumns=@JoinColumn(name="username"))
      @Column(name="authority")
      private Set&lt;String&gt; roles = new HashSet&lt;String&gt;();
  
      @ManyToOne(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
      @JoinColumn(name = "institutionId", nullable = false)
      private Institution institution;
  ... getters &amp; setters
  }
  </code></pre>
  
  <p>And my Institution:</p>
  
  <pre><code>@Entity
  @Table(name = "Institution")
  public class Institution {
  
      @Id
      @GeneratedValue(strategy = GenerationType.IDENTITY)
      private Long institutionId;
      private String name;
  
      @OneToMany(mappedBy = "institution", fetch = FetchType.EAGER)
      @Fetch(value = FetchMode.SUBSELECT)
      List&lt;User&gt; users = new ArrayList&lt;User&gt;();
      ... getters &amp; setters
  }
  </code></pre>
  
  <p>When the "/addUser" gets the User object from the UI, the institution is null.</p>
  
  <p>EDIT 
  I used Sergios suggestion to use  <code>select th:field="${user.institution}"</code>  as well as <code>select th:field="*{institution}"</code></p>
  
  <p>but that gives me an error:</p>
  
  <blockquote>
    <p>org.springframework.validation.BeanPropertyBindingResult: 1 errors
    Field error in object 'user' on field 'institution': rejected value
    [com.security.Institution@3e3945d2]; codes
    [typeMismatch.user.institution,typeMismatch.institution,typeMismatch.com.
    .security.Institution,typeMismatch]; arguments
    [org.springframework.context.support.DefaultMessageSourceResolvable:
    codes [user.institution,institution]; arguments []; default message
    [institution]]; default message [Failed to convert property value of
    type 'java.lang.String' to required type 'com.security.Institution'
    for property 'institution'; nested exception is
    org.springframework.core.convert.ConversionFailedException: Failed to
    convert from type java.lang.String to type java.lang.Long for value
    'com.security.Institution@3e3945d2'; nested exception is
    java.lang.NumberFormatException: For input string: "com.
    .security.Institution@3e3945d2"]</p>
  </blockquote>
  
  <p>Not sure if I am reading this correctly, but does that mean Thymeleaf is trying to pass the Institution.name to the user.institution field?</p>
  
  <p>Can anyone offer any advice on how to do this?</p>