删除多条数据-解决方案

Struts1.2 全选、多选,一次性删除多条数据

 

1、JSP

JavaScript控制全选,

form里面 checkbox带上删除项目的ID:<html:checkbox property="deleteId" value="${u.photoId}" />

codes:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%> <%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean"%> <%@ taglib uri="http://struts.apache.org/tags-html" prefix="html"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>图片管理</title> <mce:script type="text/javascript"><!-- 删除确认 --> function del() { if (confirm("您确定要删除吗")) { document.forms[0].submit(); } else { return false; } } <!-- 全选 --> function selectChange() { var chkSelectAll = document.getElementById("chkSelectAll"); var checked = chkSelectAll.getAttribute("checked"); if (checked) { selectAllItem(); } else { unSelectAll(); } } //选择全部商品 function selectAllItem() { var items = document.getElementsByName("deleteId"); for ( var i = 0; i < items.length; i++) { items[i].checked = true; } } //取消全选 function unSelectAll() { var items = document.getElementsByName("deleteId"); for ( var i = 0; i < items.length; i++) { items[i].checked = false; } } // --></mce:script> </head> <body> <h1><a href="system/photo/addPhoto.jsp" mce_href="system/photo/addPhoto.jsp">添加图片</a></h1> <h1><a href="photo.do?method=staticize" mce_href="photo.do?method=staticize">图片静态化</a></h1> <h1><a href="${username }/phooto/index.html" mce_href="${username }/phooto/index.html">欣赏图片</a></h1> <input type="checkbox" name="chkSelectAll" id="chkSelectAll" onclick="selectChange();"/>全选 <input type="button" onclick="return del();" value="删除"/> <html:form action="/photo.do?method=delete"> <table> <tr> <c:forEach items="${photoList}" var="u" varStatus="vs"> <td> <table> <tr> <td><a href="/MySite${u.photoLocation}" mce_href="MySite${u.photoLocation}"><img src="/MySite${u.photoLocation}" mce_src="MySite${u.photoLocation}" width="125" height="100"/></td> </tr> <tr> <td><html:checkbox property="deleteId" value="${u.photoId}" />${u.title}</td> </tr> </table> </td> <c:if test="${vs.count%4==0}"> </tr> <tr> </c:if> </c:forEach> </tr> </table> </html:form> </body> </html>

 

2、在Form类中,添加属性

codes:

/* 对多条进行删除操作 */ private Integer[] deleteId; public Integer[] getDeleteId() { return deleteId; } public void setDeleteId(Integer[] deleteId) { this.deleteId = deleteId; }

 

3、在Action中,获取需要参数项目的ID数组,逐一删除

codes:

// 删除多条照片 public ActionForward delete(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { PhotoForm puniForm = (PhotoForm) form; Integer[] deleteId=puniForm.getDeleteId(); if(deleteId==null) return mapping.findForward("showAll"); Photo u; PhotoDAO dao=new PhotoDAO(); for(int id:deleteId) { u=dao.findById(id); dao.delete(u); } // 更新静态界面 String user_id=(String)request.getSession().getAttribute("user_id"); String username=(String)request.getSession().getAttribute("username"); String location=servlet.getServletContext().getRealPath(""); staticize.IndexGenForVideo.refreshIndex(location,"/"+username, user_id); return mapping.findForward("showAll"); }

你可能感兴趣的:(html,String,function,XHTML,Integer,action)