使用Thymeleaf时th:ref属性无法正常生成链接问题求助
问题原因
你使用了错误的Thymeleaf属性名,Thymeleaf不存在th:ref这个用于生成跳转链接的属性,你写的th:ref会被当作普通自定义属性处理,不会被解析为a标签的可跳转地址。
解决方法
- 将代码中所有的
th:ref替换为th:href即可。th:href是Thymeleaf专门用于动态生成a标签href属性的属性,会正确解析@{}格式的链接表达式。
注意:Thymeleaf的动态渲染属性规则为th: + HTML原生属性名,a标签的跳转地址原生属性为href,因此对应的动态属性就是th:href
修正后的单链接代码:
<td><a th:href="@{/inativar/{id}(id=${cliente.id})}">Inativar</a></td>
修正后的完整列表循环代码:
<tr th:each="cliente : ${clientes}"> <td th:text="${cliente.nome}"></td> <td th:text="${cliente.email}"></td> <td th:text="${cliente.tipo_utilizador}"></td> <td th:text="${cliente.data_criacao}"></td> <td><a th:href="@{/inativar/{id}(id=${cliente.id})}">Inativar</a></td> </tr> </tbody>
内容的提问来源于stack exchange,提问作者user3389425
相关产品推荐
相关产品推荐

