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

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


我希望该字段显示当前在数据库中的值和值列表中的选项。


谢谢你们!


叮当猫咪
浏览 115回答 2
2回答

缥缈止盈

只有在父标签th:field中使用时才能使用。(从您发布的 HTML 中并不清楚 - 但您可能不是因为您直接添加为模型属性。)th:object<form />valoresAtributo如果您希望显示预选<option>但不使用th:object,th:field您应该使用应该评估或基于是否应该选择该选项的th:selected属性。它应该看起来像这样:truefalse<select class="form-control col-md-10">&nbsp; &nbsp; <option&nbsp; &nbsp; &nbsp; &nbsp; th:each="option : ${T(com.jequiti.JequitiIntegrador.controller.AtributoController).test(valorAtributo.sqlValidacao)}"&nbsp; &nbsp; &nbsp; &nbsp; th:value="${{option.valorAtributo}}"&nbsp; &nbsp; &nbsp; &nbsp; th:text="${option.significadoAtributo}"&nbsp; &nbsp; &nbsp; &nbsp; th:selected="${valorAtributo.valorUsuario == option.valorAtributo}" /></select>

慕无忌1623718

我使用百里香检索列表的方法:例如,带有进入 html 页面users的实体列表的列表:User@GetMapping("/parsing/explorer")public ModelAndView parsing(){&nbsp; &nbsp; ModelAndView modelAndView = new ModelAndView("show");&nbsp; &nbsp; modelAndView.addObject("users", generateUsersList());&nbsp; &nbsp; return modelAndView;}show.html例子:<table class="w3-table-all w3-card-4">&nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Id</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Name</th>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <th>Last name</th>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; &nbsp; &nbsp; <tr th:each="user : ${users}">&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td th:text="${user.getId()}"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td th:text="${user.getName()}"></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td th:text="${user.getLastName()}"></td>&nbsp; &nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; </table>所以User( users) 的列表被分配给user字符串中的变量<tr th: every = "user: $ {users}">在下一段代码中,我们也可以像java使用 getter 一样调用“用户”字段,但 Thymeleaf 还允许您引用这些字段:user.id/ user.name....确定select块中的变量:&nbsp; &nbsp; <select name="userFromHtml" id=userId required>&nbsp; &nbsp; &nbsp; &nbsp; <option th:each="user: ${users}">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <title th:text="${user.getLastName()} + ' ' + ${user.getName()} + ' ' + ${user.getIdIO}" th:value="${user.getId()}"/>&nbsp; &nbsp; &nbsp; &nbsp; </option>&nbsp; &nbsp; </select>在这种情况下有必要th:value在title块中确定:th:value="${user.getId()}"。因此,在控制器中传递了带有所选用户userFromHtml值的变量。idPS 虽然 Thymeleaf 允许您直接定义变量,但我更喜欢使用 getter 来获取数据。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java