繁体   English   中英

如何使用 Model.addAttribute() 添加的变量作为参数传递给 thymeleaf 的 hasRole 方法?

[英]How can I use the variables that added by Model.addAttribute() to pass as a parameter in hasRole method of thymeleaf?

如何使用 Model.addAttribute() 添加的变量作为参数传递给 thymeleaf 的 hasRole 方法?

我尝试这样做,但它不起作用:

在控制器中:

String role_Admin = userTags.role.ADMIN.name();
model.addAttribute("role_Admin", role_Admin);

并在我检查授权以显示消息的 html 文件中:

<span sec:authorize="${hasRole(${role_Admin})}">admin</span>

它抛出异常:

org.thymeleaf.exceptions.TemplateProcessingException:尝试解析 Spring Security 访问表达式“hasRole(${role_Admin})”时发生错误

我用了:

  • 弹簧启动版本 2.5.2
  • spring-boot-starter-security
  • spring-boot-starter-thymeleaf
  • thymeleaf-extras-springsecurity5

我该如何解决?

用双下划线包裹 ${role_Admin}。

__${role_Admin}__

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM