在WEB-INFO下新建dwr.xml文件。配置我们要通过dwr使用的Java类。
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd"> <dwr> <allow> <create creator="spring" javascript="demoDwr" scope="application"> <param name="beanName" value="mydemoDwr"/> <include method="saveClass"/> <include method="updateClass"/> <include method="saveStudent"/> <include method="findAllClass"/> <include method="findStudentByClassId"/> </create> <convert converter="bean" match="com.inspur.mydemo.domain.Student"> <param name="include" value="id,name,sex,age,birthday,cla"/> </convert> <convert converter="bean" match="com.inspur.mydemo.domain.Class"> <param name="include" value="id,name"/> </convert> </allow> </dwr>
在WEB-INFO下的web.xml力配置dwr和Sping、Hibernate。
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <filter-name>hibernateFilter</filter-name> <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class> </filter> <filter-mapping> <filter-name>hibernateFilter</filter-name> <url-pattern>*.do</url-pattern> </filter-mapping> <!-- 字符过滤器 --> <filter> <filter-name>encodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>GBK</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>*.htm</url-pattern> </filter-mapping> <!-- dwr --> <servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class> org.directwebremoting.servlet.DwrServlet </servlet-class> <init-param> <param-name>classes</param-name> <param-value>java.lang.Object</param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>WEB-INF/dwr.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping> <!-- 初始化spring context --> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/classes/applicationContext.xml,/WEB-INF/classes/demo-service.xml </param-value> </context-param> <context-param> <param-name>extremecomponentsPreferencesLocation</param-name> <param-value>/extremetable.properties</param-value> </context-param> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
在WebRoot里新建一个文件夹jsp和js
在jsp里新建student.jsp和js里新建student.js。
<%@ page contentType="text/html;charset=utf-8"%> <%@ taglib prefix="c" uri="http://java.sun.com/jstl/core_rt"%> <c:set var="ctx" value="${pageContext.request.contextPath}" /> <!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> <script type='text/javascript' src='${ctx }/dwr/interface/demoDwr.js'></script> <script type='text/javascript' src='${ctx }/dwr/engine.js'></script> <script type='text/javascript' src='${ctx }/dwr/util.js'></script> <script type='text/javascript' src='${ctx }/js/student.js'></script> </head> <body onload="init()"> <table border="0" > <tr height="250"> <td> <table border="0" > <tr><td>班级列表</td></tr> <tr> <td id="classContentDiv"> <select name="classContent" id="classId" size="10" onchange="loadStudentByClass()" ondblclick="updateInit()"> <option value="-1">请选择 </option> </select> </td> </tr> </table> </td> <td> <table border="0" > <tr><td>学生列表</td></tr> <tr> <td id="studentContentDiv"> </td> </tr> </table> </td> </tr> </table> <!---body---> <div> <span> <input type="button" value="添加新班级" onclick="javascript:newClass();" style="cursor:hand;"/> </span> </div> <div id="classForm" style="display:none;"> <div align="center"> <span>输入班级名称</span> <span><input type="text" name="newClassName" id="newClassName" /></span> <span><input type="button" value="保存" onclick="saveClass()" style="cursor:hand;"/></span> </div> </div> <div align="center"> <div id="updateClassForm" style="display:none;"> <span><input type="hidden" name="oldClassId" id="oldClassId" /></span> <span>班级名称</span> <span> <input type="text" name="oldClassName" id="oldClassName" /> <input type="button" value="修改" onclick="updateClass()"/> </span> </div> </div> <div id="studentForm"> <div> <span>姓名:</span> <input type="text" name="newStudentName" id="newStudentName" /> </div> <div> <span>年龄:</span> <input type="text" name="newStudentAge" id="newStudentAge" /> </div> <div> <span>性别:</span> <select name="newStudentSex" id="newStudentSex"> <option value="-1">请选择性别</option> <option value="男">男</option> <option value="女">女</option> </select> </div> <div> <span>生日:</span> <input type="text" name="newStudentBirthday" id="newStudentSex" /> </div> <div> <input type="button" value="保存" onclick="saveStudent()" /> </div> </div> <br clear="all" /> <!---主体结束---> </body> </html>
function init() { loadClass(); } function loadClass() { $("classForm").style.display = "none"; $("updateClassForm").style.display = "none"; demoDwr.findAllClass(loadClassCallBack); } function loadClassCallBack(data) { var str = ""; if (DWRUtil._isArray(data)) { DWRUtil.removeAllOptions("classId"); DWRUtil.addOptions("classId", data, "id", "name"); } else { str = "没有这个班级"; } loadStudentByClass(); } function loadStudentByClass() { var ClassId = $("classId").value; //alert(ClassId); if (ClassId == null || ClassId == -1 || ClassId == "") { $("classId").options[0].selected = true; ClassId = $("classId").options[0].value; } //alert(classId); demoDwr.findStudentByClassId(ClassId, loadStudentByClassCallBack); } function loadStudentByClassCallBack(data) { var str = ""; if (DWRUtil._isArray(data)) { for (var i = 0; i < data.length; i++) { var student = data[i]; var sex = student.sex; str += "<ul>" + "<li>姓名-->" + student.name + "</li><li>年龄-->" + student.age + "</li><li>性别-->"+sex+"</li><li>生日-->" +student.birthday+"</li></ul>"; } } else { str = "该班级里没有学生"; } var studentList = document.getElementById("studentContentDiv"); studentList.innerHTML = str; } function newClass() { $("classForm").style.display = ""; } function saveClass() { var name = $("newClassName").value; if (name == "" || name == null) { alert("班级名称不能为空"); return; } demoDwr.saveClass(name, saveClassCallBack); } function saveClassCallBack(data) { if (data == 0) { alert("\u6dfb\u52a0\u6210\u529f\uff01"); $("classForm").style.display = "none"; $("newClassName").value = ""; loadClass(); return; } else { alert("保存成功!"); } } function updateInit() { $("updateClassForm").style.display = ""; var classId = $("classId").value; $("oldClassName").value = DWRUtil.getText('classContent') ; } function updateClass() { var classId = $("classId").value; var name = $("oldClassName").value; //alert(classId); //alert(name); if(!classId.isValidInteger()) { alert("该班级不能被修改!因为找不到这样的班级。"); return; } if(name==""){ alert("班级名称不能为空!"); return; } demoDwr.updateClass(classId,name,updateClassCallBack); } function updateClassCallBack(data){ if (data == 0) { alert("更新成功!"); $("updateClassForm").style.display = "none"; $("oldClassName").value = ""; loadClass(); return; } else { alert("更新出错!"); } } function saveStudent() { var classId = $("classId").value; var name = $("newStudentName").value; var age = $("newStudentAge").value; //alert(age); var sex = $("newStudentSex").value; var birthday = $("newStudentBirthday").value; if (name == "") { alert("姓名不能为空!"); return; } if (!age.isValidInteger()) { alert("输入的年龄不是数字!"); return; } if (birthday == ""){ alert("生日不能为空"); return; } var stu ={ name:null, age:null, sex:null, birthday:null } stu.name=name; stu.age=age; stu.sex=sex; stu.birthday = birthday; demoDwr.saveStudent(stu,classId,saveStudentCallback); //demoDwr.saveStudent(name, age, sex ,birthday,classId, saveStudentCallback); } String.prototype.isValidInteger=function() { var result=this.match(/^(-|\+)?\d+$/); if(result==null) return false; return true; } function saveStudentCallback(data) { if(data=="0"){ alert("保存学生成功!"); }else if(data=="1"){ alert("保存学生出错,不含学生信息!"); }else if(data=="2"){ alert("没有当前这个班级、可能已经删除!"); }else if(data=="-1"){ alert("保存学生出错!"); } loadStudentByClass(); $("newStudentName").value = ""; $("newStudentAge").value = ""; $("newStudentSex").value = ""; $("newStudentBirthday").value = ""; }