首页 > 解决方案 > 如果用户具有特定角色,如何附加 CSS 类?

问题描述

th:classappend仅当用户具有特定角色时,如何将类附加到元素?

像这样的东西:

<div th:classappend="${hasRole('EDITOR')?'glow':''}"></div>

我正在使用 Spring Boot、Spring Security 和 Thymeleaf。

标签: spring-mvcspring-bootspring-securityauthorizationthymeleaf

解决方案


您可以使用request变量,它指向当前的HttpServletRequest

<div th:classappend="${#request.isUserInRole('EDITOR')?'glow':''}"></div>

推荐阅读