一、项目部署
1.Eclipse创建一个Dynamic Web Project
2.在src里面创建1个Package(com.airui),该包下面有5个子包 (dao,domain,service,servlet,utils),在src里导入c3p0-config.xml,如图下:
3.dao、domain、service里创建class,servlet里创建servlet,utils我是直接导入了一个封装好的jar包,如图下:
4.lib里导入一些jar包,WebContent里创建jsp文件、导入jq.js
5.重要的一步部署Tomcat环境
链接: https://pan.baidu.com/s/1nH6PVG4uaZbstJlxjf4ixA 提取码: 84as (项目源码+Tomcat7.0+jar包+连接池)
6.创建一个emp数据库
注:字段要与domain里的Emp实体类的字段一致
二.Dao层
//EmpDao
package com.airui.dao;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.airui.domain.Emp;
import com.airui.domain.PageBean;
public class EmpDao {
public List<Emp> show(PageBean pb,QueryRunner qr) {
//sql语句
String sql="select * from emp limit ?,?;";
/*
* Select * from xx limit x,y;
* X:(page-1)*tr
* Y:tr
* */
//要一一对应sql语句中的每一个'?'
Object[] o= {(pb.getPage()-1)*pb.getTr(),pb.getTr()};
try {
//用集合封装数据
List<Emp> list=qr.query(sql, new BeanListHandler<Emp>(Emp.class),o);
return list;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public void update(Emp e, QueryRunner qr) {
//sql语句
String sql="update emp set name=?,pwd=?,age=?,sex=?,dept=?,addr=?,birthday=? where id=?;";
//要一一对应sql语句中的每一个'?'
Object[] o= {e.getName(),e.getPwd(),e.getAge(),e.getSex(),e.getDept(),e.getAddr(),e.getBirthday(),e.getId()};
try {
qr.update(sql, o);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public Emp findbyid(int id, QueryRunner qr) {
//sql语句
String sql="select * from emp where id=?;";
try {
//返回一个Emp对象,'?'对应id
Emp e=qr.query(sql, new BeanHandler<Emp>(Emp.class), id);
return e;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public void delete(int id, QueryRunner qr) {
//sql语句
String sql="delete from emp where id=?;";
try {
//'?'所对应的id
qr.update(sql,id);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void add(Emp e, QueryRunner qr) {
//sql语句
String sql="insert into emp values(null,?,?,?,?,?,?,?);";
//'?'所对应的值
Object[] o= {e.getName(),e.getPwd(),e.getAge(),e.getSex(),e.getDept(),e.getAddr(),e.getBirthday()};
try {
qr.update(sql, o);
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public int count(QueryRunner qr) {
//sql语句
String sql="select count(*) from emp;";
try {
//ScalarHandler:将单个值封装、例如select count(*),求内容的条数
//如果查出结果为空,返回0,不是null
Object o=qr.query(sql, new ScalarHandler());
return Integer.parseInt(o.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
public void deleteall(String[] ids, QueryRunner qr) {
//sql语句
String sql="delete from emp where id=?;";
// 由于dbutils工具类的批量删除操作,需要的是二维数组来存放给?赋值的一维数组
// 必须创建一个二维数组,这个二维数组的大小,就是一维数组的个数
Object[][] arr=new Object[ids.length][];
for (int i = 0; i < ids.length; i++) {
Object arr1[]={ids[i]};
arr[i] = arr1;
}
try {
//批量删除的方法,需要传入一个二维数组
qr.batch(sql, arr);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public List<Emp> selname(String selname, String sex, PageBean pb, QueryRunner qr) {
String sql="select * from emp where name like ? and sex=? limit ?,?;";
//'?'所对应的值
Object[]o= {"%"+selname+"%",sex,(pb.getPage()-1)*pb.getTr(),pb.getTr()};
try {
return qr.query(sql, new BeanListHandler<Emp>(Emp.class),o);
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public int count1(String selname, String sex, QueryRunner qr) {
//sql语句
String sql="select count(*) from emp where name like ? and sex=?;";
//两个'?'所对应的值
Object []o= {"%"+selname+"%",sex};
try {
//ScalarHandler:将单个值封装、例如select count(*),求内容的条数
//如果查出结果为空,返回0,不是null
Object o1=qr.query(sql, new ScalarHandler(),o);
return Integer.parseInt(o1.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return 0;
}
}
三.Domain层
//Emp实体类
package com.airui.domain;
import java.util.Date;
public class Emp {
private Integer id;
private String name;
private String pwd;
private Integer age;
private String sex;
private String dept;
private String addr;
private Date birthday;
public Emp() {
super();
// TODO Auto-generated constructor stub
}
public Emp(Integer id, String name, String pwd, Integer age, String sex, String dept, String addr, Date birthday) {
super();
this.id = id;
this.name = name;
this.pwd = pwd;
this.age = age;
this.sex = sex;
this.dept = dept;
this.addr = addr;
this.birthday = birthday;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getDept() {
return dept;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getAddr() {
return addr;
}
public void setAddr(String addr) {
this.addr = addr;
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
@Override
public String toString() {
return "Emp [id=" + id + ", name=" + name + ", pwd=" + pwd + ", age=" + age + ", sex=" + sex + ", dept=" + dept
+ ", addr=" + addr + ", birthday=" + birthday + "]";
}
}
//PageBean(分页)
package com.airui.domain;
import java.util.List;
public class PageBean {
private int tr; //每页要显示数据的个数
private int trsum; //一共有多少条数据
private int page; //当前是第几页,用户前端页面,传给后台的
/*
* 用法:Math.ceil(double x)
* 功能:返回大于或者等于指定表达式的最小整数,即向上取整
* 用例:写分页的时候要根据查到的记录总数count和
* 每页数据数roots,
* 求出总共有多少页。这里都定义为int类型的
* */
private int pagesum; //总页码,Matn.ceil(trsum/tr)
private List<Emp> list; //当前页的数据用list集合装起来
public PageBean() {
super();
// TODO Auto-generated constructor stub
}
public PageBean(int tr, int trsum, int page, int pagesum, List<Emp> list) {
super();
this.tr = tr;
this.trsum = trsum;
this.page = page;
this.pagesum = pagesum;
this.list = list;
}
public int getTr() {
return tr;
}
public void setTr(int tr) {
this.tr = tr;
}
public int getTrsum() {
return trsum;
}
public void setTrsum(int trsum) {
this.trsum = trsum;
}
public int getPage() {
return page;
}
public void setPage(int page) {
this.page = page;
}
public int getPagesum() {
return pagesum;
}
public void setPagesum(int pagesum) {
this.pagesum = pagesum;
}
public List<Emp> getList() {
return list;
}
public void setList(List<Emp> list) {
this.list = list;
}
@Override
public String toString() {
return "PageBean [tr=" + tr + ", trsum=" + trsum + ", page=" + page + ", pagesum=" + pagesum + ", list=" + list
+ "]";
}
}
四.Service层
//EmpService
package com.airui.service;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import com.airui.dao.EmpDao;
import com.airui.domain.Emp;
import com.airui.domain.PageBean;
import com.airui.utils.JDBCUtils;
public class EmpService {
EmpDao ed=new EmpDao();
QueryRunner qr=JDBCUtils.getQR();
//展示
public List<Emp> show(PageBean pb) {
//调用EmpDao的show方法
List<Emp> list=ed.show(pb,qr);
return list;
}
//修改
public void update(Emp e) {
//调用EmpDao的update方法
ed.update(e,qr);
}
//根据id查找(修改和删除必须通过该方法)
public Emp findbyid(int id) {
//调用EmpDao的findbyid方法
return ed.findbyid(id,qr);
}
//删除
public void delete(int id) {
//调用EmpDao的delete方法
ed.delete(id,qr);
}
//添加
public void add(Emp e) {
//调用EmpDao的add方法
ed.add(e,qr);
}
//计算总数
public int count() {
//调用EmpDao的count方法
return ed.count(qr);
}
//删除选中的数据
public void deleteall(String[] ids) {
//调用EmpDao的deleteall方法
ed.deleteall(ids,qr);
}
public List<Emp> selename(String selname, String sex, PageBean pb) {
return ed.selname(selname,sex,pb,qr);
}
public int count1(String selname, String sex) {
return ed.count1(selname,sex,qr);
}
}
五.Servlet层
//Show
package com.airui.servlet;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.airui.domain.Emp;
import com.airui.domain.PageBean;
import com.airui.service.EmpService;
@WebServlet("/Show")
public class Show extends HttpServlet {
private static final long serialVersionUID = 1L;
public Show() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理中文乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//创建EmpService层对象
EmpService es = new EmpService();
PageBean pb = new PageBean();
//每页有多少条数据
int tr=3;
pb.setTr(tr);
//一共有多少条数据
int trsum=es.count();
pb.setTrsum(trsum);
//一共有多少页
int pagesum=(int)Math.ceil(trsum*1.0/tr);
pb.setPagesum(pagesum);
//当前页数据
int page=1;
String p=request.getParameter("page");
if (p!=null) {
page=Integer.parseInt(p);
}
//setAttribute:获取当前页的page
request.getSession().setAttribute("page", page);
pb.setPage(page);
//每页的所有数据
List<Emp> list=es.show(pb);
pb.setList(list);
//向前端服务器发送数据
request.setAttribute("pb", pb);
//发送到需要展示的界面'相当于'跳转到展示界面
request.getRequestDispatcher("/show.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
//Add
package com.airui.servlet;
import java.io.IOException;
import java.sql.Date;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import com.airui.domain.Emp;
import com.airui.service.EmpService;
@WebServlet("/Add")
public class Add extends HttpServlet {
private static final long serialVersionUID = 1L;
public Add() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理中文乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html,charset=utf-8");
//创建EmpService层对象
EmpService es = new EmpService();
//获取Emp表中字段的数据:request.getParameter("a"),a->show.jsp里面的input框的name属性值,即name="a";
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
String age=request.getParameter("age");
String sex=request.getParameter("sex");
//将获取的部门信息放入一个一位数组里
String[] depts=request.getParameterValues("dept");
String dept="";
if (depts!=null) {
//格式转换:array->string;以','分割
dept=StringUtils.join(depts,",");
}
String addr=request.getParameter("addr");
String birthday=request.getParameter("birthday");
//Date.valueOf方法是将字符串转换成date格式
Emp e = new Emp(null,name,pwd,Integer.parseInt(age),sex,dept,addr,Date.valueOf(birthday));
//调用Empservice中的add方法
es.add(e);
//将添加后的新数据发送到Servlet的Show中,再通过Show跳到show.jsp进行展示
request.getRequestDispatcher("Show").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
doGet(request, response);
}
}
//Delete
package com.airui.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.airui.service.EmpService;
@WebServlet("/Delete")
public class Delete extends HttpServlet {
private static final long serialVersionUID = 1L;
public Delete() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理中文乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//创建EmpService层对象
EmpService es = new EmpService();
//获取Emp表中字段的数据:request.getParameter("a"),a->show.jsp里面的input框的name属性值,即name="a";
String i=request.getParameter("id");
int id=Integer.parseInt(i);
//getAttribute:得到删除后之前的page(Show里的request.getSession().setAttribute("page", page))
Object page1=request.getSession().getAttribute("page");
int page=Integer.parseInt(page1.toString());
//调用Empservice中的delete方法
es.delete(id);
//将删除后的新数据发送到Servlet的Show中,再通过Show跳到show.jsp进行展示
request.getRequestDispatcher("Show?page="+page).forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
//Update
package com.airui.servlet;
import java.io.IOException;
import java.sql.Date;
import java.util.Arrays;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import com.airui.domain.Emp;
import com.airui.domain.PageBean;
import com.airui.service.EmpService;
@WebServlet("/Update")
public class Update extends HttpServlet {
private static final long serialVersionUID = 1L;
public Update() {
super();
// TODO Auto-generated constructor stub
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理中文乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
//创建EmpService层对象
EmpService es = new EmpService();
//获取数据
String i=request.getParameter("id");
int id=Integer.parseInt(i);
String name=request.getParameter("name");
String pwd=request.getParameter("pwd");
String age=request.getParameter("age");
String sex=request.getParameter("sex");
String[] depts=request.getParameterValues("dept");
String dept="";
if (depts!=null) {
//格式转换:array->string,以','分割
dept=StringUtils.join(depts,",");
}
String addr=request.getParameter("addr");
String birthday=request.getParameter("birthday");
//格式转换:string->date
Emp e = new Emp(id,name,pwd,Integer.parseInt(age),sex,dept,addr,Date.valueOf(birthday));
//调用Empservice中的update方法
es.update(e);
//getAttribute:得到更新后之前的page(Show里的request.getSession().setAttribute("page", page))
Object page1=request.getSession().getAttribute("page");
int page=Integer.parseInt(page1.toString());
//转发("Show?page="+page:留在当前页)
request.getRequestDispatcher("Show?page="+page).forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
//FindById(增删需要根据id)
package com.airui.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.airui.domain.Emp;
import com.airui.service.EmpService;
@WebServlet("/FindByID")
public class FindByID extends HttpServlet {
private static final long serialVersionUID = 1L;
public FindByID() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理中文乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html,charset=utf-8");
//创建EmpService层对象
EmpService es = new EmpService();
//获取数据
String i=request.getParameter("id");
int id=Integer.parseInt(i);
//调用Empservice中的findbyid方法
Emp e=es.findbyid(id);
//传送数据
request.setAttribute("e", e);
//转发
request.getRequestDispatcher("/update.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
//DeleteAll(批量删除)
package com.airui.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.airui.service.EmpService;
@WebServlet("/DeleteAll")
public class DeleteAll extends HttpServlet {
private static final long serialVersionUID = 1L;
public DeleteAll() {
super();
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理中文乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html,charset=utf-8");
//获取数据:request.getParameterValues("a"),a->show.jsp里面的input框的name属性值,即name="a";
String[] ids=request.getParameterValues("ids");
EmpService es = new EmpService();
if (ids!=null) {
es.deleteall(ids);
}
//转发
request.getRequestDispatcher("Show").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
//SelName(根据姓名和性别同时查询)
package com.airui.servlet;
import java.io.IOException;
import java.sql.Date;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import com.airui.domain.Emp;
import com.airui.domain.PageBean;
import com.airui.service.EmpService;
@WebServlet("/SelName")
public class SelName extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//处理中文乱码
request.setCharacterEncoding("utf-8");
response.setContentType("text/html,charset=utf-8");
//创建EmpService层对象
EmpService es = new EmpService();
//获取数据
String selname=request.getParameter("selname");
String sex=request.getParameter("sex");
PageBean pb = new PageBean();
int tr=3;
pb.setTr(tr);
//通过模糊查查出来的数据的个数
int trsum=es.count1(selname,sex);
pb.setTrsum(trsum);
//一共有多少页
int pagesum=(int)Math.ceil(trsum*1.0/tr);
pb.setPagesum(pagesum);
//当前页数据
int page=1;
String p=request.getParameter("page");
if (p!=null) {
page=Integer.parseInt(p);
}
pb.setPage(page);
//调用Empservice中的selename方法
List<Emp> list=es.selename(selname,sex,pb);
pb.setList(list);
//System.out.println(list);
request.setAttribute("pb", pb);
request.getRequestDispatcher("/show.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
六.Jsp
//show.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body background="cc.jpeg">
<h1 align="center"><font size="7">在线信息管理系统</font></h1>
<div align="center">
<!-- window.location方法可以链接到其他页面 -->
<button onclick="window.location='/work/add.jsp'">添加</button>
<form action="/work/SelName" method="post">
姓名:<input type="text" name="selname"><br>
性别:<input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女<br>
<input type="submit" value="根据姓名和性别查询">
</form>
<!-- <form action="/work/SelAge" method="post">
年龄:<input type="text" name="selage1">~<input type="text" name="selage2">
<input type="submit" value="查询">
</form> -->
<!-- post提交到Servlet中DeleteAll -->
<form action="/work/DeleteAll" method="post" >
<input type="submit" id="del" value="批量删除">
<table border="1" bordercolor="coral">
<tr>
<td><input type="checkbox" id="all">全选/全不选</td>
<td>编号</td>
<td>姓名</td>
<td>密码</td>
<td>年龄</td>
<td>性别</td>
<td>部门</td>
<td>地址</td>
<td>生日</td>
<td>操作</td>
</tr>
<c:forEach items="${pb.list }" var="e">
<tr>
<td><input type="checkbox" name="ids" value="${e.id }"></td>
<td>${e.id }</td>
<td>${e.name }</td>
<td>${e.pwd }</td>
<td>${e.age }</td>
<td>${e.sex }</td>
<td>${e.dept }</td>
<td>${e.addr }</td>
<td>${e.birthday }</td>
<td>
<a href="/work/FindByID?id=${e.id }">修改</a>
<a href="/work/Delete?id=${e.id }">删除</a>
</td>
</tr>
</c:forEach>
</table>
</form>
<!-- 首页直接显示page=1,默认为1,所以这边可以不用写/work/Show?page=1-->
<a href="/work/Show">首页</a>
<!-- 想要显示"上一页",必须保证当前页pb.page不等于1,
同时'上一页的页码'='当前页的页码'-1才能显示'上一页'
若当前页pb.page等于1时,就没有"上一页"了 -->
<c:if test="${pb.page!=1 }">
<a href="/work/Show?page=${pb.page-1 }">上一页</a>
</c:if>
<!-- 中间显示:当前页/总页码 例:1/3 -->
${pb.page }/${pb.pagesum }
<!-- 想要显示"下一页",必须保证当前页pb.page不等于最后一页pb.pagesum,
同时'下一页的页码'='当前页的页码'+1才能显示'下一页'
若当前页pb.page等于pb.pagesum时,就没有"下一页"了 -->
<c:if test="${pb.page!=pb.pagesum }">
<a href="/work/Show?page=${pb.page+1 }">下一页</a>
</c:if>
<!-- 首页直接显示page='最后一页' -->
<a href="/work/Show?page=${pb.pagesum }">尾页</a>
<input type="button" id="btn" value="退出">
</div>
</body>
<script type="text/javascript" src="jq.js"></script>
<script type="text/javascript">
/* all是全选/全不选多选框的id属性值,change是checkbox多选框被'✔'时即被选中 */
$("#all").change(function(){
/* 如果当前的多选按钮的,checked属性,是checked,表示,被选中
attr()方法来获取和设置元素属性 */
if($(this).attr("checked")=="checked"){
$("input[type='checkbox']").attr("checked",true);
}else{
$("input[type='checkbox']").attr("checked",false);
}
});
$("#del").click(function(){
alert("删除成功");
});
$("#btn").click(function(){
var flag=confirm("请选择确认或取消");
if (flag) {
window.close();
}
});
</script>
</html>
//update.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- post提交到Servlet中Update -->
<form action="/work/Update" method="post">
<!-- hidden:显示的id被隐藏了 -->
<%-- 数据的回显:${e.id }--当点击修改按钮时会显示出原有的数据 --%>
<input type="hidden" name="id" value="${e.id }">
姓名:<input type="text" name="name" value="${e.name }"><br>
密码:<input type="password" name="pwd" value="${e.pwd }"><br>
年龄:<input type="text" name="age" value="${e.age }"><br>
性别:<input type="radio" name="sex" value="男" <c:if test="${e.sex=='男' }">checked</c:if>>男
<input type="radio" name="sex" value="女" <c:if test="${e.sex=='女' }">checked</c:if>>女<br>
部门:<input type="checkbox" name="dept" class="dept" value="流沙" <c:if test="${e.dept.contains('流沙') }">checked</c:if>>流沙
<input type="checkbox" name="dept" class="dept" value="墨家" <c:if test="${e.dept.contains('墨家') }">checked</c:if>>墨家
<input type="checkbox" name="dept" class="dept" value="天网" <c:if test="${e.dept.contains('天网') }">checked</c:if>>天网
<input type="checkbox" name="dept" class="dept" value="影密卫" <c:if test="${e.dept.contains('影密卫') }">checked</c:if>>影密卫
<input type="checkbox" name="dept" class="dept" value="农家" <c:if test="${e.dept.contains('农家') }">checked</c:if>>农家<br>
住址:<input type="text" name="addr" value="${e.addr }"><br>
生日:<input type="date" name="birthday" value="${e.birthday }"><br>
<input type="submit" id="modify" value="修改" >
</form>
</body>
<script type="text/javascript" src="jq.js"></script>
<script type="text/javascript">
$("#modify").click(function(){
alert("修改成功");
});
</script>
</html>
//add.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- post提交到Servlet中的Add -->
<form action="/work/Add" method="post">
姓名:<input type="text" name="name"><br>
密码:<input type="password" name="pwd"><br>
年龄:<input type="text" name="age"><br>
性别:<input type="radio" name="sex" value="男">男
<input type="radio" name="sex" value="女">女<br>
部门:<input type="checkbox" name="dept" class="dept" value="流沙">流沙
<input type="checkbox" name="dept" class="dept" value="墨家">墨家
<input type="checkbox" name="dept" class="dept" value="天网">天网
<input type="checkbox" name="dept" class="dept" value="影密卫">影密卫
<input type="checkbox" name="dept" class="dept" value="农家">农家<br>
地址:<input type="text" name="addr"><br>
生日:<input type="date" name="birthday"><br>
<input type="submit" id="add" value="添加">
</form>
</body>
<script type="text/javascript" src="jq.js"></script>
<script type="text/javascript">
$("#add").click(function(){
alert("添加成功");
});
</script>
</html>