SpringBoot集成Thymeleaf采坑

在尝试集成Thymeleaf时,使用@Controller而不是@RestController,因为需要渲染页面。当遇到404错误,通过将日志级别设置为debug来排查问题。最终发现可能是Thymeleaf版本导致的问题,升级到3.0.11.RELEASE和thymeleaf-layout-dialect版本2.1.1解决了问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot集成 Thymeleaf采坑

按照网上的方式

  1. 编写 @Controller层 (注意这里不能用@RestController),因为要渲染页面,restController是以JSON格式返回

  2. 集成 Thymeleaf

    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-thymeleaf</artifactId>
            </dependency>
    
  3. 编写 html 页面

启动后发现一直报404

排查方法:

  1. 修改日志级别,看debug日志

    logging.level.root = debug
    
  2. 最后看到网上说可能是thymeleaf版本问题,添加

    <properties>
            <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
            <thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
        </properties>
    

    成功!

注:

1、控制层不能使用RestController, 要使用 Controller

2、日志级别设置

3、问题描述清楚

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值