1,导包
2,配置web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.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>
</web-app>
3,书写dwr.xml放在webinf下
<?xml version="1.0" encoding="GBK"?>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
"http://getahead.ltd.uk/dwr/dwr10.dtd">
<dwr>
<allow>
<create javascript="checkExit" creator="new">
<param name="class" value="com.dhcc.dwr.Dwr"></param>
<include method="returnString"/>
<include method="returnList"/>
<include method="returnArrayList"/>
<include method="returnMap"/>
</create>
<convert match="com.dhcc.dwr.Student" converter="bean">
<param name="include" value="name,pwd"></param>
</convert>
</allow>
</dwr>
5,书写java类
(1) dwr类
package com.dhcc.dwr;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class Dwr {
//举例返回字符,list,map,数组,javabean
public String returnString(){
return "success";
}
//返回list
public List<String> returnList(){
List<String> users=new ArrayList<String>();
users.add("1");
users.add("2");
return users;
}
//返回带有javabean的arraylist集合
public ArrayList<Student> returnArrayList(){
ArrayList<Student> stus=new ArrayList<Student>();
Student stu1=new Student();
stu1.setName("litao");
stu1.setPwd("111");
Student stu2=new Student();
stu2.setName("congcong");
stu2.setPwd("222");
stus.add(stu1);
stus.add(stu2);
return stus;
}
//返回map集合
public Map<String, String> returnMap(){
Map<String, String> user=new HashMap<String, String>();
user.put("0", "男");
user.put("1", "女");
return user;
}
}
(2) 书写javabean类,用于返回arraylist
package com.dhcc.dwr;
public class Student {
private String name;
private String pwd;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPwd() {
return pwd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
}
6测试http://localhost:8080/项目名/dwr
7,引用js
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<script type='text/javascript' src='/ExampleDwr/dwr/interface/checkExit.js'></script>
<script type='text/javascript' src='/ExampleDwr/dwr/engine.js'></script>
<script type='text/javascript' src='/ExampleDwr/dwr/util.js'></script>
<body >
这是一个DWR例子 <br>
<input type="button" value="返回字符串1" onclick="check1()"/>
<input type="button" value="返回字符串2" onclick="check2()"/>
<input type="button" value="返回list" onclick="check3()"/>
<input type="button" value="返回Arraylist" onclick="check4()"/>
<input type="button" value="返回map" onclick="check5()"><br/>
<input type="text" name="username"/>
<input type="text" name="password"/>
<select name="user" style="width:100px">
</select>
<select name="aa">
<option value="1">男</option>
<option value="2">女</option>
</select>
<script type="text/javascript">
//两种格式的书写,返回字符串
function check1(){
checkExit.returnString(function(data){
alert(data);
//${"username"}.value=data;
alert(dwr.util.getValue("aa"));
dwr.util.setValue("username",data);
});
}
function check2(){
checkExit.returnString(callback);
function callback(data){
alert(data);
}
}
//返回list集合
function check3(){
checkExit.returnList(function(data){
for(var i=0;i<data.length;i++){
alert(data[i]);
}
});
}
//返回带有javabean的集合
function check4(){
checkExit.returnArrayList(function(data){
dwr.util.addOptions("user",data,"name","pwd");
for(var i=0;i<data.length;i++){
alert(data[i].name+data[i].pwd);
}
});
}
//返回map集合
function check5(){
checkExit.returnMap(function(data){
//或者知道键的前提下
dwr.util.removeAllOptions("user");
dwr.util.addOptions("user",["请选择"]);
dwr.util.addOptions("user",data);
for( var key in data){
alert(key+data[key]);
};
});
}
</script>
</body>
</html>