1、导入pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、Template
在resource 目录下新建templates文件夹,新建user文件夹,新建list.html
html内容如下
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<h1>用户列表</h1>
<div>
<ul>
<li th:each="user:${users}">
<span th:text="${user.id}"></span>-
<span th:text="${user.name}"></span>-
<span th:text="${user.age}"></span>-
<span th:text="${user.address}"></span>
</li>
</ul>
</div>
</body>
</html>
3、新建返回的UserDTO
@Data
@NoArgsConstructor
@AllArgsConstructor
public class UserDTO {
private Integer id;
private String name;
private Integer age;
private String address;
}
4、Controller
@Controller
@RequestMapping("/user")
public class UserController {
@RequestMapping(value = "/list",method = RequestMethod.GET,produces = MediaType.APPLICATION_JSON_VALUE)
public String getUser(Model model){
List<UserDTO> userList = new ArrayList<>();
for (int i = 0; i <10; i++) {
userList.add(new UserDTO(i,"张三"+i,20+i,"上海"));
}
model.addAttribute("users", userList);
return "/user/list";
}
}