我试图引入一种布局(如here https://ultraq.github.io/thymeleaf-layout-dialect/Examples.html)在我的应用程序中使用 thymeleaf 但无法让它工作。我已经查过了this https://stackoverflow.com/questions/28385056/thymeleaf-decorator-is-not-working post.
pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
</parent>
...
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
</dependency>
这是我的 MvcConfig
@Configuration
public class MvcConfig extends WebMvcConfigurerAdapter {
...
@Bean
public LayoutDialect layoutDialect() {
return new LayoutDialect();
}
}
给定的layout.htmlhere https://ultraq.github.io/thymeleaf-layout-dialect/Examples.html#layouts。没有变化。添加xmlns:th="http://www.thymeleaf.org
在检查一些文章时。
<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org">
<head>
<title>Layout page</title>
</head>
<body>
<header>
<h1>My website</h1>
</header>
<div layout:fragment="content">
<p>Page content goes here</p>
</div>
<footer>
<p>My footer</p>
<p layout:fragment="custom-footer">Custom footer here</p>
</footer>
</body>
</html>
我正在尝试替换 login.html 中的内容片段,如下所示
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout" xmlns:th="http://www.thymeleaf.org"
layout:decorate="~{layout}">
<head>
<title>Application Login</title>
</head>
<body>
<div layout:fragment="content">
<div th:if="${param.error}">
Invalid username and password.
</div>
<div th:if="${param.logout}">
You have been logged out.
</div>
<form th:action="@{/login}" method="post">
<div><label> User Name : <input type="text" name="username" value=""/> </label></div>
<div><label> Password: <input type="password" name="password" value=""/> </label></div>
<div><input type="submit" value="Sign In"/></div>
</form>
</div>
</body>
</html>
我错过了什么吗?