dwr2.0的jar包,还需要同时导入log4j.jar和commons-loggin.jar
首先是配置文件:
<!-- DWR配置 -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class> <init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>crossDomainSessionSecurity</param-name>
<param-value>false</param-value>
</init-param>
<init-param>
<param-name>allowScriptTagRemoting</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
在web.xml的同级目录下建立dwr.xml
<?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>
<!-- javascript="DWRUserAccess" 表示提供给前台页面调用的javascript文件是DWRUserAccess.js
convert用于数据类型转换,即java类和javascript之间相互转换-->
<create creator="new" javascript="service" scope="application">
<param name="class" value="helloWorld.Service"></param>
</create>
<create creator="new" javascript="DWRUserAccess">
<param name="class" value="com.dwr.DWRUserAccess" />
</create>
<convert converter="bean" match="com.dwr.User" />
</allow>
</dwr>
user.java
package com.dwr; public class User {
private String id;
private String name;
private String password;
private String email;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
} }
userDao.java
package com.dwr; import java.util.HashMap;
import java.util.Map; public class UserDAO {
//存放保存的数据
private static Map dataMap = new HashMap();
//持久用户
public boolean save(User user){
if (dataMap.containsKey(user.getId()))
return false;
System.out.println("下面开始保存用户");
System.out.println("id:"+user.getId());
System.out.println("password:"+user.getPassword());
System.out.println("name:"+user.getName());
System.out.println("email:"+user.getEmail());
dataMap.put(user.getId(), user);
System.out.println("用户保存结束");
return true;
}
//查找用户
public User find(String id) {
return (User)dataMap.get(id);
}
}
People.java
package com.dwr; public class People {
public String getName(){
return "guoyansi";
}
}
DWRUserAccess.java
package com.dwr; public class DWRUserAccess {
UserDAO userDAO = new UserDAO(); public boolean save(User user) {
return userDAO.save(user);
} public User find(String id) {
return userDAO.find(id);
}
}
test.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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>测试dwr</title>
<script src="dwr/engine.js"></script>
<script src="dwr/util.js"></script>
<script src="dwr/interface/DWRUserAccess.js"></script> </head> <body>
<B>用户注册</B>
<br> ------------------------------------------------
<Br>
<form name="regForm">
登陆ID:<input type="text" name="id"><br>
口 令:<input type="password" name="password"><br>
姓 名:<input type="text" name="name"><br>
电子邮件:<input type="text" name="email"><br>
<input type="button" name="submitBtn" value="提交" onclick="OnSave()"><br>
</form> <br>
<br>
<B>用户查询</B>
<br> ------------------------------------------------
<Br>
<form name="queryForm">
登陆ID:<input type="text" name="id"><br>
<input type="button" name="submitBtn" value="提交" onclick="OnFind()"><br>
</form>
<br> <script type="text/javascript">
function saveFun(data) {
if (data) {
alert("注册成功!");
} else {
alert("登陆ID已经存在!");
}
} function OnSave() {
var userMap = {};
userMap.id = regForm.id.value;
userMap.password = regForm.password.value;
userMap.name = regForm.name.value;
userMap.email = regForm.email.value;
DWRUserAccess.save(userMap, saveFun);
} function findFun(data) {
if (data == null) {
alert("无法找到用户:" + queryForm.id.value);
return;
} alert("找到用户,nid:" + data.id + ",npassword:" + data.password
+ ",nname:" + data.name + ",nemail:" + data.email);
} function OnFind() {
DWRUserAccess.find(queryForm.id.value, findFun);
}
</script>
</body>
</html>
test1.jsp
<%@ page language="java" import="java.util.*" contentType="text/html; charset=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>测试dwr</title>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script type='text/javascript' src='dwr/interface/service.js'></script>
<script type="text/javascript">
function firstDwr(){
service.sayHello("jiuzi",callBackHello);
}
function callBackHello(data){
alert(data);
}
</script> </head> <body>
<button onclick="firstDwr()">测试</button>
</body>
</html>