删除员工
Restful风格,删除发送delete请求
同样,form表单无法发送delete请求
采用,input标签value,指定请求方式
用户列表页面
<form id="deleteEmpForm" method="post">
<input type="hidden" name="_method" value="delete"/>
<button th:attr="del_uri=@{/emp/}+${emp.id}" class="btn btn-sm btn-danger deleteBtn">删除</button>
</form>
Controller
根据用户id,删除用户信息
删除成功,返回用户列表页面
//员工删除
@DeleteMapping("/emp/{id}")
public String deleteEmployee(@PathVariable("id") Integer id) {
employeeDao.delete(id);
return "redirect:/emps";
}
注意
为了执行删除请求
创建了一个form表单,用来执行delete请求
有多少个删除按钮,就会有多少个form表单
可以将form表单,提取出来
使用JS,给删除按钮,绑定form的action请求
所有的删除按钮
执行同一个form表单的action请求
删除按钮
<button th:attr="del_uri=@{/emp/}+${emp.id}" class="btn btn-sm btn-danger deleteBtn">删除</button>
Form表单
<form id="deleteEmpForm" method="post">
<input type="hidden" name="_method" value="delete"/>
</form>
JavaScript
删除员工的url,需要带着员工id
在button中设置action的属性值
th:attr=del_uri=@{/emp/}+${emp.id}
然后,在JS中获取该属性值,设置给action
<script>
$(".deleteBtn").click(function () {
//删除当前员工的
$("#deleteEmpForm").attr("action", $(this).attr("del_uri")).submit();
return false;
});
</script>