我刚尝试过弹簧靴。 我用百里香,
list.html:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
>
<head>
<meta charset="UTF-8" />
<title>Thymeleaf in action</title>
</head>
<body>
<div th:replace="~{fragments/header}:: header"></div>
</body>
</html>
header.html中:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
>
<head>
<meta charset="UTF-8" />
<title>Thymeleaf in action</title>
</head>
<body>
<div th:fragment="header">
header
</div>
</body>
</html>
控制器:
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserRepository userRepository;
@GetMapping
public ModelAndView list(Model model) {
model.addAttribute("userList", userRepository.listUsers());
model.addAttribute("title", "account management");
return new ModelAndView("users/list", "userModel", model);
}
}
pom文件:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>Blog</groupId>
<artifactId>Blog</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
当我尝试打开localhost:8080 / users页面
Whitelabel错误页面 此应用程序没有/ error的显式映射,因此您看到了这一点 作为后备。
Tue Mar 20 20:51:11 PDT 2018 出现意外错误(type = Internal Server Error,status = 500)。 解析模板&#34;标头&#34;时出错,模板可能不存在或可能不存在 可由任何已配置的模板解析器(用户/列表:11)
访问不知道如何添加页眉和页脚
答案 0 :(得分:3)
如果您的模板文件夹文件结构是这样的。
模板
\
|
|-片段(文件夹)
(header.html文件位置为-> /src/main/resources/templates/fragments/header.html)
您可以像这样将header部分添加到list.html:
(替换head标签)
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
>
<header th:replace="fragments/header">
</header>
<body>
//content
</body>
</html>
header.html如下所示:
(在head标签中添加“ th:fragments =“ header”“)
<head th:fragments="header" >
<meta charset="UTF-8" />
<title>Thymeleaf in action</title>
</head>
如果要添加一些“ div”部分,可以这样添加。
example.html
<div th:replace="fragments/sample">
// content
</div>
在添加示例example.html片段list.html之后,如下所示:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
>
<header th:replace="fragments/header">
</header>
<body>
<div th:replace="fragments/example"> </div>
</body>
</html>