簡體   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