标签:
dwr2.0的jar包,还需要同时导入log4j.jar和commons-loggin.jar
首先是配置文件:
1 <!-- DWR配置 --> 2 <servlet> 3 <servlet-name>dwr-invoker</servlet-name> 4 <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> 5 6 <init-param> 7 <param-name>debug</param-name> 8 <param-value>true</param-value> 9 </init-param> 10 <init-param> 11 <param-name>crossDomainSessionSecurity</param-name> 12 <param-value>false</param-value> 13 </init-param> 14 <init-param> 15 <param-name>allowScriptTagRemoting</param-name> 16 <param-value>true</param-value> 17 </init-param> 18 </servlet> 19 <servlet-mapping> 20 <servlet-name>dwr-invoker</servlet-name> 21 <url-pattern>/dwr/*</url-pattern> 22 </servlet-mapping>
在web.xml的同级目录下建立dwr.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd"> 3 <dwr> 4 <allow> 5 <!-- javascript="DWRUserAccess" 表示提供给前台页面调用的javascript文件是DWRUserAccess.js 6 convert用于数据类型转换,即java类和javascript之间相互转换--> 7 <create creator="new" javascript="service" scope="application"> 8 <param name="class" value="helloWorld.Service"></param> 9 </create> 10 <create creator="new" javascript="DWRUserAccess"> 11 <param name="class" value="com.dwr.DWRUserAccess" /> 12 </create> 13 <convert converter="bean" match="com.dwr.User" /> 14 </allow> 15 </dwr>
user.java
1 package com.dwr; 2 3 public class User { 4 private String id; 5 private String name; 6 private String password; 7 private String email; 8 public String getName() { 9 return name; 10 } 11 public void setName(String name) { 12 this.name = name; 13 } 14 public String getPassword() { 15 return password; 16 } 17 public void setPassword(String password) { 18 this.password = password; 19 } 20 public String getId() { 21 return id; 22 } 23 public void setId(String id) { 24 this.id = id; 25 } 26 public String getEmail() { 27 return email; 28 } 29 public void setEmail(String email) { 30 this.email = email; 31 } 32 33 34 }
userDao.java
1 package com.dwr; 2 3 import java.util.HashMap; 4 import java.util.Map; 5 6 public class UserDAO { 7 //存放保存的数据 8 private static Map dataMap = new HashMap(); 9 //持久用户 10 public boolean save(User user){ 11 if (dataMap.containsKey(user.getId())) 12 return false; 13 System.out.println("下面开始保存用户"); 14 System.out.println("id:"+user.getId()); 15 System.out.println("password:"+user.getPassword()); 16 System.out.println("name:"+user.getName()); 17 System.out.println("email:"+user.getEmail()); 18 dataMap.put(user.getId(), user); 19 System.out.println("用户保存结束"); 20 return true; 21 } 22 //查找用户 23 public User find(String id) { 24 return (User)dataMap.get(id); 25 } 26 }
People.java
1 package com.dwr; 2 3 public class People { 4 public String getName(){ 5 return "guoyansi"; 6 } 7 }
DWRUserAccess.java
1 package com.dwr; 2 3 public class DWRUserAccess { 4 UserDAO userDAO = new UserDAO(); 5 6 public boolean save(User user) { 7 return userDAO.save(user); 8 } 9 10 public User find(String id) { 11 return userDAO.find(id); 12 } 13 }
test.jsp
1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>测试dwr</title> 13 <script src="dwr/engine.js"></script> 14 <script src="dwr/util.js"></script> 15 <script src="dwr/interface/DWRUserAccess.js"></script> 16 17 </head> 18 19 <body> 20 <B>用户注册</B> 21 <br> ------------------------------------------------ 22 <Br> 23 <form name="regForm"> 24 登陆ID:<input type="text" name="id"><br> 25 口 令:<input type="password" name="password"><br> 26 姓 名:<input type="text" name="name"><br> 27 电子邮件:<input type="text" name="email"><br> 28 <input type="button" name="submitBtn" value="提交" onclick="OnSave()"><br> 29 </form> 30 31 <br> 32 <br> 33 <B>用户查询</B> 34 <br> ------------------------------------------------ 35 <Br> 36 <form name="queryForm"> 37 登陆ID:<input type="text" name="id"><br> 38 <input type="button" name="submitBtn" value="提交" onclick="OnFind()"><br> 39 </form> 40 <br> 41 42 <script type="text/javascript"> 43 function saveFun(data) { 44 if (data) { 45 alert("注册成功!"); 46 } else { 47 alert("登陆ID已经存在!"); 48 } 49 } 50 51 function OnSave() { 52 var userMap = {}; 53 userMap.id = regForm.id.value; 54 userMap.password = regForm.password.value; 55 userMap.name = regForm.name.value; 56 userMap.email = regForm.email.value; 57 DWRUserAccess.save(userMap, saveFun); 58 } 59 60 function findFun(data) { 61 if (data == null) { 62 alert("无法找到用户:" + queryForm.id.value); 63 return; 64 } 65 66 alert("找到用户,nid:" + data.id + ",npassword:" + data.password 67 + ",nname:" + data.name + ",nemail:" + data.email); 68 } 69 70 function OnFind() { 71 DWRUserAccess.find(queryForm.id.value, findFun); 72 } 73 </script> 74 </body> 75 </html>
test1.jsp
1 <%@ page language="java" import="java.util.*" contentType="text/html; charset=utf-8"%> 2 <% 3 String path = request.getContextPath(); 4 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; 5 %> 6 7 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> 8 <html> 9 <head> 10 <base href="<%=basePath%>"> 11 12 <title>测试dwr</title> 13 <script type=‘text/javascript‘ src=‘dwr/engine.js‘></script> 14 <script type=‘text/javascript‘ src=‘dwr/util.js‘></script> 15 <script type=‘text/javascript‘ src=‘dwr/interface/service.js‘></script> 16 <script type="text/javascript"> 17 function firstDwr(){ 18 service.sayHello("jiuzi",callBackHello); 19 } 20 function callBackHello(data){ 21 alert(data); 22 } 23 </script> 24 25 </head> 26 27 <body> 28 <button onclick="firstDwr()">测试</button> 29 </body> 30 </html>
标签:
原文地址:http://www.cnblogs.com/guoyansi19900907/p/4527842.html