【问题标题】:Thymeleaf fragment processor and Spring 5Thymeleaf 片段处理器和 Spring 5
【发布时间】:2021-09-22 21:52:47
【问题描述】:

有人可以帮忙处理 Thymeleaf 吗?我需要创建一个模板布局,但我被<div th:fragment="content"></div> 卡住了。重点是layout:fragment 不会替换仪表板页面上的代码。顺便说一句,layout:decorate 或 th:replace 等其他处理器运行良好。我在 Spring 2.5.4 中使用 Intellij IDEA。

My template structure

#dashboard.html

<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      layout:decorate="~{admin/share/template.html}">
    <head>
        <title>Admin dashboard</title>
    </head>
    <body>
        <div layout:fragment="content">
            <h1>Admin Dashboard</h1>
        </div>
    </body>
</html>

#template.html

<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout">
<head>
    <style th:replace="~{admin/share/style}"></style>
</head>
<body>
<div class="app-container app-theme-white body-tabs-shadow fixed-sidebar fixed-header">
    <div th:replace="/admin/share/header"></div>
    <div class="app-main">
        <div th:replace="/admin/share/sidebar"></div>
        <div class="app-main__outer">
            <div class="app-main__inner">
                <div th:fragment="content">
                    <p>Page content goes here</p>
                </div>
            </div>
        </div>
    </div>
    <div th:replace="/admin/share/js"></div>
</div>
</body>
</html>

#pom.xml

<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>
    <version>3.0.0</version>
</dependency>

#Thymeleaf.class

@Configuration
public class Thymeleaf {
    @Bean
    public LayoutDialect layoutDialect() {
        return new LayoutDialect();
    }
}

Result of Dashboard page

更新:

问题解决了。只需要使用layout:fragment 而不是th:fragment =)

【问题讨论】:

  • 你试过这样添加方言吗? TemplateEngine templateEngine = new TemplateEngine(); templateEngine.addDialect(new LayoutDialect()); 如:ultraq.github.io/thymeleaf-layout-dialect/getting-started
  • 是否有可能在 #dashboard.html 上您的意思是 layout:decorate="~{/admin/share/template.html}" 而不是 layout:decorate="~{admin/share/template.html}"?

标签: java spring thymeleaf


【解决方案1】:

只需要使用layout:fragment 而不是th:fragment =)

【讨论】:

    猜你喜欢
    • 2016-07-02
    • 2014-11-02
    • 2014-01-25
    • 2016-04-22
    • 2017-03-08
    • 1970-01-01
    • 2019-10-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多