2016-04-10 23 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