Users with different roles need to have different navbars at the top of the webpage. My topnavs are th:fragments, written in separate file, and that part of code works fine. But when I use th:switch and th:replace, all topnavs are shown in the webpage instead of just one.
I was looking these questions for solutions, but with no help:
Thymleaf switch statement with multiple case
How to use thymeleaf conditions - if - elseif - else
Things I tried:
<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>
In debugger I can see in Java code that field role of type String has value role2. Instead of just second navbar, it shows both navbars. It must be that I missed something in Thymeleaf syntax?