SpringBoot切换版本时项目访问不到thymeleaf页面404

在将IDEA项目的SpringBoot版本从2.1.16降级到1.5.10后,遇到Thymeleaf页面无法正常访问的问题,报404错误。经检查,问题源于HTML文件解析错误。解决方案包括:1) 添加结束标签;2) 在pom.xml的properties中指定Thymeleaf和thymeleaf-layout-dialect的版本;3) 添加spring-boot-starter-thymeleaf及nekohtml依赖,并在application.properties中设置spring.thymeleaf.mode为LEGACYHTML5。推荐使用第三种方法。注意,第二种方法可能导致SpringSecurity标签失效。

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

由IDEA选择的版本2.1.16切换成1.5.10版本时,发现在项目正常启动时跳转themeleaf页面访问不到,报404。

image

查看控制报错日志:

image

发现是html文件解析错误
SpringBoot中thymeleaf的默认版本为:1.5.10.RELEAS

<dependency>  
  <groupId>org.springframework.boot</groupId>  
  <artifactId>spring-boot-starter-validation</artifactId>  
  <version>1.5.10.RELEASE</version>  
</dependency>

第一种办法就是加上结束标签。

第二种办法就是在pom.xml中的 properties 中指明Thymeleaf标签的版本号即可。

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <!--添加如下两行代码指定Thymeleaf标签的版本-->
        <thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
        <thymeleaf-layout-dialect.version>2.0.5</thymeleaf-layout-dialect.version>
    </properties>

第三种办法是在pom.xml中添加依赖。

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>net.sourceforge.nekohtml</groupId>
    <artifactId>nekohtml</artifactId>
    <version>1.9.22</version>
</dependency>

application.properties中:

spring.thymeleaf.mode=LEGACYHTML5

推荐使用方法三。

注意:第二种办法可能在springsecurity中的一些标签不起作用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值