zl程序教程

您现在的位置是:首页 >  其他

当前栏目

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

2023-04-18 12:52:14 时间

由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中的一些标签不起作用。