首页 > 解决方案 > Springboot 2.0.2 无法使用 thymeleaf 模板,抛出 404 错误

问题描述

我将 Springboot 1.5.7 用于我的 rest api 应用程序,并且我正在使用 thymeleaf 模板从我的 api 发送电子邮件。但是,当我将 Spring Boot 的版本更新到 2.0.2 时,它会抛出 404 错误,即“解析模板时出错”,模板可能不存在,或者任何配置的模板解析器都可能无法访问”。

下面是我在 application.yml 中的配置

spring:
  thymeleaf:
    cache: false
    enabled: true
    mode: HTML5
    prefix: /templates/
    suffix: .html   

pom.xml 中的百里香叶版本

 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
      <version>2.0.2.RELEASE</version>
    </dependency>

下面是我正在使用的模板结构,

在此处输入图像描述

我的应用程序发布已经很接近了,我严重地陷入了这个问题,如果有人可以为我提供解决方法,那将是一个很大的帮助。

标签: springspring-bootthymeleaf

解决方案


prefix: /templates/从中删除application.yml

如果仍然不起作用,请添加thymeleaf-layout-dialect依赖项(请参阅:Thymeleaf 在迁移到 Thymeleaf 3 后停止解析布局模板

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

<dependency>
    <groupId>nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>

仅供参考,我把我的观点放在了WEB-INF/webapp/views,所以我正在使用prefix: /WEB-INF/webapp/views/(spring boot war deploy to tomcat)


推荐阅读