【问题标题】:Springboot 2.0.2 not working with thymeleaf template, throwing 404 errorSpringboot 2.0.2 不使用 thymeleaf 模板,抛出 404 错误
【发布时间】:2018-11-06 09:16:45
【问题描述】:

我将 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>

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

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

【问题讨论】:

  • 您的问题解决了吗?如果解决了,您应该接受答案。

标签: spring spring-boot thymeleaf


【解决方案1】:

application.yml 中删除prefix: /templates/

如果还是不行,添加thymeleaf-layout-dialect依赖(见:Thymeleaf stopped resolving layout templates after migrating to 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)

【讨论】:

    【解决方案2】:

    如 2.0 迁移指南中所述。

    Thymeleaf 启动器包含 thymeleaf-layout-dialect 依赖项 之前。由于 Thymeleaf 3.0 现在提供native way to implement layouts, 我们删除了该强制依赖项,并将此选择权留给您。 如果您的应用程序依赖于 layout-dialect 项目,请 将其显式添加为依赖项。

    添加以下依赖项应该可以工作

    <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>
    

    来源:https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#template-engines

    【讨论】:

    • 不添加 layout-dialect 的依赖会导致解析布局模板出现问题,而不是 404。你的答案与第一个有什么不同(thymeleaf-layout-dialect 依赖也在那里提到)
    猜你喜欢
    • 2021-11-05
    • 1970-01-01
    • 2018-10-10
    • 2023-02-22
    • 1970-01-01
    • 1970-01-01
    • 2015-06-01
    • 1970-01-01
    • 2020-11-27
    相关资源
    最近更新 更多