springboot用户信息删除(delete方式)

阅读: 评论:0

springboot⽤户信息删除(delete⽅式) 需求:⽤户信息删除操作
具体步骤:
1.  userlist.html 页⾯内容如下
<!DOCTYPE html>
<html lang="en" xmlns:th="">
<head>
<meta charset="UTF-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">脑根
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<meta name="description" content="">
<meta name="author" content="">
<title>Dashboard Template for Bootstrap</title>
<!-- Bootstrap core CSS -->
<link th:href="@{/assets/css/bootstrap.min.css}" rel="stylesheet">
<!-- Custom styles for this template -->
<script src="assets/js/jquery-3.4.0.min.js"></script>
<script src="assets/js/bootstrap.min.js"></script>
</head>
<body>
<!--在原来的位置引⽤公共的头部元素,原来的 <nav元素就是在这⾥的
commonsHead:模板名,根据Spring Boot配置的Thymeleaf映射查
head:是模板中公⽤的代码⽚段-->
<div ></div>
<div class="container-fluid">
<div class="row">
<!-- 在原来的左侧菜单位置引⽤抽取好的公共左侧菜单代码
1、这是使⽤的 id选择器进⾏的引⽤
2、引⽤公共代码的⽚段的同时传递参数值过去-->
<div></div>
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<h2><a th:href="@{/userCRUD}" class="btn btn-primary">⽤户添加</a></h2>
<div class="table-responsive">
电磁线圈
<table class="table table-striped table-sm">
<thead>
<tr>
<th>#</th>
<th>⽤户名</th>
<th>邮箱</th>
<th>性別</th>
<th>⽣⽇</th>
<th>所属部门</th>
<th>操作</th>
</tr>
</thead>
<tbody>
<!-- th:each 表⽰遍历,userList是后台的参数名;循环时每次的变量名为 user
如同 JSTL ⼀样,当userList为null 或者⼤⼩为0 时,不会报错-->
<tr th:each="user:${userList}">
<!-- th:text 为标签赋值-->
<td th:text="${user.uId}"></td>
薄页纸<!-- 这是⾏内写法,写成: <td th:text="${user.uName}"></td> 也是可以的-->
<td>[[${user.uName}]]</td>
<td th:text="${ail}"></td>
<!-- 三元运算符,User的gender为0则表⽰⼥⽣,1表⽰南⼭-->
<td th:text="${der}==0?'⼥':'男'"></td>
<!-- 这是 Thymeleaf 的⽇期格式化,如同JSTL的⽇期格式类似;可以官⽅⽂档-->
<td th:text="${#dates.format(user.birth, 'yyyy-MM-dd')}"></td>
<!-- 这是级联获取属性值,因为User中关联了 Department 对象-->
<td th:text="${user.department.depName}"></td>
<td>
<a class="btn btn-sm btn-primary" th:href="@{/userCRUD/}+${user.uId}">修改</a>
<button th:attr="del_uri=@{/userCRUD/}+${user.uId}"  class="btn btn-sm btn-danger deleteBtn">删除</button>                        </td>
</tr>
</tbody>
</table>
</div>
</main>
</div>
</div>
<form id="deleteEmpForm"  method="post">
<input type="hidden" name="_method"  value="delete"/>
</form>
<script>
$(".deleteBtn").click(function(){
alert("abc");
//删除当前员⼯的
$("#deleteEmpForm").attr("action",$(this).attr("del_uri")).submit();
alert("abc");
//return false;
});
</script>
</body>
</html>
(1)⾸先,为了在js中对删除这个按钮做操作,我们在class中加⼀个属性值,我这⾥起名叫:deleteBtn
<button th:attr="del_uri=@{/emp/}+${emp.id}"  class="btn btn-sm btn-danger deleteBtn">删除</button>
(2)click⼀个事件function,返回⼀个fasle是为了取消这个按钮的默认⾏为。click事件触发form表单的deleteEmpForm
<input type="hidden" name="_method"  value="delete"/>
</form>
<script>
$(".deleteBtn").click(function(){
alert("abc");
//删除当前员⼯的
$("#deleteEmpForm").attr("action",$(this).attr("del_uri")).submit();
alert("abc");
//return false;
});
</script>
</body>
</html>
2. 在UserController类⾥添加deleteEmployee函数
/**员⼯删除
*
*/
@DeleteMapping("userCRUD/{userId}")
public String deleteEmployee(@PathVariable("userId") Integer id){
userDaoImpl.delUserById(id);
return  "redirect:/userlist";
}
/**
* 跳转到⽤户列表页⾯
*
* @param model ⽤户设值返回页⾯
* @return金属化膜
*/
@GetMapping("userlist")
public String findAllUsers(Model model) {
沟槽式管接头
List<User> userList = All();
model.addAttribute("userList", userList);
/** 往前端 Thymeleaf 模板引擎时,开头不要加 "/" ,因为它默认配置的前缀就是:
* spring.thymeleaf.prefix=classpath:/templates/
*/
System.out.println("findAllUsers");
return "userList";
}
3.UserDaoImpl类⾥添加 delUserById
/**
* 根据⽤户id删除⽤户
*
* @param user
*/
public void delUserById(Integer userId) {
if (userId != null) {
for (int i = 0; i < userList.size(); i++) {
if ((i).getUId().equals(userId)) { ve(i);
break;
}
}
}
}

本文发布于:2023-05-20 10:08:34,感谢您对本站的认可!

本文链接:https://patent.en369.cn/patent/4/106792.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:属性   事件   删除   配置   默认   模板
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 369专利查询检索平台 豫ICP备2021025688号-20 网站地图