生活随笔
收集整理的這篇文章主要介紹了
struts2批量删除
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2019獨角獸企業重金招聘Python工程師標準>>>
2012-10-27 12:48? 997人閱讀? 評論(0)? 收藏? 舉報
邏輯代碼
[java]? view plain copy
package?com.stu2;?? ?? import?java.util.ArrayList;?? import?java.util.List;?? import?java.sql.*;?? ?? import?com.stu.Conn;?? import?com.stu.DBclose;?? public?class?Stubean?{?? ????private?Connection?ct=null;?? ????private?PreparedStatement?ps=null;?? ????private?ResultSet?rs=null;?? ????private?Statement?st=null;?? public?List?all(){?? ????List?list=new?ArrayList();?? ????try{?? ????????ct=Conn.getinstance();?? ????????ps=ct.prepareStatement("select?*?from?Stu?");?? ????????rs=ps.executeQuery();?? ????Stu?s=null;?? ????????while(rs.next()){?? ????????????s=new?Stu();?? ????????????s.setId(rs.getInt(1));?? ????????????s.setSname(rs.getString(2));?? ????????????s.setClasses(rs.getString(3));?? ????????????list.add(s);?? ????????}?? ????}catch(Exception?e){?? ????????e.printStackTrace();?? ????}finally{?? ????????DBclose.close(ct,?ps,?rs);?? ????}?? ????return?list;?? ?????? }?? public?void?delete(String?id){?? ????try{?? ????????ct=Conn.getinstance();?? ????????st=ct.createStatement();?? ?????????? ????????st.addBatch("delete??from?Stu?where?id="+id+"");?? ????????st.executeBatch();?? ????}catch(Exception?e){?? ????????e.printStackTrace();?? ????}finally{?? ????????DBclose.close(ct,?ps);?? ????}?? }?? }?? Action類 [java]? view plain copy
package?com.stu2;?? ?? import?java.util.List;?? ?? import?javax.servlet.http.HttpServletRequest;?? ?? import?org.apache.struts2.ServletActionContext;?? ?? import?com.opensymphony.xwork2.ActionSupport;?? ?? public?class?StuAction?extends?ActionSupport{?? private?List?list;?? ????public?List?getList()?{?? ????return?list;?? }?? public?void?setList(List?list)?{?? ????this.list?=?list;?? }?? ????@Override?? ????public?String?execute()?throws?Exception?{?? ????????Stubean?sb=new?Stubean();?? ????list=sb.all();?? ????????return?SUCCESS;?? ????}?? ????public?String?remove(){?? ????????Stubean?sb=new?Stubean();?? ?????? ????????HttpServletRequest?q=ServletActionContext.getRequest();?? ????String?id[]=q.getParameterValues("choice");?? ????for(int?i=0;i<id.length;i++){?? ????????sb.delete(id[i]);?? ????}?? ????list=sb.all();?? ?????? ?????? ????return?SUCCESS;?? ????}?? ?? }?? 刪除頁面 [html]? view plain copy
<%@?page?language="java"?import="java.util.*"?pageEncoding="utf-8"%>?? <%@?taglib?prefix="s"?uri="/struts-tags"%>?? <%?? String?path?=?request.getContextPath();?? String?basePath?=?request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";?? %>?? ?? <!DOCTYPE?HTML?PUBLIC?"-//W3C//DTD?HTML?4.01?Transitional//EN">?? <html>?? ??<head>?? ???<style?type="text/css">???? ?????table{?border:0;border-collapse:collapse;}???? ?????td{?font:normal?12px/17px?Arial;padding:2px;width:100px;}???? ?????th{?font:bold?12px/17px?Arial;text-align:left;padding:4px;border-bottom:1px?solid?#333;}???? ?????.even{?background:#FFF38F;}??/*?偶數行樣式*/???? ?????.odd{?background:#FFFFEE;}??/*?奇數行樣式*/???? ?????.selected{?background:#FF6500;color:#fff;}???? ?????</style>???? ??<script?type="text/javascript"?src="js/jquery-1.7.1.min.js"></script>?? ??<script?type="text/javascript">?? ???//插件編寫???? ?????(function?($)?{???? ?????????$.fn.extend({???? ?????????????"alterBgColor":?function?(options)?{???? ?????????????????//設置默認值???? ?????????????????options?=?$.extend({???? ?????????????????????odd:?"odd",?/*?偶數行樣式*/???? ?????????????????????even:?"even",?/*?奇數行樣式*/???? ?????????????????????selected:?"selected"?/*?選中行樣式*/???? ?????????????????},?options);???? ?????????????????$("tbody>tr:odd",?this).addClass(options.odd);???? ?????????????????$("tbody>tr:even",?this).addClass(options.even);???? ?????????????????$('tbody>tr',?this).click(function?()?{???? ?????????????????????//判斷當前是否選中???? ?????????????????????var?hasSelected?=?$(this).hasClass(options.selected);???? ?????????????????????//如果選中,則移出selected類,否則就加上selected類???? ?????????????????????$(this)[hasSelected???"removeClass"?:?"addClass"](options.selected)???? ?????????????????????//查找內部的checkbox,設置對應的屬性。???? ??????????.find(':checkbox').attr('checked',?!hasSelected);???? ?????????????????});???? ???????? ???????? ?????????????????//表頭中的checkbox?(全選?反選)???? ?????????????????$("thead>tr?th:first?:checkbox:first?").click(function?()?{???? ?????????????????????//判斷當前是否選中???? ?????????????????????var?hasSelected?=?$(this).attr("checked");???? ?????????????????????//如果選中,則移出selected類,否則就加上selected類???? ?????????????????????$('tbody>tr')[!hasSelected???"removeClass"?:?"addClass"](options.selected);???? ?????????????????????if?(hasSelected)???? ?????????????????????????$('tbody>tr?:checkbox').attr("checked",true);???? ?????????????????????else???? ????????????????????????$('tbody>tr?:checkbox').attr("checked",false);???? ?????????????????});???? ?????????????????//?如果單選框默認情況下是選擇的,則高色.???? ?????????????????$('tbody>tr:has(:checked)',?this).addClass(options.selected);???? ?????????????????return?this;??//返回this,使方法可鏈。???? ?????????????}???? ?????????});???? ?????})(jQuery);???? ???????? ?????//插件應用???? ?????$(function(){???? ?????$("#remove").click(function(){?? ?????? ?? if($(":checkbox[name=choice]:checked").size()==0){?? alert("請選擇一行");?? return?false;?? }?? ?????});?? ??????$("#table2")???? ????????.alterBgColor()??//應用插件???? ????????.find("th").css("color","red");//可以鏈式操作???? ?????});???? ???????? ??</script>?? ????<base?href="<%=basePath%>">?? ?????? ????<title>My?JSP?'All.jsp'?starting?page</title>?? ?????? ????<meta?http-equiv="pragma"?content="no-cache">?? ????<meta?http-equiv="cache-control"?content="no-cache">?? ????<meta?http-equiv="expires"?content="0">?????? ????<meta?http-equiv="keywords"?content="keyword1,keyword2,keyword3">?? ????<meta?http-equiv="description"?content="This?is?my?page">?? ????<!--? ????<link?rel="stylesheet"?type="text/css"?href="styles.css">? ????-->?? ?? ??</head>?? ???? ??<body>?? ??<form?action="delete.action"?method="post">?? ???<table?id="table2">??? ???<tr><input?type="submit"?value="刪除"??id="remove"/></tr>??? ?????<thead><tr><th><input?type="checkbox"/></th><th>編號</th><th>姓名</th><th>班級</th></tr></thead>???? ?????<tbody>??? ?????<s:iterator?value="list">?? ??????<tr>???? ???????<td><input?type="checkbox"?name="choice"?value="<s:property?value='id'/>"/></td>???? ???????<td><s:property?value="id"/></td>???? ???????<td><s:property?value="sname"/></td>???? ???????<td><s:property?value="classes"/></td>???? ??????</tr>???? ??????</s:iterator>?? ??????</tbody>?? ??????</table>?? ??????<input?type="hidden"?value="<s:property?value='id'/>"?/>?? ??????</form>?? ??????<s:debug></s:debug>?? ??</body>?? </html>??
轉載于:https://my.oschina.net/heiyexue/blog/323057
總結
以上是生活随笔為你收集整理的struts2批量删除的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。