首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用thymeleaf的下拉列表默认值

使用thymeleaf的下拉列表默认值
EN

Stack Overflow用户
提问于 2022-06-30 08:55:12
回答 1查看 137关注 0票数 0

我有一个下拉列表,状态:有4个州(估计,预算,处理,完成)。每个唯一的ID都有可能有这4种状态值中的一种,我使用thymeleaf动态显示所有的状态值。

现在的要求是打开一个特定的ID,然后更新状态。BEfore这样做,它应该显示状态的当前值(来自db)。

我从我的控制器类传递所需的信息:

controller.java

代码语言:javascript
运行
复制
StatusTable st = new StatusTable();
List<String>statusValues = st.getAllStatus();
String status = st.getStatus(100); //Herre the status is processed
model.addAttribute("statusdef", statusval)
model.addAttribute("status", statusValues )
return "form.html";

form.html

代码语言:javascript
运行
复制
<html xmlns:th="https://www.thymeleaf.org"
    xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
    <body>
    <div th:fragment="editdata">
        <div class="container-md">
            <a style="font-weight: bold">Edit the Status</a>
            <div class="row justify-content-md-end">
                <form>
                    <div class="row">
                        <div class="col-2">
                            <a>Status</a>
                        </div>
                        <div class="col-3">
                            <select class="form-select" aria-label="Default select example"
                                name="status" th:selected ="${statusdef}"  required>
                                <option th:each="statusvalue : ${status}"
                                    th:text="${statusvalue}">
                            </select>
                          </div>
                         </div>
                        </form>
                        </div>
                         </div>




</body>

当我这样做时,我看到它没有显示默认值(已处理),它显示了我希望它显示“已处理”的第一个值(估计值)。有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-06-30 09:02:35

您使用的是th:seleceted在错误的位置,它必须带有选项标记,并且必须计算为true,如下所示-

代码语言:javascript
运行
复制
  <select class="form-select" aria-label="Default select example"
        name="status"  required>
       <option th:each="statusvalue : ${status}"
            th:selected ="${statusdef==statusvalue}"
            th:text="${statusvalue}">
       </option>
    </select>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72812960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档