Ich habe eine Seite, die eine Liste von SQL-Verbindungen anzeigt. Eines der Felder in dieser Tabelle die database ist, die in der Enum definiert:Thymoleaf + Spring MVC - Wie behalte ich Werte aus einer Liste oder enum auf einem Formular?
public enum SqlDatabaseType {
NONE("None"),
MySQL("MySql"),
SQLSERVER("SQLServer");
Zur Erstellung und Bearbeitung des Objekts, fülle ich die Auswahlbox mit diesem:
<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>
Das DTO-Objekt dass auffüllt, dass Form ist:
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
das Problem ist, dass alle String Felder beibehalten, aber nicht die komplexen Typ ein.
Die Tabelle, die alle Objekte erstellten Listen ist definiert als:
<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>
Und das gerenderte aus ist:
Bevor Thymeleaf mit Start war ich mit JSP, die Tabellendefinition war sehr ähnlich und ich habe den databaseType
Wert ohne Probleme.
Die Umstellung auf Thymeleaf bot einige schöne Visuals, aber auch ein paar Probleme, wie dieses, für enum und Listen (um in einer anderen Frage erwähnt zu werden). Was ist die beste Lösung in diesem Fall? Einen Konverter definieren? Wenn das so ist, wie?
können Sie erklären, warum doppelte Klammern im th: Feld ?? {{databaseType}}? und was ist die Bedeutung von $ {T und b.c.m.h.m ist Ihr Paketname für die Klasse SqlDatabaseType korrekt? – user641887