Spring Boot Thymeleaf 3 获取请求的 Uri 路径
Spring Boot Thymeleaf About 3,069 words需求
由于Thymeleaf3跟随Spring Boot3进行了版本更新,Java扩展包的命名空间由JavaEE改成了JakartaEE。
Thymeleaf3也相应的去除了原先的${#request}对象。
解决方法
现在有几种解决方法:
Controller中注入ModelAndAttribute。- 使用
${#ctx}上下文对象。
推荐 #ctx
Thymeleaf3中的ctx对象,它实际是org.thymeleaf.context.WebEngineContext对象,包含了Spring运行的很多Bean对象、Component对象。
<div th:text="${#ctx.getClass()}"></div>
代码
备注:Thymeleaf 3.1后无法使用,下文更新了解决方法。
ctx中保存了一个springRequestContext对象,该对象是org.springframework.web.servlet.support.RequestContext,并提供了一个getRequestUri()方法。
备注:IDEA中可能没有提示,而且即使启用了Thymeleaf插件,也会有黄色波浪线的告警Cannot resolve 'springRequestContext',不用管IDEA(强迫症可以禁用检查)。
<div th:text="${#ctx.springRequestContext.getRequestUri()}"></div>
ctx 保存的对象
<div th:each="varName : ${#ctx.getVariableNames()}">
<div th:text="${varName}"></div>
</div>
输出:springRequestContext/springMacroRequestContext/thymeleafRequestContext都是同一个对象(RequestContext)。
org.springframework.web.context.request.async.WebAsyncManager.WEB_ASYNC_MANAGER
org.springframework.web.servlet.HandlerMapping.bestMatchingHandler
org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.FILTERED
org.springframework.web.servlet.DispatcherServlet.CONTEXT
thymeleaf::EvaluationContext
org.springframework.web.servlet.resource.ResourceUrlProvider
org.springframework.web.filter.ServerHttpObservationFilter.context
characterEncodingFilter.FILTERED
class org.springframework.security.web.ObservationFilterChainDecorator.observation
org.springframework.web.util.ServletRequestPathUtils.PATH
org.springframework.web.filter.ServerHttpObservationFilter.observation
org.springframework.security.web.access.intercept.AuthorizationFilter@4fa6fb7f.APPLIED
org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER
formContentFilter.FILTERED
org.springframework.security.web.debug.DebugFilter.FILTERED
org.springframework.web.servlet.HandlerMapping.bestMatchingPattern
requestContextFilter.FILTERED
org.springframework.web.servlet.DispatcherServlet.OUTPUT_FLASH_MAP
serverHttpObservationFilter.FILTERED
org.springframework.web.servlet.DispatcherServlet.FLASH_MAP_MANAGER
org.springframework.core.convert.ConversionService
org.springframework.security.web.header.HeaderWriterFilter@3e63bd6b.FILTERED
org.springframework.web.servlet.View.selectedContentType
org.springframework.web.servlet.HandlerMapping.matrixVariables
org.springframework.security.web.FilterChainProxy.APPLIED
thymeleafRequestContext
org.springframework.web.servlet.DispatcherServlet.THEME_SOURCE
springMacroRequestContext
sessionIdentityFilter.FILTERED
org.springframework.security.web.session.DisableEncodeUrlFilter.FILTERED
org.springframework.web.servlet.HandlerMapping.pathWithinHandlerMapping
org.springframework.web.servlet.HandlerMapping.uriTemplateVariables
springRequestContext
org.springframework.web.servlet.DispatcherServlet.THEME_RESOLVER
org.springframework.security.web.context.SecurityContextHolderFilter.APPLIED
更新
Thymeleaf 3.1之后禁止了org.springframework包下的方法调用。
新方法
<div th:text="${#ctx.getExchange().getRequest().getRequestURI()}"></div>
————        END        ————
Give me a Star, Thanks:)
https://github.com/fendoudebb/LiteNote扫描下方二维码关注公众号和小程序↓↓↓