是否可以使用th字段在Thymeleaf中检索列表的值?

时间:2019-04-29 17:58:35

标签: java spring-boot thymeleaf spring-el

我正在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>
@RequestMapping(value="/seguranca/atributo/valores", params = {"atributo","siteOpt","applOpt","userOpt","aplicacao","usuario"}, method = RequestMethod.GET)
    public String initAttributeValuesFormFilter(@RequestParam("atributo") Long idAtributo, @RequestParam("siteOpt") Integer idNivel1, @RequestParam("applOpt") Integer idNivel2, @RequestParam("userOpt") Integer idNivel3, @RequestParam("aplicacao") String aplicacao, @RequestParam("usuario") String usuario, Model model)
    {
        Integer userId = 0;

        if(!Strings.isNullOrEmpty(usuario))
            userId = userServiceImpl.findIdUsuarioByFantasia(usuario).intValue();

        List<ValoresAtributoView> valoresAtributo = buscaValoresAtributos(idAtributo, idNivel1, idNivel2, idNivel3, 0, userId);

        model.addAttribute("opUsuarios", userServiceImpl.findAllActiveUsers());
        model.addAttribute("opAtributos", atributoService.findAll());
        model.addAttribute("valoresAtributo",valoresAtributo);

        return "/valoresAtributo";
    }

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

谢谢大家!

2 个答案:

答案 0 :(得分:1)

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

如果您希望显示预选的<option>但不使用th:objectth: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>

答案 1 :(得分:0)

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

例如列表usersUser实体列表进入html页面:

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

因此Userusers)的列表已分配给字符串user中的变量<tr th: every = "user: $ {users}">

在下一段代码中,我们也可以使用吸气剂以与java相同的方式调用“用户”字段,但是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:valuetitle中的th:value="${user.getId()}"所必需的。因此,在控制器中传递了具有选定用户值userFromHtml的变量id

PS尽管Thymeleaf允许您直接定义变量,但我更喜欢使用getter进行数据获取。