【发布时间】:2021-09-14 19:20:51
【问题描述】:
不同角色的用户需要在网页顶部有不同的导航栏。我的 topnavs 是 th:fragments,写在单独的文件中,这部分代码工作正常。 但是当我使用 th:switch 和 th:replace 时,所有的 topnav 都会显示在网页中,而不仅仅是一个。
我一直在寻找这些问题的解决方案,但没有任何帮助:
Thymleaf switch statement with multiple case
How to use thymeleaf conditions - if - elseif - else
我尝试过的事情:
<span th:if = "${role.value} == 'role1' " th:replace = "fragments/topnav :: navbar_role1"></span>
<span th:if = "${role.value} == 'role2' " th:replace = "fragments/topnav :: navbar_role2"></span>
#2
<span th:if = "${role} == 'role1' " th:replace = "fragments/topnav :: navbar_role1"></span>
<span th:if = "${role} == 'role2' " th:replace = "fragments/topnav :: navbar_role2"></span>
#3
<span th:if = "${role} eq 'role1' " th:replace = "fragments/topnav :: navbar_role1"></span>
<span th:if = "${role} eq 'role2' " th:replace = "fragments/topnav :: navbar_role2"></span
#4
<th:block th:switch = "${role}">
<div th:case = "${role} eq 'role1' " th:replace = "fragments/topnav :: navbar_role1"></div>
<div th:case = "${role} eq 'role2' " th:replace = "fragments/topnav :: navbar_role2"></div>
</th:block>
在调试器中,我可以在 Java 代码中看到 String 类型的字段角色具有值 role2。它不仅显示了第二个导航栏,还显示了两个导航栏。 一定是我错过了 Thymeleaf 语法中的某些内容?
【问题讨论】:
标签: thymeleaf