title: Thymeleaf + Spring MVC - How to persist values from a list or enum on a form?
body: <p>I have a page that displays a list of sql connections. One of the fields in that table is the databaseType, which is defined in the enum:</p>
  
  <pre><code>public enum SqlDatabaseType {
      NONE("None"),
      MySQL("MySql"),
      SQLSERVER("SQLServer");
  </code></pre>
  
  <p>For creating and editing the object, I populate the select box with this:</p>
  
  <pre><code>&lt;label th:for="databaseType"&gt;SQL Database Type:&lt;/label&gt;
  &lt;select&gt;
      &lt;option th:each="databaseType : ${T(b.c.m.h.m.SqlDatabaseType).values()}"
          th:value="${databaseType}"
          th:field="*{databaseType}"
          th:text="${databaseType.databaseType}"&gt;
      &lt;/option&gt;
  &lt;/select&gt;
  </code></pre>
  
  <p>The DTO object that populates that form is:</p>
  
  <pre><code>public class SqlPojo {
      private String id;
      private String description;
      private SqlDatabaseType databaseType;
      private String url;
      private String port;
      private String database;
      private String username;
      private String password;
      // getter and setters
  </code></pre>
  
  <p>The problem is that all String fields are persisted, but not the complex type one.</p>
  
  <p>The table that lists all created objects is defined as:</p>
  
  <pre><code>&lt;table class="table table-bordered table-striped"&gt;
      &lt;thead&gt;
          &lt;tr&gt;
              &lt;td style="text-align: center"&gt;description&lt;/td&gt;
              &lt;td style="text-align: center"&gt;databaseType&lt;/td&gt;
              &lt;td style="text-align: center"&gt;url&lt;/td&gt;
              &lt;td style="text-align: center"&gt;port&lt;/td&gt;
              &lt;td style="text-align: center"&gt;database&lt;/td&gt;
              &lt;td style="text-align: center"&gt;username&lt;/td&gt;
              &lt;td style="text-align: center"&gt;actions&lt;/td&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;
      &lt;tbody&gt;
          &lt;tr th:each="pojo : ${pojoList}"&gt;
              &lt;td style="text-align: center" th:text="${pojo.description}"/&gt;
              &lt;td style="text-align: center" th:text="${pojo.databaseType}"/&gt;
              &lt;td style="text-align: center" th:text="${pojo.url}"/&gt;
              &lt;td style="text-align: center" th:text="${pojo.port}"/&gt;
              &lt;td style="text-align: center" th:text="${pojo.database}"/&gt;
              &lt;td style="text-align: center" th:text="${pojo.username}"/&gt;
  
              &lt;td style="text-align: center" &gt;
                  &lt;a th:href="@{/sql/edit(id=${pojo.id})}"&gt;
                      &lt;img width="20px" height="20px" alt="edit" th:src="@{/assets/img/edit.png}" /&gt;
                  &lt;/a&gt;
                  &lt;a th:href="@{/sql/remove(id=${pojo.id})}"&gt;
                      &lt;img width="20px" height="20px" alt="remove" th:src="@{/assets/img/delete.png}" /&gt;
                  &lt;/a&gt;
              &lt;/td&gt;
          &lt;/tr&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
  </code></pre>
  
  <p>And the rendered out is:</p>
  
  <p><a href="https://i.stack.imgur.com/dCsQV.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/dCsQV.png" alt="enter image description here"></a></p>
  
  <p>Before starting using Thymeleaf, I was using JSP, the table definition was very similar and I got the <code>databaseType</code> value with no problems.</p>
  
  <p>Switching to Thymeleaf presented some nice visuals, but also a few problems, like this one, for enum and lists (to be mentioned in another question). What's the best solution in this case? Defining a converter? If so, how?</p>