Thymelaf是一个用于web和独立环境的现代服务器端Java模板引擎。
Thymelaf的主要目标是为您的开发工作流程带来优雅的自然模板-HTML,它可以在浏览器中正确显示,也可以作为静态原型工作,从而增强开发团队的协作能力。
1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2.编写Controller
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class DemoController {
@RequestMapping("/hello")
public String hospital(ModelMap map) {
map.addAttribute("hello", "helloWord");
return "hello";
}
}
3.编写前端页面
resources/templates/hello.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>helloWard</title>
<meta charset="UTF-8">
<meta content="width=device-width, initial-scale=1" name="viewport">
</head>
<body>
<h1 th:align="center" th:text="${hello}"></h1>
</body>
</html>
4.启动测试结果