邵波的空间 的 Dw入门
Person.java
Java代码
- package com.wangyu;
- public class Person
- {
- private String name;
-
- public Person()
- {
- }
-
- public Person(String name)
- {
- this.name = name;
- }
- public void setName(String name)
- {
- this.name = name;
- }
- public String getName()
- {
- return name;
- }
-
- }
package com.wangyu;
public class Person
{
private String name;
public Person()
{
}
public Person(String name)
{
this.name = name;
}
public void setName(String name)
{
this.name = name;
}
public String getName()
{
return name;
}
}
HelloDwr.java
Java代码
- package com.wangyu;
- 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");
- result[1] = new Person("数组bbbb");
- result[2] = new Person("数组cccc");
- 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;
- }
- }
package com.wangyu;
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");
result[1] = new Person("数组bbbb");
result[2] = new Person("数组cccc");
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;
}
}
Cat.java
Java代码
- package com.wangyu;
-
- public class Cat
- {
- private String name;
- public Cat(String name)
- {
- this.name = name;
- }
- }
package com.wangyu;
public class Cat
{
private String name;
public Cat(String name)
{
this.name = name;
}
}
<hr>
hellodwr.js
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 + "<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;
- }
//--------------------发送简单字符串参数,返回普通字符串----------------------------
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 + "<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;
}
Html代码
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <HTML>
- <HEAD>
- <TITLE>DWR入门</TITLE>
- <script type='text/javascript' src='dwr/interface/hello.js'></script>
- <script type='text/javascript' src='dwr/engine.js'></script>
- <script type='text/javascript' src='dwr/util.js'></script>
- <script type="text/javascript" src="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>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>DWR入门</TITLE>
<script type='text/javascript' src='dwr/interface/hello.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
<script type="text/javascript" src="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>
dwr.xml
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.wangyu.HelloDwr"/>
- </create>
- <convert converter="bean" match="com.wangyu.Person"/>
- <convert converter="object" match="com.wangyu.Cat">
- <param name="force" value="true"/>
- </convert>
- </allow>
- <signatures>
- <![CDATA[
- import com.wangyu.HelloDwr;
- import com.wangyu.Person;
- import java.util.List;
- import java.util.Map;
- ]]>
- </signatures>
- </dwr>
<?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.wangyu.HelloDwr"/>
</create>
<convert converter="bean" match="com.wangyu.Person"/>
<convert converter="object" match="com.wangyu.Cat">
<param name="force" value="true"/>
</convert>
</allow>
<signatures>
<![CDATA[
import com.wangyu.HelloDwr;
import com.wangyu.Person;
import java.util.List;
import java.util.Map;
]]>
</signatures>
</dwr>
web.xml
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">
- <!-- 配置DWR的核心Servlet -->
- <servlet>
- <!-- 指定DWR核心Servlet的名字 -->
- <servlet-name>dwr</servlet-name>
- <!-- 指定DWR核心Servlet的实现类 -->
- <servlet-class>
- org.directwebremoting.servlet.DwrServlet
- </servlet-class>
- <!-- 指定DWR核心Servlet处于调试状态 -->
- <init-param>
- <param-name>debug</param-name>
- <param-value>true</param-value>
- </init-param>
- </servlet>
- <!-- 指定核心Servlet的URL映射 -->
- <servlet-mapping>
- <servlet-name>dwr</servlet-name>
- <!-- 指定核心Servlet映射的URL -->
- <url-pattern>/dwr/*</url-pattern>
- </servlet-mapping>
- <login-config>
- <auth-method>BASIC</auth-method>
- </login-config>
- </web-app>