springboot传列表到html
- 在Controller中定义一个返回List的接口方法:
@GetMapping("/users")
public String getUsers(Model model) {
List<User> users = userRepository.findAll();
model.addAttribute("users", users);
return "users";
}
- 在HTML页面中使用Thymeleaf模板引擎来遍历列表:
<table>
<thead>
<tr>
<th>ID</th>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr th:each="user : ${users}">
<td th:text="${user.id}"></td>
<td th:text="${user.name}"></td>
<td th:text="${user.email}"></td>
</tr>
</tbody>
</table>
其中,th:each用于遍历列表,${users}代表Controller中传递过来的List对象,user为遍历时的临时变量,可以通过th:text来获取对象的属性值
原文地址: https://www.cveoy.top/t/topic/ffTO 著作权归作者所有。请勿转载和采集!