Spring整合DWR(另外一种方法去掉dwr.xml )

一共两种方法。第一种是上一篇文章,第二种是这里的方法。

http://www.docin.com/p-58215636.html

这个是豆丁网的一个文章特别的好。

下面介绍Spring整合DWR的步骤:

      步骤一:

         下载Spring2.x(http://www.springframework.org)以及DWR2.0(http://getahead.org/dwr),然后将jar包导入到现有的工程下(/WEB-INF/lib/)。(因为DWR2.0中包含DwrSpringServlet类)

      步骤二:

         修改web.xml文件:

        

 

<!-- DWR servlet 配置 -->  

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

</servlet>  

<servlet-mapping>  

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

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

</servlet-mapping>  

  

<!-- 设置Spring监听器 -->  

<listener>  

    <listener-class>  

       org.springframework.web.context.ContextLoaderListener   

    </listener-class>  

</listener>  

这样配置可能会存在问题(主要取决与你IDE环境设置),因为侦听器会在/WEB-INF/下寻找applicationContext.xml文件,所以需要一点点小小的改动:

假设我的applicationContext.xml文件放在/src/com/下面,我们需要在web.xml文件中添加下面的语句进行说明。

<context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:/com/applicationContext.xml</param-value>
</context-param>

说明:
org.directwebremoting.spring.DwrSpringServlet这个类是dwr专门为整合spring提供的一个servlet,加入这个之后,dwr的配置就可以写入到spring的配置文件applicationContext.xml中,省掉了dwr.xml

这样就ok了。我们继续。

步骤三:

编写java类(用于通过AJAX实现的功能)。例如:验证码的实现(我的Blog验证码java实现的日志中有相关代码,请参考)。

步骤四:

配置applicationContext.xml文件

因为我门要在Spring配置文件中使用DWR的标签,所以要引入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.0.xsd   

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

<!-- DWR 配置开始 -->  

    <dwr:configuration></dwr:configuration><!-- 必须要configuration -->  

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

    <!-- DWR 配置结束 -->  

         

     <!-- spring bean配置 -->  

     <bean id="userService" class="test.bcndyl.service.impl.UserServiceImpl">  

     </bean>  

        

     <bean id="myCode" class="dwr.MyCode">  

        <!-- 定义调用的js文件 -->  

        <dwr:remote javascript="myCode"></dwr:remote>  

    </bean>  

    <!-- end spring bean配置 -->  

</beans>

在这里本人遇到过这样的问题:

如果这样写:

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"

在<dwr:configuration></dwr:configuration>和<dwr:remote javascript="myCode"></dwr:remote> 处会有错误的提示。将

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  改为

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd,错误就消失了  。

步骤五:

在需要AJAX的JSP页面中添加AJAX相关:

dwr/engine.js、dwr/util.js、dwr/interface/myCode.js

接下来的编码工作不用我说了!

DWR大大简化了AJAX的编码,很实用的东西啊!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值