创建控制器

@GetMapping("/p6")
public void p6(Model model, HttpServletRequest req) {
    req.getSession().setAttribute("test","西瓜");

    User user = new User();
    user.setUsername("P6");
    user.setDate(new Date());
    model.addAttribute("user",user);
}

创建前端

创建templates/p6.html文件,内容如下:

<!DOCTYPE html>
<html lang="en" xmlns:th="<http://www.thymeleaf.org>">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>

<!--获取session的对象-->
<h1 th:text="${session.test}"></h1>

<!--格式化时间-->
<h2 th:text="${user.date}"></h2>
<h2 th:text="${#dates.format(user.date, 'yyyy-MM-dd HH:mm')}"></h2>

</body>
</html>