1 用DWR2的话,记得加载的包是其下载例子中的lib下的包,这样才能齐全不能只加载一个dwr.jar的包.
2 dwr中的web.xml的配置
<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>
其中debug=true,允许以http://localhost:8085/xxxx/dwr/index.html的方式调试
3 dwr.xml小结
<?xml version="1.0" encoding="GBK"?>
<!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="lee.HelloDwr"/>
</create>
<convert converter="bean" match="lee.Person"/>
<convert converter="object" match="lee.Cat">
<param name="force" value="true"/>
</convert>
</allow>
<signatures>
<![CDATA[
import lee.HelloDwr;
import lee.Person;
import java.util.List;
import java.util.Map;
]]>
</signatures>
</dwr>
其中
<create creator="new" javascript="hello">
<param name="class" value="lee.HelloDwr"/>
</create>
表示客户端要调用时,用的是hello了,要将lee.HelloDwr这类进行转化为javascript.注意,一般对于类A转换为javascript,我们
用creator创建器,如果类A中的方法B要转换,我们使用convert完成JAVA类和javascript之间转换.
HelloDwr.java (演示了各类型的转换)
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.HashMap;
/**
* @author yeeku.H.lee [email protected]
* @version 1.0
* <br>Copyright (C), 2005-2008, yeeku.H.Lee
* <br>This program is protected by copyright laws.
* <br>Program Name:
* <br>Date:
*/
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;
}
}
Person.java:
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;
}
}
客户端调用.
//--------------------发送简单字符串参数,返回普通字符串----------------------------
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;
}
标签:DWR,function,name,笔记,Person,result,data,public From: https://blog.51cto.com/u_14230175/5906217