目录
2、在templates目录下面创建一个test.html文件
Thymeleaf模板引擎(就是做以前jsp的工作,页面+后端数据,做的工作跟jso一样,接收后端数据,放在前端代码中,显示在页面里面)
1、引入thymeleaf,导入依赖
<dependency>
<groupId>org.thymeleaf</groupId>
<artifactId>thymeleaf-spring5</artifactId>
</dependency>
<dependency>
<groupId>org.thymeleaf.extras</groupId>
<artifactId>thymeleaf-extras-java8time</artifactId>
</dependency>
2、在templates目录下面创建一个test.html文件
并且导入约束 xmlns:th="http://www.thymeleaf.org" 这样才能使用thymeleaf的语法
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<!--所有的html元素都可以被thymeleaf替换接管, th:元素名-->
<div th:text="${msg}"></div>
<div th:utext="${msg}"></div>
<hr>
<!--遍历取值-->
<h3 th:each="user:${users}" th:text="${user}"></h3>
</body>
</html>
3、编写Controller
package com.zhou.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
@Controller
public class HelloController {
@GetMapping("/hello")
@ResponseBody
public String hello(){
return "hello,world";
}
@GetMapping("/test")
public String test(Model model){
model.addAttribute("msg", "hello springboot");
// 制造出一个集合,然后用thymeleaf来遍历
model.addAttribute("users", Arrays.asList("zhoujie", "xiaojie "));
return "test";
}
}