2019独角兽企业重金招聘Python工程师标准>>>
一、引入依赖(可在新建项目时勾选thymeleaf)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
二、设置模板信息
@Controller
public class SysUserController {
@GetMapping("/")
public ModelAndView index(HttpSession session){
Object username = session.getAttribute("username");
if(username==null){
username="张三";
session.setAttribute("username",username);
}
ModelMap model = new ModelMap("message","个人信息减价")
.addAttribute("username",username)
.addAttribute("title",username)
.addAttribute("date",new Date())
.addAttribute("foot","哈哈");
return new ModelAndView("index",model);
}
}
三、读取模板信息
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Thymeleaf</title>
<link rel="stylesheet" th:href="@{~/css/app.css}">
</head>
<body>
<section>
<header>
<h1 th:text="${title}">标题</h1>
</header>
<article>
<div>
<p th:text="${username}">用户名</p>
<p th:text="${#calendars.format(date,'yyyy-MM-dd')}">日期</p>
</div>
<p th:text="${message}"></p>
</article>
<footer th:text="${foot}">注脚</footer>
</section>
</body>
</html>
参考: https://blog.csdn.net/f0rd_/article/details/80580225
https://jingyan.baidu.com/album/2a138328edd242074a134fce.html?picindex=6