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>
时间: 2024-12-31 05:46:45