posts - 1,  comments - 0,  trackbacks - 0

          Spring是一個輕量級(大小和系統開支的角度)的IoC和AOP容器.它力圖簡化J2EE開發即J2EE without EJB.而且作為幫助企業級開發的核心支柱,Spring為模型層(OR持久層:Hibernate、JDO、iBatis等)服務層(EJB、JNDI、WebService)以及表現層(Struts、JSF、Velocity)都提供了良好的支持和集成方案. 訪問Spring官方站

          Jakarta-Struts是Apache軟件組織提供的一個開源項目.它為Java Web應用提供了基于Model-View-Controller的MVC框架,尤其適用于開發大型可擴展的Web應用.盡管基于Java的MVC框架層出不窮,事實上Spring的MVC模型也提供了驅動應用系統Web層的能力,但Jakarta-Struts仍然是所有這些框架中的佼佼者.

          下面,將如何整合這兩個J2EE領域的經典項目給出兩套詳盡的集成方案.


          1.首先我們來看一個Spring-Struts整合應用下的控制器Action類源代碼.


          public class CourceAction extends Action {
          private CourceService courceService;
          public ActionForward execute(
          ActionMapping mapping,
          ActionForm form,
          HttpServletRequest request,
          HttpServletResponse response) throws Exception {
          Set allCources = courceService.getAllCources();
          //..........the other statements
          request.setAttribute("cources", allCources);
          return mapping.findForward("jspView");
          } }


          分析:CourceService為一個業務實現的接口,此接口聲明了一系列的業務處理方法.該方法的實現配置為Spring上下問的一個Bean.由此看來,我們大家都可能會產生一個疑問:Struts action如何取得一個包含在Spring上下文中的Bean呢?為了回答這個問題,Spring提供了兩種與Struts集成的方式:

          (1).從一個知曉Spring上下文的基類派生我們自己的Struts Action類.然后,在派生類中就可以使用super.XX()方法來獲得一個對Spring受控Bean的引用.

          (2).將請求委托給作為Spring Bean管理的Struts Action來處理.

          2.注冊Spring插件:為了使Struts Action能夠訪問由Spring管理的Bean,我們就必須要注冊一個知道Spring應用上下文的Struts插件.可以在struts-config.xml中通過如下的方式來完成注冊.


          < plug-in classname="org.springframework.web.struts.ContextLoadPlugin">
          < set-property value="WEB-INF/Yhcip.xml,......" property="contextConfigLocation">
          < /PLUG-IN>


          ContextLoadPlugin()負責裝載一個Spring應用上下文.(具體的說:是一個WebApplicationContext).value屬性值為要加載的配置Spring受控Bean的xml文件的URI.

          3.完成第一種集成方案:實現一個知曉Spring的Action基類.

          這種集成方案是從一個公共的能夠訪問Spring應用上下文的基類中派生所有的Struts Action,但值得慶幸的是:我們不用自己去編寫這個知曉Spring應用上下文的基類,因為Spring已經提供了org.springframework.web.struts.ActionSupport:一個org.apache.struts.action.Action的抽象實現.它重載了setServlet()方法以從ContextLoaderPlugin中獲取WebapplicationContext.因此,任何時候我們只需要調用super.getBean()方法即可獲得一Spring上下文中的一個Bean的引用.

          我們再來看一段Action源代碼:


          public class CourceAction extends ActionSupport {
          public ActionForward execute(
          ActionMapping mapping,
          ActionForm form,
          HttpServletRequest request,
          HttpServletResponse response) throws Exception {
          //取得Spring上下文
          ApplicationContext context = super.getWebApplicationContext();
          //取得CourceService Bean
          CourseService courseService = (CourseService) context.getBean("courseService");
          Set allCources = courceService.getAllCources(); request.setAttribute("cources", allCources);
          //..........the other statements.
          return mapping.findForward("jspView");
          }}


          分析:這個Action類由ActionSupport派生,當CourceAction需要一個Spring受控Bean時:它首先調用基類的getWebApplicationContext()方法以取得一個Spring應用上下文的引用;接著它調用getBean()方法來獲取由Spring管理的courceService Bean的一個引用.

          小結

          至此,我們已經用第一種方案圓滿的完成了Spring與Struts的集成工作.這種集成方式的好處在于直觀簡潔容易上手.除了需要從ActionSupport中派生,以及需要從應用上下文中獲取Bean之外,其他都與在非Spring的Struts中編寫和配置Action的方法相似.但這種集成方案也有不利的一面.最為顯著的是:我們的Action類將直接使用Spring提供的特定類,這樣會使我們的Struts Action(即控制層)的代碼與Spring緊密耦合在一起.這是我們不情愿看到的.另外,Action類也負責查找由Spring管理的Bean,這違背了反向控制(IoC)的原則.

          4.實現第二種集成方案:代理和委托Action.

          這種集成方案要求我們編寫一個Struts Action,但它只不過是一個包含在Spring應用上下文中的真正Struts Action的一個代理.該代理Action從Struts插件ContextLoaderPlugIn中獲取應用上下文,從中查找真正的Struts Action,然后將處理委托給真正的Struts Action.這個方法的幽雅之處在于:只有代理action才會包含Spring特定的處理.真正的Action可以作為org.apache.struts.Action的子類來編寫.

          下面我們來看一段在之中集成方式下的Struts Action源代碼:


          public class CourceAction extends Action {
          private CourceService courceService;
          public ActionForward execute(
          ActionMapping mapping,
          ActionForm form,
          HttpServletRequest request,
          HttpServletResponse response) throws Exception {
          Set allCources = courceService.getAllCources();
          request.setAttribute("cources", allCources);
          //..........the other statements.
          return mapping.findForward("jspView");
          }
          /* 注入CourceService */
          public void setCourceService(CourceService courceService) {
          this.courceService = courceService;
          }}


          分析:大家可以看到,在這種方式之下,我們的Struts Action類和Spring是低耦合的,它僅僅依賴了Spring提供的反向控制(IoC)機制把CourceService注入到了我們的Action中.到此,大家肯定會有一個疑問:那就是Spring到底是如何提供IoC反向控制的呢?回答這個問題,我們需要完成兩個步驟的配置:

          (1).在struts-config.xml中注冊Struts Action.但要注意的是我們在這里注冊的是代理Action.幸運的是,我們不必親自編寫這個類.因為Spring已經通過org.springframework.web.struts.DelegatingActionProxy提供了這個代理的Action.具體的配置方法如下:


          < action type="org.springframework.web.struts.DelegatingActionProxy" path="/listCourses">


          (2)將真正的Struts Action作為一個Spring Bean并在Spring上下文配置文件中作為一個Bean注冊之.并將Action所要引用的courceService注入給它.


          < bean class="com.eRedCIP.web.CourceAction" name="/listCourses">
          < property name="">
          < ref bean="courseService">
          < /property>
          < /bean>


          注意:name屬性的值是非常重要的,它必須和struts-config.xml中的path屬性完全一致.這是因為DelegatingActionProxy會使用path屬性值在Spring上下文中查找真正的Action.使用DelegatingActionProxy的好處在于我們可以不使用任何Spring特定的類來編寫Struts Action.同時,Struts動作能夠利用IoC取得和他合作的對象.唯一不足之處就是不太直觀,配置相對復雜.為了使action委托顯得更為直觀一些,我們可對這種集成方案做進一步的改進:使用請求委托.

          5.使用請求委托.

          為了使action委托看上去更為直觀一些,Spring提供了DelegatingRequestProcessor,另一種專門用于Spring的請求處理器.需要在struts-config.xml中做如下配置:


          < controller processorclass="org.springframework.web.struts.DelegatingRequestProcessor">


          這樣,DelegatingRequestProcessor將告訴Struts自動將動作請求委托給Spring上下文中的Action來處理.這使得我們可以在struts-config.xml中用struts action的真正類型來聲明它們.例如:


          < action type="com.eRedCIP.web.CourceAction" path="/listCourses">


          當接受到一個針對/listCourses的請求時,DelegatingRequestProcessor會自動從Spring上下文配置文件中查找一個名為/listCourses的Bean(實為一個Struts Action)類.


          < action type="com.eRedCIP.web.CourceAction" path="/listCourses">
          (轉)

          posted on 2009-03-02 18:02 ID刀 閱讀(176) 評論(0)  編輯  收藏 所屬分類: Spring

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          留言簿(2)

          隨筆檔案(1)

          文章分類(21)

          文章檔案(17)

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 13736
          • 排名 - 2090

          最新評論

          主站蜘蛛池模板: 竹溪县| 乌兰察布市| 彭阳县| 黄浦区| 堆龙德庆县| 长乐市| 冕宁县| 新沂市| 平潭县| 宿松县| 普定县| 广安市| 改则县| 巴马| 西贡区| 曲水县| 萨迦县| 遂溪县| 三明市| 墨江| 黄石市| 图片| 桃江县| 房山区| 梁山县| 杭锦后旗| 德钦县| 鄂尔多斯市| 安塞县| 汾阳市| 浦城县| 乌兰县| 岢岚县| 寿光市| 楚雄市| 民乐县| 河间市| 朝阳区| 奈曼旗| 嘉义县| 普兰县|