<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
配置
spring:
thymeleaf:
prefix: classpath:/templates
suffix: .html
controller
@Controller
public class TestController {
@RequestMapping("/test")
public String test( Model model) {
Linker linker1 = new Linker();
linker1.setName("wo de name");
linker1.setLink("wo de linkddd");
Linker linker2 = new Linker();
linker2.setName("wo de name1");
linker2.setLink("wo de link1dd");
ArrayList<Object> list = new ArrayList<>();
list.add(linker1);
list.add(linker2);
model.addAttribute("welcome","欢迎光临");
model.addAttribute("list",list);
return "hello";
}
}
hello.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<p th:text="'Hello, ' + ${welcome1} + '!'">3333</p>
<table border="1">
<tr>
<th>用户名</th>
<th>邮箱</th>
</tr>
<tr th:each="user,userStat : ${list}">
<td th:text="${user.name}">Onions</td>
<td th:text="${user.link}">test@test.com.cn</td>
</tr>
</table>
</body>
</html>
渲染截图
image.png