<servlet> <servlet-name>dwr-invoker</servlet-name> <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class> <init-param> <param-name>debug</param-name> <param-value>true</param-value> </init-param> <init-param> <param-name>crossDomainSessionSecurity</param-name> <param-value>false</param-value> </init-param> <init-param> <param-name>allowScriptTagRemoting</param-name> <param-value>true</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>dwr-invoker</servlet-name> <url-pattern>/dwr/*</url-pattern> </servlet-mapping>
因为SSH框架集成了struts2,此处必须配置不让struts2拦截掉/dwr/*的action,需要作第二步配置。
第二步:struts.xml配置
<constant name="struts.action.excludePattern" value="/dwr/.*,/dwr/test/.*"></constant>
用以使struts不拦截/dwr/.*的请求
第三步:spring的applicationContext.xml配置
1)、增加命名空间
xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr" xmlns:dwra="http://www.directwebremoting.org/schema/spring-dwr-annotations"
2)、
http://www.directwebremoting.org/schema/spring-dwr http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd http://www.directwebremoting.org/schema/spring-dwr-annotations http://www.directwebremoting.org/schema/spring-dwr-annotations.xsd
3)、
<context:annotation-config/> <context:component-scan base-package="com.gd.service.impl" /> <dwr:configuration /> <dwr:annotation-scan base-package="com.gd.service.impl" scanDataTransferObject="true" scanRemoteProxy="true" />
第四步:接口编写加注解:
@RemoteProxy(creator = SpringCreator.class,name = "DWRService") public class TmEduExaminationInfoManagerImpl implements ITmEduExaminationInfoManager
@RemoteMethod public String queryInfo(String str){ return "hello " + str; }
注:必须有接口及其实现类
第五步:jsp页面调用
1)、引用js
<script type='text/javascript' src='dwr/engine.js'></script> <script type='text/javascript' src='dwr/util.js'></script> <script type='text/javascript' src='dwr/interface/DWRService.js'></script>
2)、JS调用
DWRService.queryInfo("你是谁",function(flag){ if(flag){ alert(flag); }else{ alert(flag); } });