dwr實作AJAX非常先進。比如動态生成javaScript代碼;隐藏的HTTP協定,javascript用于java代碼互動的javaScript對象。
自從我開始DWR感覺DWR是用javaScript對象去代替java類的對象去做某些事情。
比如一個java類名為:ShowHello 當你在dwr.xml檔案做了映射 javascrip對象叫 showHello 那麼你在html檔案或jsp檔案中想使用ShowHello中的某些方法。那就可直接使用showHello就可以了。dwr就是這樣的功能。
使用時要注意以下幾點:
1.引入兩個包。dwr.jar,xalan.jar這個包一般的情況在高版中把這個jar包的類已經包含了。低版的tomcat要引入這個包。可以根據情況,先引入dwr.jar測試不成功,再引入xalan.jar。
2.配置web.xml檔案。
具體的内容如下:
<servlet>
<servlet-name>kmweb</servlet-name>
<servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
<load-on-startup>1</load-on-startup>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
注意:debug這個參數一定要有,是用來測試的。
<servlet-mapping>
<url-pattern>/ajax/*</url-pattern>
</servlet-mapping>
3.編寫dwr.xml檔案
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC
"-//GetAhead Limited//DTD Direct Web Remoting 1.0//EN"
<dwr>
<allow>
<create javascript="showName" creator="new">
<param name="class" value="Test.A"/>
<include method="addNode"/>
</create>
</allow>
</dwr>
這裡面我已經寫了一例子。當你想在頁面中使用Test.A時就可以用showName來引用。include的意思就可showName對象可引用Test.A的哪些方法,不寫的話就是全部的方法。
上面的是用普通的方法。如果使用的spring架構的話。配置則是另外一種法。如下:
<create creator="spring" javascript="JSUserTree">
<param name="beanName" value="remoteUserTree"/>
<include method="addNode"/>
<include method="deleteNode"/>
<include method="renameNode"/>
<include method="getAllNodes"/>
</create>
4.編寫類。
5.測試(http://服務名字或IP:端口号/項目名/ajax/)如果出來index頁面就OK了
6.在測試index頁面中會出現Classes known to DWR:标題.下面是我測試是的一個例子:
showName (Test.A)
hitNum (Test.ReturnInt)
這就是類與js的對應關系. 點選要要使用的類進入下一個頁面.如:
Methods For: hitNum (Test.ReturnInt)
To use this class in your javascript you will need the following script includes:
<script type='text/javascript' src='/exercise/ajax/interface/hitNum.js'></script>
<script type='text/javascript' src='/exercise/ajax/engine.js'></script>
In addition there is an optional utility script:
<script type='text/javascript' src='/exercise/ajax/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 11 declared methods:
getHitNum( ); excute
setHitNum( ); excute
hashCode() is not available: Methods defined in java.lang.Object are not accessible
getClass() is not available: Methods defined in java.lang.Object are not accessible
wait() is not available: Methods defined in java.lang.Object are not accessible
equals() is not available: Methods defined in java.lang.Object are not accessible
notify() is not available: Methods defined in java.lang.Object are not accessible
notifyAll() is not available: Methods defined in java.lang.Object are not accessible
toString() is not available: Methods defined in java.lang.Object are not accessible
點選可以進行測試了.測試成功的話.下一步就是編寫您的頁面.
7.在編寫您的頁面時一定要把
類拟于上面的兩句話加入您的頁面.
8.在使用js時注意一定要一個回調函數做為最後一個參數(前提這個方法有傳回值時).
9.編寫回調函數,回調函數的參數就是js對應相應類的方法傳回值.
10.值得注意的是如果傳回的不是原始類型則必須在dwr.xml中寫上如下:
<convert match="Test.Person" converter="bean"></convert>
match是傳回的類型.converter表示是一個對象(确且我也不明白,明白後在寫一般情況就是這).
本文轉自左正部落格園部落格,原文連結:http://www.cnblogs.com/soundcode/archive/2010/12/20/1911885.html,如需轉載請自行聯系原作者