使用的是 Spring Boot 3.3.1版本
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.3.1</version>
</parent>
Thymeleaf版本与Spring Boot 版本可能不兼容问题导致获取内置对象时显示无法解析“#xxx”
运行服务器时会显示错误信息
The 'request','session','servletContext' and 'response' expression utility objects are no longer available by default for template expressions and their use is not recommended. In cases where they are really needed, they should be manually added as context variables.
(默认情况下,'request'、'session'、'servletContext' 和 'response' 表达式实用程序对象不再可用于模板表达式,因此不建议使用它们。在确实需要它们的情况下,应将它们手动添加为上下文变量。
)
目前还没有解决 解决后再更新上来
---------------------------------------------------------------------------------------------------------------------------------
已解决!!!
将Spring Boot 的版本修改为2.2.4.RELEASE
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent>
如果也有引入lombok依赖 也需要将lombok版本修改为最新版本
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.34</version>
</dependency>
Thymeleaf即可访问内置对象
Controller
@GetMapping("/servlet")
public String servlet(HttpServletRequest request){
request.setAttribute("value","request");
request.getSession().setAttribute("value","session");
request.getServletContext().setAttribute("value","servletContext");
return "test";
}
HTML
<p th:text="${#request.getAttribute('value')}"></p>
<p th:text="${#session.getAttribute('value')}"></p>
<p th:text="${#servletContext.getAttribute('value')}"></p>
<p th:text="${#response}"></p>