Thymeleaf获取内置对象显示无法解析“#xxx”

使用的是 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>

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值