DWR是Direct Web Remoting的简称,主要实现用Java类来直接转换AJAX操作,能自动生成JavaScript文件供客户端实现AJAX功能。
前提:
项目/WEB-INF/lib下包含dwr20.dtd和dwr-2.0.1.jar(版本不计)
dwr.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "WEB-INF/lib/dwr20.dtd">
<dwr>
<!-- without allow, DWR isn't allowed to do anything -->
<allow>
<convert converter="bean" match="java.lang.StringBuffer"/>
<convert converter="bean" match="com.companya.projecta.DiskItem" javascript="DiskItem"/>
(convert通常用于DWR调用涉及到的对象输入)
<create creator="new" javascript="SomeHelper" scope="request">
<create creator="new" javascript="SomeHelper" scope="request">
<param name="class" value="com.companya.projecta.SomeHelper"/>
</create>
(new创建的是一些DWR调用辅助的一些功能类)
<create creator="spring" javascript="SomeHandler" scope="request">
<create creator="spring" javascript="SomeHandler" scope="request">
<param name="beanName" value="someHandler"></param>
<include method="methodA"/>
<include method="methodB"/>
<include method="methodC"/>
<include method="methodD"/>
<exclude method="methodE"/>
<auth method="methodA" role="Role_A"/>
</create>
<auth method="methodA" role="Role_A"/>
</create>
(spring创建的是真正用于AJAX调用的类对象,通常按beanName创建,然后在xml配置中定义该bean即可)
</allow>
<signatures>
<signatures>
<![CDATA[
import java.util.List;
import com.companya.projecta.DiskItem;
import com.companya.projecta.DiskItemImpl;
SomeHandler.addDiskItem(List<DiskItem>, String[][], String[], String[], boolean);
]]>
</signatures>
(签名需要实现AJAX调用的方法和其中用到的数据类,辅助类等)
</dwr>
web.xml:
<!-- dwr lister start -->
<listener>
<listener-class>org.directwebremoting.servlet.EfficientShutdownServletContextAttributeListener</listener-class>
</listener>
<listener>
<listener-class>org.directwebremoting.servlet.EfficientShutdownServletContextListener</listener-class>
</listener>
<!-- dwr lister end -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<description>Direct Web Remoter Servlet</description>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>maxWaitAfterWrite</param-name>
<param-value>500</param-value>
</init-param>
<load-on-startup>6</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
<listener>
<listener-class>org.directwebremoting.servlet.EfficientShutdownServletContextAttributeListener</listener-class>
</listener>
<listener>
<listener-class>org.directwebremoting.servlet.EfficientShutdownServletContextListener</listener-class>
</listener>
<!-- dwr lister end -->
<servlet>
<servlet-name>dwr-invoker</servlet-name>
<display-name>DWR Servlet</display-name>
<description>Direct Web Remoter Servlet</description>
<servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
<init-param>
<param-name>debug</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>activeReverseAjaxEnabled</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>initApplicationScopeCreatorsAtStartup</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>maxWaitAfterWrite</param-name>
<param-value>500</param-value>
</init-param>
<load-on-startup>6</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dwr-invoker</servlet-name>
<url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
(定义DWR所用的servlet到web.xml)
jsp:
<script type="text/javascript" src="/dwr/interface/SomeHandler.js"></script><!--this is functional-->
<script type="text/javascript" src="/dwr/engine.js"></script><!--this is required-->
<script type="text/javascript" src="/dwr/util.js'"></script><!--this is optional-->
(其中的/dwr/被web.xml定义的servlet所拦截并被DWR组件响应处理成合适的JavaScript文件。)
<script type="text/javascript" src="/dwr/interface/SomeHandler.js"></script><!--this is functional-->
<script type="text/javascript" src="/dwr/engine.js"></script><!--this is required-->
<script type="text/javascript" src="/dwr/util.js'"></script><!--this is optional-->
(其中的/dwr/被web.xml定义的servlet所拦截并被DWR组件响应处理成合适的JavaScript文件。)
js:
--initial the parameters
disklist = new Array(5);
strmatrix = new Array(5);
strmatrix[0]=new Array(4);
...
<!-- if /dwr/util.js included-->
var val1 = ${"dom-name1"}.value; // ${"dom-name1"} <=> document.getElementById("dom-name1")
SomeHandler.addDiskItem(disklist,strmatrix,strarray1,strarray2,boolean1,function(data){callbackfunc(data);});
配置bean someHandler-〉对应class为你的实现类,其中实现addDiskItem方法和methodA~E。
在用户定义的JS中可以把DWR定义的对象当成JavaScript中对象直接调用其方法,DWR会帮你做参数的转换和匹配,并在全部定义的参数后,增加一个函数型参数,供回调处理,其参数会被DWR所签名函数返回值自动填充。
本文转自 dannyy1026 51CTO博客,原文链接:
http://blog.51cto.com/dannyyuan/185115