加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

DWR与SPRING 集成

发布时间:2020-12-16 00:51:45 所属栏目:百科 来源:网络整理
导读:Step 1 - Give DWR access to the Spring context Use Spring MVC Use the DWRSpringServlet Use theDwrSpringServlet web.xml配置: listener listener-classorg.springframework.web.context.ContextLoaderListener/listener-class /listener context-para


Step 1 - Give DWR access to the Spring context

  1. Use Spring MVC
  2. Use the DWRSpringServlet

Use theDwrSpringServlet

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配置文件
  • Use the DWR/Spring namespace(Spring 2.5 or greater,DWR 2.x or greater,dwr.xml not required or recommended)
  • 使用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 indwr.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.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 toscan the classpathand:
      • 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 befound here.

  • annotation-config- Enables DWR toscan 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读