升级到 Spring Boot 2.2.2.RELEASE + Thymeleaf 3.0.11.RELEASE,thymeleaf/layout 停止工作

Val*_*jon 3 java spring thymeleaf spring-boot

将 Spring-Boot 从 迁移1.5.3.RELEASE到 后2.2.2.RELEASE,Thymeleaf 停止工作。

pom.xml

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.2.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>

...

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
    <groupId>org.thymeleaf.extras</groupId>
    <artifactId>thymeleaf-extras-springsecurity5</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)

我已经按照Thymeleaf 3 迁移指南+ 阅读Spring-Boot 版本,但没有成功......

模板片段(Thymeleaf 3.0 layout:decorate

<!DOCTYPE html>
<html th:lang="${#locale}" xmlns="http://www.w3.org/1999/xhtml"
    xmlns:th="https://www.thymeleaf.org"
    xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity4"
    xmlns:layout="https://www.ultraq.net.nz/thymeleaf/layout"
    layout:decorate="layout">
<head>
<title></title>
</head>
<body>
    <div layout:fragment="content">
Run Code Online (Sandbox Code Playgroud)

http://localhost/index 路径比较:

代码片段(1.5.3.RELEASE)

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

<head>

    <title>APLLICATION</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <!--[if IE]><meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1'/><![endif]-->
    <meta name="viewport" content="width=device-width,initial-scale=1,maximum-scale=1" />
    <meta name="description" content="Application description" />
    <meta name="fragment" content="text" />

    <link rel="stylesheet" href="css/vendor/bootstrap.min.css" />
    <link rel="stylesheet" href="css/vendor/jquery-ui.min.css" />
    ...
Run Code Online (Sandbox Code Playgroud)

代码片段(2.2.2.RELEASE)

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:layout="https://www.ultraq.net.nz/thymeleaf/layout"
    layout:decorate="layout">
<head>
<title></title>
</head>
<body>
    <div layout:fragment="content">
...
Run Code Online (Sandbox Code Playgroud)

任何帮助表示赞赏。谢谢

编辑:

@Marged,模型设置:

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/index").setViewName("index");
        ...
    }

}
Run Code Online (Sandbox Code Playgroud)

上下文路径默认为 ( /)

@SpringBootApplication(exclude = { MongoAutoConfiguration.class, MongoDataAutoConfiguration.class })
@EnableConfigurationProperties({ AppConfig.class })
@EnableScheduling
@EnableJpaRepositories
@EnableAutoConfiguration
@ComponentScan("foo.bar")
public class MainApplication {

    public static void main(String[] args) {
        SpringApplication.run(MainApplication.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

Jak*_*Ch. 6

您的问题似乎与Thymeleaf 布局方言有关(请参阅 #4)。Spring-Boot1.5.3.RELEASE支持开箱即用,但2.2.2.RELEASE不支持。这甚至在方言的文档中有所说明:

布局方言已作为 Spring Boot 1.x 中 Thymeleaf 入门包的一部分包含在内,但已在 Spring Boot 2 中删除,因此上述 Spring Boot 2 用户的额外配置步骤。

上面链接中的以下安装步骤应该会有所帮助,即:

  • 添加maven依赖:
<dependency>
    <groupId>nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
    <version>2.4.1</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
  • 将额外添加@Bean到您的配置中:
@Bean
public LayoutDialect layoutDialect() {
    return new LayoutDialect();
}
Run Code Online (Sandbox Code Playgroud)