Eclipse + Struts2.x+Spring2.x+Dwr3.x

我了解过官网关于 dwr3 的介绍后,发现它介绍的配置 dwr 的方法不是与 spring 结合的。也就是说每次进行 dwr 访问,系统都会重新 new 一个 service 来异步处理我们的请求。这对于简单的功能是没什么问题的,但如果涉及到复杂的逻辑处理,特别是需要 synchronized 的场合,就会出现问题了。所以我在网上专门找把 dwr3 spring 结合的配置方法。现在总结一下配置方法。

 

 

web.xml 的配置:

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

    < servlet-mapping >

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

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

    </ servlet-mapping >

注意: org.directwebremoting.spring.DwrSpringServlet dwr2.x 官网介绍的与 spring 结合的那个 servlet 一样 .

 

applicationContext.xml 的配置:

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

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

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

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

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

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

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

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

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

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

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

 

    <!-- 隐式注册了 AutowiredAnnotationBeanPostProcessor CommonAnnotationBeanPostProcessor -->

    < context:component-scan base-package = " service " />

    <!-- 扫描 spring 注解的类 , 使其成为客户端调用接口 -->

    < dwr:annotation-config />

    <!-- 扫描需要转换的 java 对象 -->

        < dwr:annotation-scan scanRemoteProxy = "false" base-package = " entity " />

    <!-- dwr 初始化配置 -->

    < dwr:configuration ></ dwr:configuration >

   

 

    < bean id = "loginService" class = "service.LoginService" >

       < property name = "sessionFactory" ref = "sessionFactory" ></ property >

    </ bean >

</ beans >

注意:

1.   文件头说明是 dwr3 的应用。

2.   红色字体的“ service ”指出 dwr 调用的服务类所在的包名。 LoginService 是我例子中处理 dwr 请求的服务类,它所在的包名就是“ service ”。

3.   红色字体的“ entity ”指出 dwr 调用传递的类对象的所在的包名。 User 是我例子中 dwr 传递的对象类,它所在的包名就是“ entity ”。

 

LoginService 服务类的定义:

package service;

 

import org.directwebremoting.annotations.RemoteMethod;

import org.directwebremoting.annotations.RemoteProxy;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import org.springframework.stereotype.Service;

 

@Service    

@RemoteProxy

public class LoginService extends HibernateDaoSupport {

 

      

       @RemoteMethod

       public String getService(){

              return this.toString();

       }

}

 

注意:使用的注解与 dwr 官网关于 dwr3 的介绍一样

 

调用 dwr index.jsp 的相关内容如下:

1 <input type="button" value=" 获取 Service 对象 " οnclick="getService()">

 

2 < script type = 'text/javascript' src = '/struts2_spring2_ahibernate3_dwr3/dwr/interface/LoginService.js' ></ script >

< script type = 'text/javascript' src = '/struts2_spring2_ahibernate3_dwr3/dwr/engine.js' ></ script >

 

3 function getService(){

       LoginService.getService( function (result){

           alert(result);

       });

    }

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值