DWR學(xué)習(xí)心得(一)
DWR是一個(gè)可以允許你去創(chuàng)建AJAX WEB 站點(diǎn)的JAVA 開源庫(kù)。
它可以讓你在瀏覽器中的Javascript代碼調(diào)用Web服務(wù)器上的Java 代碼,就像在Java代碼就在瀏覽器中一樣。
DWR包含 2個(gè)主要部分:
一個(gè)運(yùn)行在服務(wù)器端的Java Servlet,它處理請(qǐng)求并且向?yàn)g覽器發(fā)回響應(yīng)。
一個(gè)運(yùn)行在瀏覽器端的JavaScript,它發(fā)送請(qǐng)求而且還能動(dòng)態(tài)更新網(wǎng)頁(yè)。
DWR工作原理是通過動(dòng)態(tài)把Java類生成為Javascript。它的代碼就像Ajax魔法一樣,你感覺調(diào)用就像發(fā)
生在瀏覽器端,但是實(shí)際上代碼調(diào)用發(fā)生在服務(wù)器端,DWR負(fù)責(zé)數(shù)據(jù)的傳遞和轉(zhuǎn)換。這種從Java 到
JavaScript的遠(yuǎn)程調(diào)用功能的方式使DWR用起來有種非常像RMI或者SOAP的常規(guī)RPC機(jī)制,而且DWR
的優(yōu)點(diǎn)在于不需要任何的網(wǎng)頁(yè)瀏覽器插件就能運(yùn)行在網(wǎng)頁(yè)上。
Java從根本上講是同步機(jī)制,然 AJAX卻是異步的。所以你調(diào)用遠(yuǎn)程方法時(shí),當(dāng)數(shù)據(jù)已經(jīng)從網(wǎng)絡(luò)上返回
的時(shí)候,你要提供有反調(diào) (callback) 功能的DWR。
第 1個(gè) DWR 例子:Hello World
1) 從官方網(wǎng)站下載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,內(nèi)容如下
<?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) 啟動(dòng)web服務(wù)器,訪問http://localhost/工程/dwr ,頁(yè)面結(jié)果顯示為
Classes known to DWR:
* service (helloworld.Service)
5) 點(diǎn)擊進(jìn)入 *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)創(chuàng)建jsp,內(nèi)容如下
<%@ 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="測(cè)試" onclick="firstDwr()">
</body>
</html>
當(dāng)點(diǎn)擊"測(cè)試"時(shí),就出現(xiàn)結(jié)果了.
總結(jié):
1.在web.xml中加入dwr servlet
2.在drw.xml中指明你要調(diào)用的類,并指明生成的javascript名
<create creator="new" javascript="service">
<param name="class" value="helloworld.Service"/>
</create>
3.完成對(duì)應(yīng)的類
4.在html/jsp中寫javascript函數(shù),調(diào)用java服務(wù)器端的方法,并寫處理結(jié)果的回調(diào)函數(shù)
posted on 2007-06-01 22:39 想飛就飛 閱讀(3046) 評(píng)論(1) 編輯 收藏 所屬分類: J2EE