有几个组织单位,每个单位都有一个自定义角色。在该页上,授权后,您需要显示仅用于此角色(部门)的按钮。此代码适用于:
<div>
<a th:sec:authorize="hasRole('ROLE_DEP-1')" href="/userPage/department1" type="button">DEP-1</a>
<a th:sec:authorize="hasRole('ROLE_DEP-2')" href="/userPage/department2" type="button">DEP-2</a>
<a th:sec:authorize="hasRole('ROLE_DEP-3')" href="/userPage/department3" type="button">DEP-3</a>
<a th:sec:authorize="hasRole('ROLE_DEP-4')" href="/userPage/department4" type="button">DEP-4</a>
</div>不过,我认为用人手书写所有细分单位是不正确的,因为将来可能会增加更多的单位。我决定通过th完成:每个Thymeleaf循环:
<div th:each="d : ${departments}">
<a th:sec:authorize="hasRole('+${d.role.name}+')" href="/userPage/department" type="button" th:text="${d.shortName}"></a>
</div>但不幸的是,这是行不通的,按钮没有显示。正确显示角色名称。部门和角色表之间的关系是1:1,我不明白为什么它不能工作。在这个函数中可能需要有一个特殊的语法:
"hasRole('+${d.role.name}+')“
发布于 2021-05-22 17:03:07
您将需要使用预处理
<div th:each="d : ${departments}">
<a sec:authorize="hasRole(' + __${d.role.name}__ +')" href="/userPage/department" type="button" th:text="${d.shortName}"></a>
</div>https://stackoverflow.com/questions/67650751
复制相似问题