(转)spring与DWR集成的两种方法

1。第一种:

 

在这种方式中,不需要dwr.xml文件,将这部分配置与Spring的配置写在同一个文件中。DWR使用的servlet为:org.directwebremoting.spring.DwrSpringServlet,这样,只需要两个配置文件就可以了,分别是:

 

web.xml

 

<?xml version="1.0" encoding="utf-8"?>

 

<!DOCTYPE web-app PUBLIC

 

    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

 

    "http://java.sun.com/dtd/web-app_2_3.dtd">

 

<web-app id="simplelife.cn">

 

  <context-param>

 

    <param-name>contextConfigLocation</param-name>

 

    <param-value>/WEB-INF/applicationContext.xml</param-value>

 

  </context-param>

 

  <listener>

 

    <listener-class>

 

      org.springframework.web.context.ContextLoaderListener

 

    </listener-class>

 

  </listener>

 

  <servlet>

 

    <servlet-name>dwr-invoker</servlet-name>

 

    <display-name>DWR Servlet</display-name>

 

    <description>Direct Web Remoter Servlet</description>

 

    <servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>

 

    <!– This should NEVER be present in live –>

 

    <init-param>

 

      <param-name>debug</param-name>

 

      <param-value>true</param-value>

 

    </init-param>

 

    <!– Remove this unless you want to use active function reverse() {

 

[native code]

 

} ajax –>

 

    <init-param>

 

      <param-name>activeReverseAjaxEnabled</param-name>

 

      <param-value>true</param-value>

 

    </init-param>

 

    <!– By default DWR creates application scope objects when they are first

 

    used. This creates them when the app-server is started –>

 

    <init-param>

 

      <param-name>initApplicationScopeCreatorsAtStartup</param-name>

 

      <param-value>true</param-value>

 

    </init-param>

 

    <!– This enables full streaming mode. It’s probably better to leave this

 

    out if you are running across the internet –>

 

    <init-param>

 

      <param-name>maxWaitAfterWrite</param-name>

 

      <param-value>-1</param-value>

 

    </init-param>

 

    <!–

 

    For more information on these parameters, see:

 

    - http://getahead.org/dwr/server/servlet

 

    - http://getahead.org/dwr/function reverse() {

 

[native code]

 

}-ajax/configuration

 

    –>

 

    <load-on-startup>1</load-on-startup>

 

  </servlet>

 

  <servlet-mapping>

 

    <servlet-name>dwr-invoker</servlet-name>

 

    <url-pattern>/dwr/*</url-pattern>

 

  </servlet-mapping>

 

</web-app>

 

2)在applicationContext.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:p="http://www.springframework.org/schema/p"

       xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"

       xmlns:context="http://www.springframework.org/schema/context"

       xsi:schemaLocation="http://www.springframework.org/schema/beans

                           http://www.springframework.org/schema/beans/spring-beans-2.0.xsd

                           http://www.directwebremoting.org/schema/spring-dwr

                           http://www.directwebremoting.org/schema/spring-dwr-2.0.xsd

                           http://www.springframework.org/schema/context

                           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

  <bean id="dwrJavaDate" class="java.util.Date">

    <dwr:remote javascript="javaDate">

    </dwr:remote>

  </bean>

 

  <dwr:configuration></dwr:configuration>

  <dwr:controller id="dwrController" debug="true" />

</beans>

 

2.第二种

 

第二种配置方式

使用第一种方式的Spring配置文件看起来比较乱,不那么干净,在这种方式中,将Spring的配置与DWR的配置完全分开,看起来干净利落。DWR使用的servlet为:org.directwebremoting.servlet.DwrServlet ,这样就需要三个配置文件了:

 

web.xml文件:

 

<?xml version="1.0" encoding="ISO-8859-1"?>

 

<!DOCTYPE web-app PUBLIC

 

    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"

 

    "http://java.sun.com/dtd/web-app_2_3.dtd">

 

<web-app id="simplelife.cn">

 

  <context-param>

 

    <param-name>contextConfigLocation</param-name>

 

    <param-value>/WEB-INF/applicationContext.xml</param-value>

 

  </context-param>

 

 <listener>

 

     <listener-class>

 

         org.springframework.web.context.ContextLoaderListener

 

     </listener-class>

 

 </listener>

 

  <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>

 

    <!– This should NEVER be present in live –>

 

    <init-param>

 

      <param-name>debug</param-name>

 

      <param-value>true</param-value>

 

    </init-param>

 

    <!– Remove this unless you want to use active function reverse() {

 

[native code]

 

} ajax –>

 

    <init-param>

 

      <param-name>activeReverseAjaxEnabled</param-name>

 

      <param-value>true</param-value>

 

    </init-param>

 

    <!– By default DWR creates application scope objects when they are first

 

    used. This creates them when the app-server is started –>

 

    <init-param>

 

      <param-name>initApplicationScopeCreatorsAtStartup</param-name>

 

      <param-value>true</param-value>

 

    </init-param>

 

    <!– This enables full streaming mode. It’s probably better to leave this

 

    out if you are running across the internet –>

 

    <init-param>

 

      <param-name>maxWaitAfterWrite</param-name>

 

      <param-value>-1</param-value>

 

    </init-param>

 

    <!–

 

    For more information on these parameters, see:

 

    - http://getahead.org/dwr/server/servlet

 

    - http://getahead.org/dwr/function reverse() {

 

[native code]

 

}-ajax/configuration

 

    –>

 

    <load-on-startup>1</load-on-startup>

 

  </servlet>

 

  <servlet-mapping>

 

    <servlet-name>dwr-invoker</servlet-name>

 

    <url-pattern>/dwr/*</url-pattern>

 

  </servlet-mapping>

 

</web-app>

 

 applicationContext.xml文件,这是一个纯粹的Spring配置文件:

 

<?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: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-2.0.xsd

 

                           http://www.springframework.org/schema/context

 

                           http://www.springframework.org/schema/context/spring-context-2.5.xsd">

 

 

 

  <bean id="dwrJavaDate" class="java.util.Date">

 

  </bean>

 

</beans>

 

  最后一个文件是dwr.xml,在这个文件中,使用spring容器中的对象:

 

<?xml version="1.0" encoding="UTF-8"?>

 

<!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" 

 

  "http://getahead.org/dwr/dwr20.dtd">

 

<dwr>

 

  <allow>

 

    <create creator="spring" javascript="javaDate">

 

      <param name="beanName" value="dwrJavaDate"/>

 

    </create>

 

  </allow>

 

</dwr>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值