1.在pom中引入thymeleaf
<!--引入thymeleaf依赖-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.如何关闭thymeleaf缓存
在application.properties中设置:
spring.thymeleaf.cache=false
3.编写模板文件hello.html
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"xmlns:th="http://www.thymeleaf.org"
xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" lang="en">
<head>
<meta charset="UTF-8">
<title>Hello World!</title>
</head>
<body>
<h1 th:inline="text">Hello.v.2</h1>
<p th:text="${hello}"></p>
</body>
</html>
4.编写访问模板文件controller
TemplatesController:
package com.huawei.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.Map;
/**
* 注意:
* 1.在Thymeleaf模板文件中,标签是需要闭合的,3.0之前是需要闭合的
* 2.thymeleaf3.0+是可以不强制要求闭合的。
*/
@Controller
@RequestMapping("/templates")
public class TemplatesController
{
@RequestMapping("/test")
public String hello(Map<String, Object> map) {
map.put("hello", "hello");
return "hello";
}
}
运行结果: