【问题标题】:Thymeleaf stopped resolving layout templates after migrating to Thymeleaf 3Thymeleaf 在迁移到 Thymeleaf 3 后停止解析布局模板
【发布时间】:2018-08-20 06:24:52
【问题描述】:

我在 1.5.7 版中有一个简单的 Spring Boot 应用程序,我正在尝试将其迁移到 2.0.0 版。我差不多完成了,但还有最后一块缺失,那就是 Thymeleaf。

在旧版本中一切正常,但在迁移之后,Spring Boot 停止解析任何模板(页面和电子邮件)。

我有所有模板src/main/resources/templates。另外,我在src/main/resources/templates/layout 中有一个名为 default 的布局,如下所示:

<!DOCTYPE html>
<html>
<head th:replace="fragments/header :: head"></head>
<body>

    <div id="page">
        <nav class="navigation" th:replace="fragments/navigation"></nav>

        <div class="container">
            <div layout:fragment="container"></div>
        </div>
    </div>
</body>
</html>

我的页面链接到此布局,如下所示:

<!DOCTYPE html>
<html lang="en"
  xmlns:th="http://www.thymeleaf.org"
  xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
  layout:decorate="layout/default">

<body>

    <div layout:fragment="container">
...
</body>
</html>

定位在src/main/resources/templeats/fragments中的导航片段

在 Spring Boot 2 中,登录页面被渲染,但没有应用布局(因此缺少导航栏和样式)。 2.0.0 版有什么变化吗?我在官方文档或迁移指南中没有找到任何解决方案。

编辑:正如答案中所建议的,它是由迁移到 Thymeleaf 3 引起的。我用一些反映迁移指南的更改更新了我的问题,但代码仍然没有注入布局。

我也试过 layout:data-layout-decorate=~{layout/default} 和 layout:data-layout-decorate=~{layout/default.html}

我通过手动添加百里香方言依赖项使其工作:

compile('nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect')

但我认为这应该是 Spring 中已经存在的传递依赖,我不应该手动添加它...

【问题讨论】:

    标签: java spring-boot thymeleaf


    【解决方案1】:

    谢谢 Smajl,添加

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

    也为我解决了这个问题。

    【讨论】:

      【解决方案2】:

      Spring Boot 2 不捆绑 Thymeleaf 2.0,而是捆绑 3.0。虽然整体语法保持不变,但在底层发生了许多变化。似乎您对 Thymeleaf 没有问题,但布局插件没有问题,因为您的登录页面呈现但只是布局未应用。 Thymeleaf 3.0 中的布局插件已从头开始重写

      Layout Dialect 2.0 迁移指南:https://ultraq.github.io/thymeleaf-layout-dialect/MigrationGuide.html Thymeleaf 3.0 迁移指南:https://www.thymeleaf.org/doc/articles/thymeleaf3migration.html

      【讨论】:

      • 我注意到layout:decorator 被重命名为layout:decorate 但肯定还有别的东西。我将尝试深入研究文档...
      • 我想知道这个问题是否已经解决了?我遇到了类似的问题。
      • 对我来说,解决方案是使用:thymeleaf.org" xmlns:layout="ultraq.net.nz/thymeleaf/layout" lang="en" layout:decorate="~{layouts /main}">
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 2019-02-20
      • 1970-01-01
      • 2016-12-21
      • 2020-03-14
      • 2021-11-05
      • 1970-01-01
      相关资源
      最近更新 更多