DWR是一个可以允许你去创建AJAX WEB 站点的JAVA 开源库。
它可以让你在浏览器中的Javascript代码调用Web服务器上的Java 代码,就像在Java代码就在浏览器中一样。
DWR包含 2个主要部分:
一个运行在服务器端的Java Servlet,它处理请求并且向浏览器发回响应。
一个运行在浏览器端的JavaScript,它发送请求而且还能动态更新网页。
DWR工作原理是通过动态把Java类生成为Javascript。它的代码就像Ajax魔法一样,你感觉调用就像发
生在浏览器端,但是实际上代码调用发生在服务器端,DWR负责数据的传递和转换。这种从Java 到
JavaScript的远程调用功能的方式使DWR用起来有种非常像RMI或者SOAP的常规RPC机制,而且DWR
的优点在于不需要任何的网页浏览器插件就能运行在网页上。
Java从根本上讲是同步机制,然 AJAX却是异步的。所以你调用远程方法时,当数据已经从网络上返回
的时候,你要提供有反调 (callback) 功能的DWR。
第 1个 DWR 例子:Hello World
1) 从官方网站下载dwr.jar包。然后将它放在你 webapp 的 WEB-INF/lib目录下。
2) 修改web.xml,如下
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app id="dwr">
<display-name>DWR (Direct Web Remoting)</display-name>
<description>A Simple Demo DWR</description>
<servlet>
<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>
</web-app>
3) 在WEB-INF下新建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="service">
<param name="class"
value="helloworld.Service"/>
</create>
</allow>
</dwr>
4) 启动web服务器,访问http://localhost/工程/dwr ,页面结果显示为
Classes known to DWR:
* service (helloworld.Service)
5) 点击进入 *service,看到提示....
Methods For: service (helloworld.Service)
To use this class in your javascript you will need the following script includes:
<script type='text/javascript' src='/TestDWR/dwr/interface/service.js'></script>
<script type='text/javascript' src='/TestDWR/dwr/engine.js'></script>
In addition there is an optional utility script:
<script type='text/javascript' src='/TestDWR/dwr/util.js'></script>
Replies from DWR are shown with a yellow background if they are simple or in an alert box otherwise.
The inputs are evaluated as Javascript so strings must be quoted before execution.
There are 10 declared methods:
* sayHello( );
* hashCode( );
(Warning: hashCode() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* getClass( );
(Warning: No Converter for java.lang.Class. See below)
(Warning: getClass() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* wait( );
(Warning: overloaded methods are not recommended. See below)
(Warning: wait() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* wait( , );
(Warning: overloaded methods are not recommended. See below)
(Warning: wait() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* wait( );
(Warning: overloaded methods are not recommended. See below)
(Warning: wait() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* equals( );
(Warning: No Converter for java.lang.Object. See below)
(Warning: equals() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* notify( );
(Warning: notify() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* notifyAll( );
(Warning: notifyAll() is excluded: Methods defined in java.lang.Object are not accessible. See below)
* toString( );
(Warning: toString() is excluded: Methods defined in java.lang.Object are not accessible. See below)
6)创建jsp,内容如下
<%@ page language="java" pageEncoding="UTF-8"%>
<html>
<head>
<script type='text/javascript' src='dwr/interface/service.js'></script>
<script type='text/javascript' src='dwr/engine.js'></script>
<script type='text/javascript' src='dwr/util.js'></script>
</script>
<script type="text/javascript">
function firstDwr(){
service.sayHello(" Test ",callBackHello);
}
function callBackHello(data){
alert(data);
}
</script>
</head>
<body>
<input type="button" name="button" value="测试" onclick="firstDwr()">
</body>
</html>
当点击"测试"时,就出现结果了.
总结:
1.在web.xml中加入dwr servlet
2.在drw.xml中指明你要调用的类,并指明生成的javascript名
<create creator="new" javascript="service">
<param name="class" value="helloworld.Service"/>
</create>
3.完成对应的类
4.在html/jsp中写javascript函数,调用java服务器端的方法,并写处理结果的回调函数