Thymeleaf + Spring表格-如何从控制器中持久加载选择框值以查看? [英] Thymeleaf + Spring form - How load persisted select box values from controller to view?

查看:167
本文介绍了Thymeleaf + Spring表格-如何从控制器中持久加载选择框值以查看?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在此

In this question regarding persisting select box elements I managed to work my way around and persisting the selected entity on the select menu by passing it's id to the controller and passing it to my business layer and persisting as I intended.

第一个清单是一个枚举.这样效果很好,并且在编辑实体时会加载选定的值.

The first listing is for an enum. This works nicely and the selected value gets loaded when editing the entity.

<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>

问题在于定义为以下内容的列表对象:

The problem is with the list objects that are defined as:

<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>

现在这个问题恰恰相反.当我单击实体的编辑链接时,选择框被设置为选择框的默认实体值,并且在提交后,默认实体的ID被传递到控制器上并被保留.

This question now is rather the opposite. When I click the entity's edit link, the select box are set to default entity value for the select box and, upon submitting, the default entity's id is passed onto the controller and it gets persisted.

如何确保列表中选定的值是从持久实体正确加载的值?

How to make sure the selected value on the list is the one correctly loaded from the persisted entity?

推荐答案

<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:selected="${ftpList.contains(ftpConnection)}"                                
            th:text="${ftpConnection.getDescription()}">
        </option>
    </select>
</div>

这篇关于Thymeleaf + Spring表格-如何从控制器中持久加载选择框值以查看?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆