2016-04-10 18 views
2

我有一个显示sql连接列表的页面。一个在表中的字段是数据库类型,这是在枚举定义:Thymeleaf + Spring MVC - 如何在列表中持久化列表中的值或枚举?

public enum SqlDatabaseType { 
    NONE("None"), 
    MySQL("MySql"), 
    SQLSERVER("SQLServer"); 

用于创建和编辑的对象,我填充选择框与此:

<label th:for="databaseType">SQL Database Type:</label> 
<select> 
    <option th:each="databaseType : ${T(b.c.m.h.m.SqlDatabaseType).values()}" 
     th:value="${databaseType}" 
     th:field="*{databaseType}" 
     th:text="${databaseType.databaseType}"> 
    </option> 
</select> 

的DTO对象填充该表单是:

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 

问题是所有的字符串字段是持久的,但不是复杂的类型之一。

,列出了所有创建的对象的表定义为:

<table class="table table-bordered table-striped"> 
    <thead> 
     <tr> 
      <td style="text-align: center">description</td> 
      <td style="text-align: center">databaseType</td> 
      <td style="text-align: center">url</td> 
      <td style="text-align: center">port</td> 
      <td style="text-align: center">database</td> 
      <td style="text-align: center">username</td> 
      <td style="text-align: center">actions</td> 
     </tr> 
    </thead> 
    <tbody> 
     <tr th:each="pojo : ${pojoList}"> 
      <td style="text-align: center" th:text="${pojo.description}"/> 
      <td style="text-align: center" th:text="${pojo.databaseType}"/> 
      <td style="text-align: center" th:text="${pojo.url}"/> 
      <td style="text-align: center" th:text="${pojo.port}"/> 
      <td style="text-align: center" th:text="${pojo.database}"/> 
      <td style="text-align: center" th:text="${pojo.username}"/> 

      <td style="text-align: center" > 
       <a th:href="@{/sql/edit(id=${pojo.id})}"> 
        <img width="20px" height="20px" alt="edit" th:src="@{/assets/img/edit.png}" /> 
       </a> 
       <a th:href="@{/sql/remove(id=${pojo.id})}"> 
        <img width="20px" height="20px" alt="remove" th:src="@{/assets/img/delete.png}" /> 
       </a> 
      </td> 
     </tr> 
    </tbody> 
</table> 

而呈现出来的是:

enter image description here

使用Thymeleaf在开始之前,我用JSP,表定义是非常相似的,我得到了没有问题的价值。

切换到Thymeleaf提供了一些很好的视觉效果,但也有一些问题,比如这个,枚举和列表(在另一个问题中提到)。这种情况下最好的解决方案是什么?定义一个转换器?如果是这样,怎么样?

回答

8

对于枚举,我把它做的工作:

<div class="form-group"> 
    <label th:for="databaseType">SQL Database Type:</label> 
     <select class="form-control" th:field="*{{databaseType}}"> 
      <option th:each="databaseType : ${T(b.c.m.h.m.SqlDatabaseType).values()}" 
        th:value="${{databaseType}}" 
        th:selected="${databaseType == T(b.c.m.h.m.SqlDatabaseType)}" 
        th:text="${databaseType.databaseType}"> 
     </option> 
    </select> 
</div> 

对于列表,解决方案是一个有点不同:

<div class="form-group"> 
    <label th:for="ftpConnection">FTP Connection:</label> 
    <select class="form-control" name="ftpId" > 
     <option th:each="ftpConnection : ${ftpList}" 
       th:value="${ftpConnection.getId()}" 
       th:text="${ftpConnection.getDescription()}"> 
     </option> 
    </select> 
</div> 

希望它可以帮助别人!

+0

你能解释为什么双括号中的字段? {{databaseType}}? $ {T和b.c.m.h.m的含义是什么意思是类名SqlDatabaseType的yr包名是否正确? – user641887