Thymeleaf + Spring MVC - 如何从表单中的列表或枚举中持久化值? [英] Thymeleaf + Spring MVC - How to persist values from a list or enum on a form?
问题描述
public enum SqlDatabaseType {
NONE(无),
MySQL(MySql),
SQLSERVER(SQLServer);
对于创建和编辑对象,我填充选择框:
< label th:for =databaseType> SQL数据库类型:< / label>
< select>
< option th:each =databaseType:$ {T(bcmhmSqlDatabaseType).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数据库;
private String username;
private String password;
// getter和setter
问题是所有String字段都是持久化的,但不是复杂类型一。
列出所有创建对象的表定义为:
< 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>数据库< / 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:centerth:text =$ {pojo.description}/>
< td style =text-align:centerth:text =$ {pojo.databaseType}/>
< td style =text-align:centerth:text =$ {pojo.url}/>
< td style =text-align:centerth:text =$ {pojo.port}/>
< td style =text-align:centerth:text =$ {pojo.database}/>
< td style =text-align:centerth:text =$ {pojo.username}/>
< td style =text-align:center>
< a th:href =@ {/ sql / edit(id = $ {pojo.id})}>
< img width =20pxheight =20pxalt =editth:src =@ {/ assets / img / edit.png}/>
< / a>
< a th:href =@ {/ sql / remove(id = $ {pojo.id})}>
< img width =20pxheight =20pxalt =removeth:src =@ {/ assets / img / delete.png}/>
< / a>
< / td>
< / tr>
< / tbody>
< / table>
呈现的是:
在开始使用Thymeleaf之前,我使用的是JSP,表的定义非常相似,我得到了 databaseType
值没有问题。
切换到Thymeleaf提供了一些漂亮的视觉效果,但也有一些问题,像这样,枚举和列表(在另一个问题中提及)。这种情况下最好的解决方案是什么?定义转换器?如果是这样,那么如何?
对于枚举,我通过做以下工作:
< div class =form-group>
< label th:for =databaseType> SQL数据库类型:< / label>
< select class =form-controlth:field =* {{databaseType}}>
< option th:each =databaseType:$ {T(bcmhmSqlDatabaseType).values()}
th:value =$ {{databaseType}}
th:selected =$ {databaseType == T(bcmhmSqlDatabaseType)}
th:text =$ {databaseType.databaseType}>
< / option>
< / select>
< / div>
对于列表,解决方案有所不同:
< div class =form-group>
< label th:for =ftpConnection> FTP连接:< / label>
< select class =form-controlname =ftpId>
< option th:each =ftpConnection:$ {ftpList}
th:value =$ {ftpConnection.getId()}
th:text =$ {ftpConnection。 getDescription()}>
< / option>
< / select>
< / div>
希望它帮助某人!
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:
public enum SqlDatabaseType {
NONE("None"),
MySQL("MySql"),
SQLSERVER("SQLServer");
For creating and editing the object, I populate the select box with this:
<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>
The DTO object that populates that form is:
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
The problem is that all String fields are persisted, but not the complex type one.
The table that lists all created objects is defined as:
<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>
And the rendered out is:
Before starting using Thymeleaf, I was using JSP, the table definition was very similar and I got the databaseType
value with no problems.
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?
For the enum, I got it working by doing:
<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>
For the list, solution was a bit different:
<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>
Hope it helps someone!
这篇关于Thymeleaf + Spring MVC - 如何从表单中的列表或枚举中持久化值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!