迁移到 Thymeleaf 3 后,Thymeleaf 停止解析布局模板

Sma*_*ajl 5 java thymeleaf spring-boot

我有一个简单的 Spring Boot 应用程序,版本为 1.5.7,我正在尝试将其迁移到版本 2.0.0。我快完成了,但还缺少最后一块,那就是百里香叶。

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

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

<!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>
Run Code Online (Sandbox Code Playgroud)

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

<!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>
Run Code Online (Sandbox Code Playgroud)

导航片段位于src/main/resources/templeats/fragments

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

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

layout:data-layout-decorate=~{layout/default}也尝试过布局:data-layout-decorate=~{layout/default.html}

我通过手动添加 thymeleaf 方言依赖项来使其工作:

compile('nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect')
Run Code Online (Sandbox Code Playgroud)

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

Joc*_*lle 5

谢谢 Smajl,添加

<dependency>
    <groupId> nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

也为我解决了这个问题。


Dar*_*ice 1

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

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