两个要用的bean:
dwr java file:
dwr.xml:
web.xml:
hellodwr.js:
index.jsp:
package com.bean;
public class Cat
{
private String name;
public Cat(String name)
{
this.name = name;
}
}
package com.bean;
public class Person
{
private String name;
private String age;
/**
* @param name
* @param age
*/
public Person(String name, String age)
{
super();
this.name = name;
this.age = age;
}
/**
* @return the age
*/
public String getAge()
{
return age;
}
/**
* @param age the age to set
*/
public void setAge(String age)
{
this.age = age;
}
public Person()
{
}
public Person(String name)
{
this.name = name;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
dwr java file:
package com.bean;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
public class HelloDwr
{
public String hello(String name)
{
return name + ",您好!您已经开始了DWR的学习之旅,祝您学得开心...";
}
public String sendObj(Person p )
{
return p.getName() + ",您好!您已经学会了使用JavaBean参数...";
}
public Person getBean(String name)
{
return new Person("服务器端" + name);
}
public Cat getObject(String name)
{
return new Cat("服务器端" + name);
}
public List<Person> getPersonList()
{
List<Person> result = new ArrayList<Person>();
result.add(new Person("集合aaaa"));
result.add(new Person("集合bbbb"));
result.add(new Person("集合cccc"));
return result;
}
public Person[] getPersonArray()
{
Person[] result = new Person[3];
result[0] = new Person("数组aaaa","22");
result[1] = new Person("数组bbbb","33");
result[2] = new Person("数组cccc","44");
return result;
}
public Map<String, Person> getPersonMap()
{
Map<String, Person> result = new HashMap<String, Person>();
result.put("first" , new Person("Map aaaa"));
result.put("second" , new Person("Map bbb"));
result.put("third" , new Person("Map cccc"));
return result;
}
public String sendList(List<Person> pl)
{
String result = "";
for (Person p : pl)
{
result += p.getName() + "<br>";
}
return result;
}
public String sendMap(Map<String , Person> pmap)
{
String result = "";
for (String key : pmap.keySet())
{
result += "键" + key + " 其值为:" + pmap.get(key).getName() + "<br>";
}
return result;
}
}
dwr.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">
<dwr>
<allow>
<create creator="new" javascript="hello">
<param name="class" value="com.bean.HelloDwr"/>
</create>
<convert converter="bean" match="com.bean.Person"/>
<convert converter="object" match="com.bean.Cat">
<param name="force" value="true"/>
</convert>
</allow>
<signatures>
<![CDATA[
import com.bean.HelloDwr;
import com.bean.Person;
import java.util.List;
import java.util.Map;
]]>
</signatures>
</dwr>
web.xml:
<?xml version="1.0" encoding="GBK"?>
<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">
<!--
the configuration of DWR
-->
<servlet>
<display-name>DWR Servlet</display-name>
<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>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
hellodwr.js:
//--------------------发送简单字符串参数,返回普通字符串----------------------------
function sendMessage()
{
var name = document.getElementById("name").value;
hello.hello(name , cb)
}
function cb(data)
{
document.getElementById("show").innerHTML = data;
}
//-------------------发送一个JavaBean对象作为参数,返回普通字符串-------------------
function sendObject()
{
var nameValue = document.getElementById("name").value;
hello.sendObj({name:nameValue} , cb);
}
//--------------------调用返回JavaBean方法----------------------
function getBean()
{
var name = document.getElementById("name").value;
hello.getBean(name , beanCb)
}
function beanCb(data)
{
document.getElementById("show").innerHTML = data.name + ",您好,您已经学会了使用JavaBean返回值";
}
//--------------------调用返回getObject方法----------------------
function getObject()
{
var name = document.getElementById("name").value;
hello.getObject(name , objCb)
}
function objCb(data)
{
document.getElementById("show").innerHTML = data.name + ",是从服务器返回的猫的名字";
}
//---------------调用返回集合的方法--------------------------
function getBeanList()
{
hello.getPersonList(listCb);
}
function listCb(data)
{
var result='';
for (var i = 0 ; i < data.length ; i ++)
{
result += data[i].name + "<br>";
}
document.getElementById("show").innerHTML = result;
}
//---------------调用返回数组的方法-------------------------
function getBeanArray()
{
hello.getPersonArray(arrayCb);
}
function arrayCb(data)
{
var result='';
for (var i = 0 ; i < data.length ; i ++)
{
result += data[i].name +data[i].age+ "<br>";
}
document.getElementById("show").innerHTML = result;
}
//---------------调用返回数组的方法-------------------------
function getBeanMap()
{
hello.getPersonMap(mapCb);
}
function mapCb(data)
{
var result='';
for (var key in data)
{
result += "键为" + key + ",其值为:" + data[key].name + "<br>";
}
document.getElementById("show").innerHTML = result;
}
//---------------调用发送集合的方法-------------------------
function sendBeanList()
{
var args = [
{name:"客户端aaa"},
{name:"客户端bbb"},
{name:"客户端ccc"}
];
hello.sendList(args , sendListCb);
}
function sendListCb(data)
{
document.getElementById("show").innerHTML = data;
}
//---------------调用发送Map的方法-------------------------
function sendBeanMap()
{
var args = {
first:{name:"客户端aaa"},
second:{name:"客户端bbb"},
third:{name:"客户端ccc"}
};
hello.sendMap(args , sendMapCb);
}
function sendMapCb(data)
{
document.getElementById("show").innerHTML = data;
}
index.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>DWR入门</TITLE>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/interface/hello.js'></script>
<script type='text/javascript' src='<%=request.getContextPath()%>/dwr/engine.js'></script>
<script type='text/javascript' src="<%=request.getContextPath()%>/dwr/util.js"></script>
<script type="text/javascript" src="<%=request.getContextPath()%>/hellodwr.js"></script>
</HEAD>
<BODY>
<h3>DWR入门</h3>
请输入您的名字<input id="name" name="name" type="text"/><br>
<input type="button" value="发送简单请求" onClick="sendMessage();"/>
<input type="button" value="发送对象参数" onClick="sendObject();"/>
<input type="button" value="返回JavaBean" onClick="getBean();"/><br>
<input type="button" value="返回Object" onClick="getObject();"/>
<input type="button" value="返回Bean集合" onClick="getBeanList();"/>
<input type="button" value="返回Bean数组" onClick="getBeanArray();"/><br>
<input type="button" value="返回Bean Map" onClick="getBeanMap();"/>
<input type="button" value="发送Bean集合" onClick="sendBeanList();"/>
<input type="button" value="发送Bean Map" onClick="sendBeanMap();"/><br>
<hr>
下面是服务器的回应:<br>
<div id= "show"></div>
</BODY>
</HTML>