spring-boot-starter-thymeleaf 和 thymeleaf-layout-dialect 无需显式 LayoutDialect bean 即可工作 - 可以吗?

问题描述 投票:0回答:1

我有一个 Spring boot Maven 项目:

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

依赖项中有:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
        </dependency>

版本有:

  • org.springframework.boot:spring-boot-starter-thymeleaf:jar:3.2.0
  • nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:jar:3.3.0

此说明说:

要开始使用 Layout Dialect,我们需要将其包含到 pom.xml 中。依赖关系是:

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

我们还需要通过向模板引擎添加额外的方言来配置集成:

@Bean
public SpringTemplateEngine templateEngine() {
    SpringTemplateEngine templateEngine = new SpringTemplateEngine();
    ...
    templateEngine.addDialect(new LayoutDialect());
    return templateEngine;
}

我不添加

SpringTemplateEngine
bean,我的项目可以正常工作。

其他一些说明说我需要添加

nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect
bean,但我也没有添加。

出于调查目的,我使用以下控制器:

@Controller
public class DemoController {

    @Autowired
    LayoutDialect layoutDialect;

    @GetMapping("/demo")
    public String getAllUsers() {
        System.out.println(layoutDialect);
        return "thymeleaf_first_page";
    }
}

在控制台中我看到:

nz.net.ultraq.thymeleaf.layoutdialect.LayoutDialect@7c6ab057

这意味着该 bean 存在于上下文中。

问题

您能否确认,使用我的较新版本的 Spring Boot 和

spring-boot-starter-thymeleaf
不需要显式添加
LayoutDialect
bean?

有相关文档吗?

更新

这对于这个问题来说并不重要,但会添加一些关于所做工作的说明。

布局是(

src/main/resources/templates/layout/layout.html
):

<!DOCTYPE html>
<html lang="en" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title layout:fragment="title"></title>
</head>
<body>
<header>
    <nav><span>LOGO</span> <a href="#">LINK1</a> <a href="#">LINK2</a> </nav>
</header>
    <main id="mainpart" layout:fragment="main">
        <p><em>This content must not be visible.</em></p>
    </main>
<footer>Footer information</footer>
</body>
</html>

页面是(

src/main/resources/templates/thymeleaf_first_page.html
):

<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      xmlns:th="http://www.thymeleaf.org"
      layout:decorate="~{layout/layout}">
<head>
    <title layout:fragment="title">Thymeleaf demo</title>
</head>
<body>
    <main layout:fragment="main">
        <h1>Thymeleaf template extension demo</h1>
        <p>Actual page content: [<th:block th:text="${modelValue}" />]</p>
    </main>
</body>
</html>

结果是:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    
    <title>Thymeleaf demo</title>
    
</head>
<body>
<header>
    <nav><span>LOGO</span> <a href="#">LINK1</a> <a href="#">LINK2</a> </nav>
</header>
    <main id="mainpart">
        <h1>Thymeleaf template extension demo</h1>
        <p>Actual page content: [some value from the model]</p>
    </main>
<footer>Footer information</footer>
</body>
</html>

java spring-boot thymeleaf
1个回答
0
投票

看起来 Thymeleaf 3.1 版模板扩展应该使用教程:使用 Thymeleaf (3.1.2.RELEASE),第 8 章模板布局中描述的其他机制来完成:

布局是(

src/main/resources/templates/layout/layout_simple.html
):

<!DOCTYPE html>
<html lang="en" th:fragment="layout(title, main)"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title th:replace="${title}">Layout title</title>
</head>
<body>
<header>Common block: header</header>
<main id="mainpart" th:replace="${main}">
    <p><em>This content must not be visible.</em></p>
</main>
<footer>Common block: footer</footer>
</body>
</html>

页面是(

src/main/resources/templates/thymeleaf_first_page_simple.html
):

<!DOCTYPE html>
<html th:replace="~{layout/layout_simple :: layout(~{::title}, ~{::main})}"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Thymeleaf demo simple</title>
</head>
<body>
<main>
    <h1>Thymeleaf template extension simple demo</h1>
    <p>Actual page content: [<th:block th:text="${modelValue}" />]</p>
</main>
</body>
</html>

结果是:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Thymeleaf demo simple</title>
</head>
<body>
<header>Common block: header</header>
<main>
    <h1>Thymeleaf template extension simple demo</h1>
    <p>Actual page content: [some simple value from the model]</p>
</main>
<footer>Common block: footer</footer>
</body>
</html>

因此无需使用依赖项

nz.net.ultraq.thymeleaf:thymeleaf-layout-dialect:jar
(并定义
LayoutDialect
bean)。

© www.soinside.com 2019 - 2024. All rights reserved.