servlet模块 参数delete 编写删除功能
先要在页面代码中找到带有删除功能的按钮位置
<button type="button" class="btn btn-default" title="删除" onclick='deleteById()'><i class="fa fa-trash-o"></i> 删除</button>
这里的删除按钮 在onclick 单击事件调用了deleteById() 操作
这就要去script标签中找,
<script>
function deleteById() {
var id = getCheckId()
if(id) {
if(confirm("你确认要删除此条记录吗?")) {
location.href="${ctx}/store/company?operation=delete&id="+id;
}
}else{
alert("请勾选待处理的记录,且每次只能勾选一个")
}
}
</script>
它会单出一个框 confirm(“你确认要删除此条记录吗?”)
通过 var id = getCheckId()获取到勾选中复选框中的id值
location.href="${ctx}/store/company?operation=delete&id="+id;
在这个语句中的访问路径拼接上获取到的id值
如果此时没有勾选中的复选框它会走
alert(“请勾选待处理的记录,且每次只能勾选一个”)
弹出提示信息。
在servlet模块中
通过 这招获取参数名称operation,得到参数的值
使它去走进本类中的delete方法this.delete(req,resp);
else if("delete".equals(operation)){
this.delete(req,resp);
}
接下来就要编写delete(req,resp);方法
先用 BeanUtil.fillBean(req, Company.class);将请求数据封装成对象,
在调用service层的delete 删除方法,并将封装得到的对象传到delete方法中去。
删除后 走本类中的list方法,为的是用list方法中分页显示的功能
回到list.jsp分页显示数据的页面,
删除后数据库中的数据会发生改变,所以要发送req、resp对象回到list.jsp页面
private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
//将数据获取到,封装成一个对象
Company company = BeanUtil.fillBean(req, Company.class);
//调用业务层接口save
// CompanyService companyService = new CompanyServiceImpl();
companyService.delete(company);
//跳转回到页面list
//list(request,response);
resp.sendRedirect(req.getContextPath()+"/store/company?operation=list");
}