思路:在jsp中确认删除的标签中添加点击事件
删除已选
jsp中数据是先前通过后台查询语句所返回的语句接收的代码。
记住:在该如下代码中input单选框中一定要写value="<%=single.get(“emp_id”)%>",因为你要通过这串代码得到当选中的时候取得该行中你要通过该数据(如:emp_id)去后台与后台进行数据传输,并且通过该数据区进行选中删除。
<%
List> list = (List>) request.getAttribute("search");
%>
<%
for(int j=0;j single = list.get(j);
%>
<%= single.get("emp_id")%> <%= single.get("enterprise_name")%> <%= single.get("enterprise_leader")%> <%= single.get("login_id")%> <%= single.get("password")%> 详细信息
<%} %>
然后在该jsp中写一串js代码,为remove()事件写下记录是否选中的方法。
document.write中写的是一个在jsp中隐形的form表单,只有后台人员能看到,在jsp前端页面中看不到,我们就是通过这个form表单将所得到的数据传输到后台controller的方法。
controller代码:
@RequestMapping(value = "/deleteCheckedById")public ModelAndView deleteById(@RequestParam String num){List param = Arrays.asList(num.split(","));this.clientService.deleteCheckedByID(param);return new ModelAndView("redirect:search.do");}
因为前端传回的数据在前端是数组类型封装的,但是通过form表单提交过后到后台是String类型,所以我们要用String类型装,其次我们要将该数据以逗号的形式用数组形式装进去,num.split(",")。
为了方便mybatis中的sql语句查询,我们又将该数组通过Arrays.asList(要转类型的数组),转成List类型,这样就可以将数据通过Service层和Dao层到Mapper层的sql语句查询,以下是Mapper中的代码:
delete from netsite_plat_enterprisewhere emp_id in#{item}
其中id=""是Dao层中的接口方法名,这样才能与Dao有联系,parameterType中的是传入的数据类型,接下来的是sql语句,我们通过将传入的List类型数据遍历出来,通过delete from 数据库表名 where id in ( 1,2 ,3 ,4)的形式删除 当id等于1234时的数据库字段,collection="list"指的是传入的数据类型是List,index 就是 当前 List 集合遍历次数的 计数器, 故而使用它来 解决我们 业务中 需要设置 sort 字段的值的变化,item表示集合中每一个元素进行迭代时的别名,open表示该语句以什么开始,separator表示在每次进行迭代之间以什么符号作为分隔 符,close表示以什么结束。#{item}就是上面item所定义的值。
以上就是jsp中复选框选中的删除。