title: Spring Boot - how to retrieve selected item on drop down?
body: <p>I'm facing the following problem in the User edit screen:</p>
  
  <p>Imagine the following classes User and Country,
  and I have a table for each one and the relationship is one to many.</p>
  
  <p>That way in the session of edit screen we have in which country the user is registered and display a
  drop down to update this information,
  but I'd like to bring selected the current user's country and other countries, how can I do?</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="form-group"&gt;
   &lt;label class="col-sm-2 control-label" for="country"&gt;Country&lt;/label&gt;
  
   &lt;div class="col-sm-6 col-xs-12"&gt;
    &lt;select id="country" th:field="*{user.name}" class="form-control"
      required="required"&gt;
     &lt;option value=""&gt;Select...&lt;/option&gt;
     &lt;option th:each="c : ${countries}" th:value="${c.id}"
       th:text="${c.name}" value=""&gt;&lt;/option&gt;
    &lt;/select&gt;
   &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I'm using Thymeleaf and Springboot.</p>
  
  <hr>
  
  <h3>Solved</h3>
  
  <p>For the code to work, just change this way:</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="form-group"&gt;
   &lt;label class="col-sm-2 control-label" for="country"&gt;Country&lt;/label&gt;
  
   &lt;div class="col-sm-6 col-xs-12"&gt;
    &lt;select id="country" name="user.country" class="form-control"
      required="required"&gt;
     &lt;option value=""&gt;Select...&lt;/option&gt;
     &lt;option th:each="c : ${countries}" th:value="${c.id}"
       th:text="${c.name}" th:selected="(${c.id} == *{user.country.id})"&gt;&lt;/option&gt;
    &lt;/select&gt;
   &lt;/div&gt;
  &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>
  
  <p>I found the solution in this <a href="https://stackoverflow.com/questions/32206849/spring-mvc-thymeleaf-thselected-not-working">link</a>:</p>
  
  <p>"according to the thymeleaf forum (<a href="http://forum.thymeleaf.org/th-selected-not-working-on-multiple-select-tp4025883p4025884.html" rel="nofollow noreferrer">post</a>) th:field doesn't work with th:selected"</p>