【发布时间】:2021-09-22 21:52:47
【问题描述】:
有人可以帮忙处理 Thymeleaf 吗?我需要创建一个模板布局,但我被<div th:fragment="content"></div> 卡住了。重点是layout:fragment 不会替换仪表板页面上的代码。顺便说一句,layout:decorate 或 th:replace 等其他处理器运行良好。我在 Spring 2.5.4 中使用 Intellij IDEA。
#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();
}
}
更新:
问题解决了。只需要使用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}"?