为了账号安全,请及时绑定邮箱和手机立即绑定

是否可以使用 th-field 来检索 Thymeleaf 中列表的值?

是否可以使用 th-field 来检索 Thymeleaf 中列表的值?

叮当猫咪 2022-10-26 17:00:36
我在 thymeleaf 中创建一个页面,在其中从控制器检索列表以安装表,在此表中有一个选择字段,我在其中检索值列表但我无法显示已经存在的值检索的数据库可以帮助我吗?请!我已经尝试使用以下命令但无法恢复该值:th:field="*{valoresAtributo[__${stat.index}__].valorUsuario}"我收到以下错误: Bean 名称的 BindingResult 和普通目标对象都不能用作请求属性。<table class="table table-sm table-striped table-bordered"  id="dataTable">    <thead>                     <tr><th colspan="4">Valores dos Atributos</th></tr>                 </thead>    <tbody>         <tr class="text-black">            <td rowspan="2"> Atributo</td>            <td rowspan="2"> Local</td>            <td>Aplicação</td>                          <td>Usuário</td>        </tr>        <tr>            <td id="vlAppl"></td>                           <td id="vlUser"></td>        </tr>        <tr th:each="valorAtributo, stat : ${valoresAtributo}">            <td th:text="${valoresAtributo[__${stat.index}__].nomeAtributo}"></td>            <td th:text="${valoresAtributo[__${stat.index}__].valorLocal}"></td>            <td th:text="${valoresAtributo[__${stat.index}__].valorAplicaco}"></td>            <td>             <select class="form-control col-md-10" th:field="*{valoresAtributo[__${stat.index}__].valorUsuario}">                    <option th:each="option : ${T(com.jequiti.JequitiIntegrador.controller.AtributoController).test(valorAtributo.sqlValidacao)}"                                                th:value="${{option.valorAtributo}}"                                                th:text="${option.significadoAtributo}">                                        </option>                </select>             </td>            </tr>        </tbody>    </table>我希望该字段显示当前在数据库中的值和值列表中的选项。谢谢你们!
查看完整描述

2 回答

?
缥缈止盈

TA贡献2041条经验 获得超4个赞

只有在父标签th:field中使用时才能使用。(从您发布的 HTML 中并不清楚 - 但您可能不是因为您直接添加为模型属性。)th:object<form />valoresAtributo


如果您希望显示预选<option>但不使用th:object,th:field您应该使用应该评估或基于是否应该选择该选项的th:selected属性。它应该看起来像这样:truefalse


<select class="form-control col-md-10">

    <option

        th:each="option : ${T(com.jequiti.JequitiIntegrador.controller.AtributoController).test(valorAtributo.sqlValidacao)}"

        th:value="${{option.valorAtributo}}"

        th:text="${option.significadoAtributo}"

        th:selected="${valorAtributo.valorUsuario == option.valorAtributo}" />

</select>


查看完整回答
反对 回复 2022-10-26
?
慕无忌1623718

TA贡献1744条经验 获得超4个赞

我使用百里香检索列表的方法:


例如,带有进入 html 页面users的实体列表的列表:User


@GetMapping("/parsing/explorer")

public ModelAndView parsing(){

    ModelAndView modelAndView = new ModelAndView("show");

    modelAndView.addObject("users", generateUsersList());

    return modelAndView;

}

show.html例子:


<table class="w3-table-all w3-card-4">

        <tr>

            <th>Id</th>

            <th>Name</th>

            <th>Last name</th>

        </tr>

        <tr th:each="user : ${users}"> 

            <td th:text="${user.getId()}"></td>

            <td th:text="${user.getName()}"></td>

            <td th:text="${user.getLastName()}"></td>

        </tr>

    </table>

所以User( users) 的列表被分配给user字符串中的变量<tr th: every = "user: $ {users}">


在下一段代码中,我们也可以像java使用 getter 一样调用“用户”字段,但 Thymeleaf 还允许您引用这些字段:user.id/ user.name....


确定select块中的变量:


    <select name="userFromHtml" id=userId required>

        <option th:each="user: ${users}">

            <title th:text="${user.getLastName()} + ' ' + ${user.getName()} + ' ' + ${user.getIdIO}" th:value="${user.getId()}"/>

        </option>

    </select>

在这种情况下有必要th:value在title块中确定:th:value="${user.getId()}"。因此,在控制器中传递了带有所选用户userFromHtml值的变量。id


PS 虽然 Thymeleaf 允许您直接定义变量,但我更喜欢使用 getter 来获取数据。


查看完整回答
反对 回复 2022-10-26
  • 2 回答
  • 0 关注
  • 112 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信