DWR与SPRING 集成


Step 1 - Give DWR access to the Spring context

 
  1. Use Spring MVC
  2. Use the DWRSpringServlet
Use the DwrSpringServlet
web.xml配置:
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>
    classpath:yourSpringContext.xml
  </param-value>
</context-param>
<servlet>
  <servlet-name>dwr</servlet-name>
  <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>
  <init-param>
    <param-name>debug</param-name>
    <param-value>true</param-value>
  </init-param>
</servlet>
<!-- /dwr/* 必须在/*前面 -->
<servlet-mapping>
  <servlet-name>dwr</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
 
直接使用SPRING MVC + org.directwebremoting.spring.DwrController
<servlet>
  <servlet-name>springDispatcher</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
    <param-name>contextConfigLocation</param-name>
    <param-value> classpath:yourSpringContext.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
  <servlet-name>springDispatcher</servlet-name>
  <url-pattern>*.html</url-pattern>
</servlet-mapping>
<servlet-mapping>
  <servlet-name>springDispatcher</servlet-name>
  <url-pattern>/dwr/*</url-pattern>
</servlet-mapping>
 
org.directwebremoting.spring.DwrController
 <dwr:controller id="dwrController" debug="true">
    <dwr:config-param name="activeReverseAjaxEnabled" value="true"/>
  </dwr:controller 
STEP 2   - Configure DWR's remoting DWR配置文件
  • 使用SPRING 配置文件替换原来的DWR文件的配置方式,这种配置方式和原来的DWR文件配置方式变化较小:
  • The configuration tag
    <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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.directwebremoting.org/schema/spring-dwr
        http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">

    The <dwr:configuration/> is used to mimic the behavior of the configuration available indwr.xml. This tag is optional and it may have nested tags (init, creator, signatures,..). These nested tags mimic the behavior of those available in dwr.xml. Example:

     

      <dwr:configuration>
        <dwr:convert type="bean" class="org.uk.ltd.dwr.dev.model.Address" />
      </dwr:configuration>
    

     

    The remote tag

    Inside each bean you want to remote include a <dwr:remote javascript="Fred"> tag. There you can specify the methods that are going to be proxied and those that won't. For example:

    <bean id="timeConvert" class="com.mycompany.ui.util.TimeConvert">
      <dwr:remote javascript="AjaxTimeConvert">
        <dwr:include method="convert" />
      </dwr:remote>
    </bean>
  • Use the DWR/Spring namespace with annotations (Spring 2.5 or greater, DWR 2.x or greater, dwr.xml not required or recommended)
  • 使用注解的方式替换原有的DWR配置文件,实现零配置文件
  • <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"
      xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.directwebremoting.org/schema/spring-dwr
        http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd">
    • 以下配置作用是
    • <dwr:annotation-config id="dwrAnnotationConfig" /><!--开启注解扫描配置 -->
    • <dwr:annotation-scan base-package="com.yourpackage.whatever" scanDataTransferObject="true" scanRemoteProxy="false" /> <!--注解扫描范围的配置 -->
    • annotation-scan - Enables DWR to scan the classpath and:
      • Detect beans annotated with @RemoteProxy & @RemoteMethod, and register the Spring beans and DWR Creator proxies for them. Because DWR is registering the beans into the Spring context for you all Spring annotated beans (@Service, @Component, @Controller, etc.) will be ignored to avoid duplicate registration.
      • Detect DWR converters annotated with @DataTransferObject.
      This element has several available attributes:
      • base-package - The base package to initiate scanning from - i.e. com.myApp.*.
      • regex - A regular expression that will be used in the classpath scanner.
      • scanRemoteProxy - Should DWR scan for remote proxies? Defaults to true.
      • scanDataTransferObject - Should DWR scan for converters? Defaults to true.
      • scanGlobalFilter - Defaults to true.

 

A complete working example of a dwr:annotation-scan configuration can be found here.

 

  • annotation-config - Enables DWR to scan the Spring context, detect beans annotated with @RemoteProxy & @RemoteMethod and register the DWR Creator proxies for them.
  • 然后在具体的代码上使用
  • @RemoteProxy - Functional namespace equivalent = dwr:remote
  • @RemoteMethod - Functional namespace equivalent = dwr:include method="methodName"
  • @DataTransferObject - Functional namespace equivalent = dwr:convert
  • 案例代码如下:
  • package org.myframework.dwr;
  • import javax.annotation.Resource;
  • import org.directwebremoting.annotations.RemoteMethod;
  • import org.directwebremoting.annotations.RemoteProxy;
  • import org.springframework.stereotype.Component;
  • //http://127.0.0.1:8010/springdwr/test/SayHello
  • // http://127.0.0.1:8010/dwr/index.html
  • @RemoteProxy(name="SayHello")
  • public class SayHello {
  • @RemoteMethod 
  • public String sayHello(String abc){
  • return "sayHello to " + abc ;
  • }
  • @RemoteMethod 
  • public String sayHelloFromSpringBean(){
  • return "sayHello to " + springBean ;
  • }
  • @Resource(name = "springBean")
  • SpringBean springBean;
  • }
  • Use the Spring Creator (Will work for older versions of Spring and DWR - dwr.xml required. Not the recommended approach.)
  • 第三种传统方式不推荐使用:
  • <allow>
      ...
      <create creator="spring" javascript="Fred">
        <param name="beanName" value="Shiela"/>
      </create>
      ...
    </allow>

另外:直接使用SPRING mvc貌似test的时候必须写清楚调用的具体内容,没有界面展现所有的DWR调用的内容:

  • //http://127.0.0.1:8010/springdwr/test/SayHello dwrController
  • // http://127.0.0.1:8010/dwr/index.html springDwr
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值