升级到 Spring Boot 2.2.2.RELEASE + Thymeleaf 3.0.11.RELEASE,Thy



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

绒球.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 迁移指南 + 阅读了 Spring-Boot 版本,但没有成功......

模板片段(百里香叶 3.0layout: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.发布(

<!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.发布(

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

您的问题似乎与百里香叶布局方言有关(见#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>
  • 将额外@Bean添加到您的配置中:
@Bean
public LayoutDialect layoutDialect() {
return new LayoutDialect();
}

最新更新