Thymeleaf+springBoot实现:“增”,“删”,“改”,“查”接口功能(2)
2.查、改、删
前台代码:
(1)list页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div th:replace="~{fragments/header :: header}"></div>
<h3 th:text="${userModel.title}"></h3>
<div>
<a href="/user/form.html" th:href="@{/user/form}">创建用户</a>
</div>
<table border="1">
<thead>
<tr>
<td>ID</td>
<td>Name</td>
<td>Email</td>
</tr>
</thead>
<tbody>
<tr th:if="${userModel.userList.size() eq 0}">
<td colspan="3">没有用户信息!</td>
</tr>
<tr th:each="user:${userModel.userList}">
<td th:text="${user.id}"></td>
<td><a th:href="@{'/user/view/'+${user.id}}" th:text="${user.name}"></a></td>
<td th:text="${user.email}"></td>
</tr>
</tbody>
</table>
<div th:replace="~{fragments/footer :: footer}"></div>
</body>
</html>
(2)view页面
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org"
xmlns:layout="http://www.ultraq.net.nz/web/thymeleaf/layout">
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<div th:replace="~{fragments/header :: header}"></div>
<h3 th:text="${userModel.title}">删除或修改</h3>
<div>
<p><strong>ID:</strong><span th:text="${userModel.user.id}"></span></p>
<p><strong>Name:</strong><span th:text="${userModel.user.name}"></span></p>
<p><strong>Email:</strong><span th:text="${userModel.user.email}"></span></p>
</div>
<div>
<a th:href="@{'/user/delete/'+${userModel.user.id}}">删除</a>
<a th:href="@{'/user/modify/'+${userModel.user.id}}">修改</a>
</div>
<div th:replace="~{fragments/footer :: footer}"></div>
</body>
</html>
2.后台代码
1.control中
/**
* 删除
*
* @return
*/
@GetMapping("/delete/{id}")
public ModelAndView delete(@PathVariable("id") Long id) {
userRepository.deleteUser(id);
return new ModelAndView("redirect:/user/list");// 重定向到list页面
}
/**
* 修改用户见面
*
* @return
*/
@GetMapping("/modify/{id}")
public ModelAndView modify(@PathVariable("id") Long id, Model model) {
User user = userRepository.getUserById(id);
model.addAttribute("user", user);
model.addAttribute("title", "修改用户");
return new ModelAndView("users/form", "userModel", model);
}
2.实现类中:
@Override
public void deleteUser(Long id) {
this.userMap.remove(id);
}
3.浏览器页面