一、J2EE项目不用任何其他框架集成DWR框架:
web.xml配置文件:
<servlet> <servlet-name>dwr</servlet-name> <servlet-class>org.directwebremoting.servlet.DwrServlet </servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <!-- 配置DWR.XML方法一 --> <init-param> <param-name>config</param-name> <param-value>WEB-INF/classes/dwr.xml</param-value> </init-param> <!-- 如果不配置这个容易出现dwr Error: java.lang.SecurityException,Session Error异常 --> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>WEB-INF/classes/dwr.xml的配置文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://www.getahead.ltd.uk/dwr/dwr20.dtd"> <dwr> <allow> <!-- <convert converter="bean" match=""/> --> <create creator="new" javascript="Hello" scope="page"> <param name="class" value="luff.dwr.TESTDwr" /> <include method="test"/> </create> </allow> </dwr>
luff.dwr.TESTDwr类的编写内容:
package luff.dwr; public class TESTDwr { public String test(){ return "123456"; } }
jsp页面调用的方法:
<script type="text/javascript" src="dwr/interface/Hello.js"></script> <script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript" src="dwr/util.js"></script> <script type="text/javascript"> function ad(){ Hello.test(function(data){ alert(data); }); } </script>
其中maven依赖为:
<dependency> <groupId>org.directwebremoting</groupId> <artifactId>dwr</artifactId> <version>2.0.10</version> </dependency> <!-- 如果不包含这个依赖包会出现: DWR Could not initialize class org.directwebremoting.servlet.PlainPollHandler异常 --> <dependency> <groupId>commons-logging</groupId> <artifactId>commons-logging</artifactId> <version>1.2</version> </dependency>
需要的jar包:commons-logging-1.2.jar、dwr-2.0.10.jar
二、J2EE项目集成spring与DWR框架:
web.xml配置内容:
<!-- 配置spring --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>WEB-INF/classes/app*.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>dwr</servlet-name> <!-- 这里如果还是org.directwebremoting.servlet.DwrServlet则会出现如下异常 Error: java.lang.SecurityException: No class by name: TESTDwr --> <servlet-class>org.directwebremoting.spring.DwrSpringServlet </servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <!-- 如果不配置这个容易出现dwr Error: java.lang.SecurityException,Session Error异常 --> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>dwr</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
其中WEB-INF/classes/app*.xml的配置文件内容:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd" default-autowire="byName"> <bean id="tESTDwr" class="luff.dwr.TESTDwr"> <dwr:remote javascript="Hello"> <!-- 异步方法方法 --> <dwr:include method="qixi" /> <!-- <dwr:convert class="" type="bean" /> --> </dwr:remote> </bean> </beans>
luff.dwr.TESTDwr类内容的编写:
package luff.dwr; public class TESTDwr { public String test(){ return "123456"; } public String qixi(String name){ return "欢迎你:" + name; } }
jsp页面调用方法:
<script type="text/javascript" src="dwr/interface/Hello.js"></script> <script type="text/javascript" src="dwr/engine.js"></script> <script type="text/javascript" src="dwr/util.js"></script> <script type="text/javascript"> function ad(){ Hello.qixi("八年",function(data){ alert(data); }); } </script>
其中maven依赖为:
<dependency> <groupId>org.directwebremoting</groupId> <artifactId>dwr</artifactId> <version>3.0.M1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>3.2.10.RELEASE</version> </dependency>
注意,千万不要依赖spring版本为4.x的,spring4.x的部分类重构之后会报方法找不到异常。
需要的jar包:
参考文献:
1、http://www.cnblogs.com/kunpengit/archive/2012/03/09/2387537.html J2EE单独配置DWR
2、http://niwenhui.blog.163.com/blog/static/17060361520108822011653/ J2EE单独配置DWR,多个方法名配置到一个include里面用‘,’分隔。
3、http://www.jb51.cc/article/p-xejlikgn-bgy.html 解决:DWR Could not initialize classorg.directwebremoting.servlet.PlainPollHandler异常
4、http://www.jb51.cc/article/p-xejlikgn-bgy.html解决:Error: java.lang.SecurityException,Session Error异常
5、http://www.cnblogs.com/linjiqin/archive/2011/03/28/1998125.html J2EE配置spring、dwr框架
6、http://blog.sina.com.cn/s/blog_6792eed00100ijh9.html
7、http://blog.sina.com.cn/s/blog_5f044a4d010185pn.html 解决:Error: java.lang.SecurityException,Session Error 异常
原文链接:https://www.f2er.com/ajax/164600.html