这几天,学习了下DWR框架,DWR即direct web remoting也即web远程调用,下面谈下关于DWR的一些比较基本的知识。
利用这个框架可以让AJAX开发变得很简单.利用DWR可以在客户端利用JavaScript直接调用服务端的Java方法并返回值给JavaScript就好像直接本地客户端调用一样(DWR根据Java类来动态生成JavaScrip代码).
DWR确实是一个非常优秀的项目,它通过反射,将java翻译成javascript,然后利用回调机制,轻松实现了javascript调用Java代码。
其大概开发过程如下:
1.编写业务代码,该代码是和dwr无关的。
2.确认业务代码中哪些类、哪些方法是要由javascript直接访问的。
3.编写dwr组件,对步骤2的方法进行封装。
4.配置dwr组件到dwr.xml文件中,如果有必要,配置convert,进行java和javascript类型互转。
5.通过反射机制,dwr将步骤4的类转换成javascript代码,提供给前台页面调用。
5.编写网页,调用步骤5的javascript中的相关方法(间接调用服务器端的相关类的方法),执行业务逻辑,将执行结果利用回调函数返回。
6.在回调函数中,得到执行结果后,可以继续编写业务逻辑的相关javascript代码。
以下是代码部分:
(1)web.xml文件的配置如下;
<?xml version="1.0" encoding="UTF-8"?>
<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">
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<!-- This should NEVER be present in live -->
<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>
(2)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="JDat">
<param name="class" value="java.util.Date"/>
</create>
-->
<create creator="new" javascript="test" scope="application">
<param name="class" value="cn.com.test.Test"/>
</create>
<create creator="new" javascript="person" scope="application">
<param name="class" value="cn.com.bean.Person"/>
</create>
<convert converter="bean" match="cn.com.bean.Person"/>
</allow>
</dwr>
(3)其他相关类
1>JAVABEAN:Person类
package cn.com.bean;
public class Person {
private String name;
public Person(){}
public Person(String name){
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
2>测试类;TEST
package cn.com.test;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import cn.com.bean.Person;
public class Test {
public String hello(String name){
return name + ",您好!您已经开始了DWR的学习之旅,祝您学得开心";
}
public String sendObj(Person person){
return person.getName() + ",传bean参数,您好!您已经开始了DWR的学习之旅,祝您学得开心";
}
public Person getBean(String name){
return new Person(name);
}
public List<Person> getPersonList(){
List result = new ArrayList<Person>();
Person p1 = new Person("p1");
Person p2 = new Person("p2");
Person p3 = new Person("p3");
result.add(p1);
result.add(p2);
result.add(p3);
return result;
}
public Person[] getPersonArray(){
Person[] persons = new Person[4];
persons[0] = new Person("p0");
persons[1] = new Person("p1");
persons[2] = new Person("p2");
persons[3] = new Person("p3");
return persons;
}
public Map<String,Person> getPersonMAp(){
Map<String,Person> result = new HashMap<String,Person>();
result.put("01", new Person("p01"));
result.put("02", new Person("p02"));
result.put("03", new Person("p03"));
return result;
}
public String sendList(List<Person> plist){
String result = "";
for (Person p:plist){
result += p.getName() + "<br/>";
}
return result;
}
public String sendBeanMap(Map<String,Person> personMap){
String result = "";
for(String key:personMap.keySet()){
result += "键:"+key+",其值为:"+personMap.get(key).getName()+"<br/>";
}
// Set set = personMap.keySet();
// Iterator it = set.iterator();
//
// while(it.hasNext()){
//
// }
return result;
}
}
(4)页面层
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>index.html</title>
<script type="text/javascript" src='dwr/interface/test.js' charset="UTF-8"></script>
<script type="text/javascript" src='dwr/interface/person.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='test.js'></script>-->
<script type="text/javascript">
<!-- 返回值为字符串-->
function sendMessage(){
var name = document.getElementById("name").value;
test.hello(name,cb);
}
function cb(data){
document.getElementById("show").innerHTML = data;
}
<!-- 返回值为JavaBean -->
function getBean(){
var name = document.getElementById("name").value;
test.getBean(name,beanCb);
}
function beanCb(data){
document.getElementById("show").innerHTML = data.name + ",返回JavaBean";
}
<!-- 返回值为字符串,传递JAVABEAN参数-->
function sendObject(){
var nameValue = document.getElementById("name").value;
test.sendObj({name:nameValue},cb);
}
<!-- 返回值为JAVABEAN 的List集合 -->
function getPerList(){
test.getPersonList(perListCb);
}
<!-- 返回值为JAVABEAN 的数组-->
function getBeanArray(){
test.getPersonArray(perListCb);
}
function perListCb(data){
var result = '';
for(var i = 0;i < data.length;i++){
result += data[i].name + "<br/>";
document.getElementById("show").innerHTML = result;
}
}
<!-- 返回值为JAVABEAN 的MAP-->
function getBeanMap(){
test.getPersonMAp(mapCb);
}
function mapCb(data){
var result = '';
for(var key in data){
result += "键为:" + key +",其值为:"+ data[key].name + "<br/>";
}
document.getElementById("show").innerHTML = result;
}
<!-- 返回值为字符串,传递JAVABEAN的list集合-->
function sendBeanList(){
var args = [
{name:"p0"},
{name:"p1"},
{name:"p2"}
];
test.sendList(args,sendListCb);
}
function sendListCb(data){
document.getElementById("show").innerHTML = data;
}
<!-- 返回值为字符串,传递JAVABEAN的map集合-->
function sendBeanMap(){
var args = {
key01:{name:"p0"},
key02:{name:"p1"},
key03:{name:"p2"}
};
test.sendBeanMap(args,sendListCb);
}
</script>
</head>
<body>
请输入您的名字:
<input id="name" name="name" type="text" /><br/>
<input type="button" value="返回JavaBean" οnclick="getBean();" /><br/>
<input type="button" value="返回简单请求" οnclick="sendMessage();" /><br/>
<input type="button" value="发送对象参数" οnclick="sendObject();" /><br/>
<input type="button" value="返回JavaBean的list" οnclick="getPerList();" /><br/>
<input type="button" value="返回JavaBean的数组" οnclick="getBeanArray();" /><br/>
<input type="button" value="返回JavaBean的map" οnclick="getBeanMap();" /><br/>
<input type="button" value="发送JavaBeanLIST参数" οnclick="sendBeanList();" /><br/>
<input type="button" value="发送JavaBeanMAP参数" οnclick="sendBeanMap();" /><br/>
下面是服务器端的回应:<br/>
<div id="show"></div>
</body>
</html>