Spring Boot 3.1.4中Thymeleaf获取requestUri的正确方式
解决Spring Boot 3.1.4 + Thymeleaf中无法获取requestUri的问题
针对你遇到的RequestContext的requestUri访问被禁止的问题,当前环境下有两种可靠的获取方式:
方式一:使用Thymeleaf内置
#request对象
这是最简洁直接的写法,Thymeleaf提供了内置的#request对象直接对应当前的HttpServletRequest:th:action="${#request.requestURI}"方式二:通过Spring RequestContext间接获取
如果需要保留从#ctx.springRequestContext的路径访问,需要先获取内部的request实例再访问requestURI属性:th:action="${#ctx.springRequestContext.request.requestURI}"
原因说明
你遇到的报错是因为Thymeleaf的表达式安全规则更新,禁止直接访问RequestContext类的requestUri成员属性,必须通过其内部持有的HttpServletRequest实例来获取请求路径信息,这是框架为了提升安全性、规范属性访问路径做出的调整。
内容的提问来源于stack exchange,提问作者Jasper
相关产品推荐
相关产品推荐

