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 對象 " onclick="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);

       });

    }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章