日期:2014-05-16  浏览次数:20361 次

json+ajax实现的增删改查

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
	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>
		<base href="<%=basePath%>">

		<title>My JSP 'emp.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">
	-->

		<script type="text/javascript" src="./js/util.js">
</script>
		<script>
window.onload = function() {
	document.getElementById("addemp").style.display = "none";
}
var pageinfo;

function findAll(nowpage) {
	//获取xhr对象
	var xhr = getXhr();
	//封装请求信息
	xhr.open("get", "./listEmp.do?nowpage=" + nowpage, true);

	//发送请求
	xhr.send();

	//相应
	xhr.onreadystatechange = function() {
		if (xhr.readyState == 4 && xhr.status == 200) {

			var jsonResponse = eval("(" + xhr.responseText + ")");

			//获取写入的tbody的DOM对象
			cha(jsonResponse);

		}
	}
}

function cha(jsonResponse) {
	var listemps = $("listemps");
	listemps.innerHTML = "";
	for ( var i = 0; i < jsonResponse.length; i++) {
		var emptr = document.createElement("tr");

		var empno = document.createElement("td");
		var ename = document.createElement("td");
		var job = document.createElement("td");
		var hiredate = document.createElement("td");
		var mgr = document.createElement("td");
		var sal = document.createElement("td");
		var comm = document.createElement("td");
		var deptno = document.createElement("td");
		var cz = document.createElement("td");
		empno.appendChild(document.createTextNode(jsonResponse[i].empno));
		ename.appendChild(document.createTextNode(jsonResponse[i].ename));
		job.appendChild(document.createTextNode(jsonResponse[i].job));
		var a = (jsonResponse[i].hiredate.year + 1900) + "年"
				+ (jsonResponse[i].hiredate.month + 1) + "月"
				+ jsonResponse[i].hiredate.date + "日";
		hiredate.appendChild(document.createTextNode(a));
		mgr.appendChild(document.createTextNode(jsonResponse[i].mgr));
		sal.appendChild(document.createTextNode(jsonResponse[i].sal));
		comm.appendChild(document.createTextNode(jsonResponse[i].comm));
		deptno.appendChild(document.createTextNode(jsonResponse[i].deptno));
		var udmp = document.createElement("input");
		var delmp = document.createElement("input");
		delmp.setAttribute("type", "button");
		udmp.setAttribute("type", "button");
		delmp.setAttribute("value", "删除");
		udmp.setAttribute("value", "编辑");

		emptr.appendChild(empno);
		emptr.appendChild(ename);
		emptr.appendChild(job);
		emptr.appendChild(hiredate);
		emptr.appendChild(mgr);
		emptr.appendChild(sal);
		emptr.appendChild(comm);
		emptr.appendChild(deptno);
		cz.appendChild(udmp);
		cz.appendChild(delmp);
		emptr.appendChild(cz);
		listemps.appendChild(emptr);

		delmp.onclick = function() {
			if (window.confirm("您确定删除??")) {
				var empno = this.parentNode.parentNode.firstChild.innerHTML;
				listemps.innerHTML = "";
				var xhr = getXhr();
				//封装请求信息
				xhr.open("get", "./DelEmp.do?empno=" + empno, true);

				//发送请求
				xhr.send();

				//相应
				xhr.onreadystatechange = function() {
					if (xhr.readyState == 4 && xhr.status == 200) {
						var jsonResponse = eval("(" + xhr.responseText + ")");

						//获取写入的tbody的DOM对象
						cha(jsonResponse);
					}
				}
			}

		}

		udmp.onclick = function() {
			document.getElementById("addemp").style.display = "block";
			document.getElementById("a").style.display = "none";
			document.getElementById("b