1.import jars
commons-logging-1.0.4.jar
dwr.jar
2.web.xml
<!-- 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>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
3.DwrDemo.java
public String getHello(String username){
System.out.println("DwrDemo getHello()......");
return "Hello "+username;
}
UserDao.java
private static Map<Integer,User> dataMap=new HashMap<Integer,User>();
public boolean save(User user){
System.out.println("*****************************");
System.out.println("UserDao save()........");
System.out.println("保存用户信息:");
System.out.println("user.username: "+user.getUsername());
System.out.println("usr.password: "+user.getPassword());
dataMap.put(1,user);
return true;
}
public User getUser(String id){
return (User)dataMap.get(1);
}
User.java
public String username;
public String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
4.dwr.xml
<?xml version="1.0" encoding="UTF-8"?>
<dwr>
<allow>
<!--配置的一个演示类,javascript="Demo" 表示可以在页面中用Demo这个名称指向DwrDemo这个java类,类中的方法可以在前台调用 -->
<!-- creater="new"表示每调用一次时,都需要new一个 -->
<create creator="new" javascript="Demo">
<param name="class" value="com.yue.test1.dwr.services.DwrDemo" />
</create>
<create creator="new" javascript="MyDate">
<param name="class" value="java.util.Date" />
</create>
<create creator="new" javascript="UserDao">
<param name="class" value="com.yue.test1.dwr.services.UserDao" />
</create>
<convert converter="bean" match="com.yue.test1.dwr.bean.User" />
</allow>
</dwr>
5.index.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" 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" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<Meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script type="text/javascript" src="../js/jquery-1.3.2.js"></script>
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/engine.js'></script>
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/util.js'></script>
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/interface/Demo.js'></script>
<script type='text/javascript' src='${pageContext.request.contextPath}/dwr/interface/UserDao.js'></script>
<script type="text/javascript"> //此函数中可以调用java类的方法,除了java方法本身的参数外,还要将回调函数名作为参数传给java方法 function sayHello(name) { Demo.getHello(name,dwrHandler); } //这是dwr的一个回调函数,data参数即java方法getHello(String name)的返回值 function dwrHandler(data) { alert(data); } function save2(data){ if(data){ alert("注册成功"); }else{ alert("注册失败"); } } function onSave(){ var userMap={}; userMap.username=form1.username.value; userMap.password=form1.password.value; UserDao.save(userMap,save2); } function getUser(data){ if(data==null){ alert("找不到用户"); return ; } alert("用户信息:"+data.username+data.password); } function findUser(){ UserDao.getUser("1",getUser); } </script>
<title>DWR Demo</title>
</head>
<body>
<button onclick="sayHello('Yue')">say Hello to Yue</button><br/>
<form name="form1">
用户名:<input type="text" name="username"/><br/>
密码:<input type="text" name="password"/><br/>
<input type="submit" name="submitBtn" value="提交" onclick="onSave()"/>
</form>
<button onclick="findUser()">findUser</button><br/>
</body>
</html>
6.other 不要出现Javascript保留关键字;和保留关键字同名的函数指定被排除。多数Javascript的关键字和Java是相同的。所以你不可能有一个方法叫做”try()” 但是该死”delete()”对与Javascript有着特殊意义,而对Java则不是。 Javascript方法重载是不支持的,所以尽量不要再Java中使用。