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

标签 java spring spring-boot thymeleaf

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

我关注了Thymeleaf 3 migration guide + 阅读 Spring-Boot release ,但没有成功...

模板片段(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">

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" />
    ...

代码片段(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">
...

任何帮助表示赞赏。谢谢

编辑:

@Marged,模型已设置:

@Configuration
public class MvcConfig implements WebMvcConfigurer {

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

}

上下文路径默认为 (/)

@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);
    }
}

最佳答案

您的问题似乎与 Thymeleaf Layout Dialect 有关(参见#4)。 Spring-Boot 1.5.3.RELEASE 开箱即用地支持它,但 2.2.2.RELEASE 不支持。这甚至在 dialect's documentation 中有说明。 :

The layout dialect is already included as part of the Thymeleaf starter pack in Spring Boot 1.x, but has been removed in Spring Boot 2, hence the additional config step for Spring Boot 2 users above.

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

  • 添加 Maven 依赖项:
<dependency>
    <groupId>nz.net.ultraq.thymeleaf</groupId>
    <artifactId>thymeleaf-layout-dialect</artifactId>
    <version>2.4.1</version>
</dependency>
  • 向您的配置添加额外的@Bean:
@Bean
public LayoutDialect layoutDialect() {
    return new LayoutDialect();
}

关于java - 升级到 Spring Boot 2.2.2.RELEASE + Thymeleaf 3.0.11.RELEASE,thymeleaf/layout 停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59739258/

相关文章:

spring - Grails DataBindingListener 没有听到绑定(bind)事件

java - Logstash : "Error: Could not find or load main class Heal" when running logstash. bat

java - Spring 表单 ModelAttribute 字段验证,避免 400 Bad Request Error

java - 找不到适用于 jdbc :amazonaws 的合适驱动程序

java - REST API 中线程 "main"java.lang.IllegalArgumentException 中出现异常

mysql - Gitlab-CI : Test Job is failing

java - spring-boot RabbitListener 如何将单个队列绑定(bind)到单个线程?

spring - Spring Boot 1.5.x 与 Hibernate 4.x 的兼容性

java - 在 Maven 中使用 AspectJ 方面编织生产类,仅用于测试执行

java - Spring JPA 投影包括链接