简体   繁体   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?

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 方法?

I try to do like this but it's not working:我尝试这样做,但它不起作用:

In the controller:在控制器中:

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

and in the html file where I check authorize to show a message:并在我检查授权以显示消息的 html 文件中:

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

and it throws exception:它抛出异常:

org.thymeleaf.exceptions.TemplateProcessingException: An error happened trying to parse Spring Security access expression "hasRole(${role_Admin})" org.thymeleaf.exceptions.TemplateProcessingException:尝试解析 Spring Security 访问表达式“hasRole(${role_Admin})”时发生错误

I used:我用了:

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

how can I solve it?我该如何解决?

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

__${role_Admin}__

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

相关问题 model.addAttribute()参数 - model.addAttribute() parameters model.addAttribute() 对于每个循环 - model.addAttribute() For Each Loop spring mvc use model.addAttribute(nav)无法使用$ {nav.id}获取jsp中父类的属性 - spring mvc use model.addAttribute(nav) can not get the parent class’s attributes in jsp with ${nav.id} Model.addAttribute 不向 .jsp 传递任何东西 - Model.addAttribute not passing any thing to the .jsp Spring MVC:在Controller中使用model.addAttribute()将各种列表发送到Ajax方法 - Spring MVC : Using model.addAttribute() in Controller to send various Lists to Ajax method 在Spring MVC中-model.addAttribute用于动态数据以及如何将数据放置在jsp文件中 - In Spring MVC - model.addAttribute usage for dynamic data and how to place the data in jsp file 做“model.addAttribute()”和“session.setAttribute()”的区别 - difference in doing “model.addAttribute()” and “session.setAttribute()” 春天的@ModelAttribute,model.addAttribute有什么区别? - What is the difference between @ModelAttribute, model.addAttribute in spring? request.setAttribute()和model.addAttribute之间有什么区别? - what differencies between request.setAttribute() and model.addAttribute? 如何在百里香中的字符串中传递参数 - How do I pass a parameter in a string in thymeleaf
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM