posts - 68, comments - 19, trackbacks - 0, articles - 1

          導(dǎo)航

          <2011年2月>
          303112345
          6789101112
          13141516171819
          20212223242526
          272812345
          6789101112

          留言簿

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評(píng)論

          評(píng)論排行榜

          Struts1和Spring的集成

          Posted on 2011-02-09 18:37 viery 閱讀(1410) 評(píng)論(1)  編輯  收藏
          轉(zhuǎn)自王勇老師筆記
          第一種集成方案

          原理:在Action中取得BeanFactory對(duì)象,然后通過(guò)BeanFactory獲取業(yè)務(wù)邏輯對(duì)象

          缺點(diǎn):產(chǎn)生了依賴,spring的類在action中產(chǎn)生了依賴查找。(注意和依賴注入的區(qū)別(前者主動(dòng)))。

          1springstruts依賴庫(kù)配置

                   * 配置struts

                             --拷貝struts類庫(kù)和jstl類庫(kù)

                             --修改web.xml文件來(lái)配置ActionServlet

                             --提供struts-config.xml文件

                             --提供國(guó)際化資源文件

                   * 配置spring

                             --拷貝spring類庫(kù)

                             --提供spring配置文件

                            

          2、在strutsAction中調(diào)用如下代碼取得BeanFactory

                  

          BeanFactoryfactory = WebApplicationContextUtils.getRequiredWebApplicationContext(request.getSession().getServletContext());

          使用listener配置beanfactory,將其初始化交給servlet,使其維持在ServletContext中,節(jié)省資源。(Listener初始化早于ServletWeblogic8除外))

          <context-param>

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

               <param-value>classpath*:applicationContext-*.xml</param-value>

          </context-param>

                   <listener>         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>

                   </listener>

          3、通過(guò)BeanFactory取得業(yè)務(wù)對(duì)象,調(diào)用業(yè)務(wù)邏輯方法

          補(bǔ)充:(Struts1.x相關(guān)并和Spring集成)

          擴(kuò)展學(xué)習(xí):

          lJbossjar包加載順序(根據(jù)字母),因此可能使得有些包無(wú)法加載。

          l使用高級(jí)模板創(chuàng)建的jsp文件,由于有

          <%

          String path = request.getContextPath();

          String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

          %>

          ------------

          <base href="<%=basePath%>" />

                   因此,jsp中的目錄都會(huì)從根目錄下查找。

          lServlet Listener

          ListenerServlet的監(jiān)聽器,它可以監(jiān)聽客戶端的請(qǐng)求、服務(wù)端的操作等。通過(guò)監(jiān)聽器,可以自動(dòng)激發(fā)一些操作,比如監(jiān)聽在線的用戶的數(shù)量。當(dāng)增加一個(gè)HttpSession時(shí),就激發(fā)sessionCreated(HttpSessionEvent se)方法,這樣就可以給在線人數(shù)加1。常用的監(jiān)聽接口有以下幾個(gè):

          ServletContextAttributeListener監(jiān)聽對(duì)ServletContext屬性的操作,比如增加、刪除、修改屬性。

          ServletContextListener監(jiān)聽ServletContext。當(dāng)創(chuàng)建ServletContext時(shí),激發(fā)contextInitialized(ServletContextEvent sce)方法;當(dāng)銷毀ServletContext時(shí),激發(fā)contextDestroyed(ServletContextEvent sce)方法。

          HttpSessionListener監(jiān)聽HttpSession的操作。當(dāng)創(chuàng)建一個(gè)Session時(shí),激發(fā)session Created(HttpSessionEvent se)方法;當(dāng)銷毀一個(gè)Session時(shí),激發(fā)sessionDestroyed (HttpSessionEvent se)方法。

          HttpSessionAttributeListener監(jiān)聽HttpSession中的屬性的操作。當(dāng)在Session增加一個(gè)屬性時(shí),激發(fā)attributeAdded(HttpSessionBindingEvent se) 方法;當(dāng)在Session刪除一個(gè)屬性時(shí),激發(fā)attributeRemoved(HttpSessionBindingEvent se)方法;當(dāng)在Session屬性被重新設(shè)置時(shí),激發(fā)attributeReplaced(HttpSessionBindingEvent se) 方法。

          第二種集成方案

          原理:將業(yè)務(wù)邏輯對(duì)象通過(guò)spring注入到Action中,從而避免了在Action類中的直接代碼查詢

          (客戶端請(qǐng)求---->代理action--->取得beanFactory--->getBean(..)創(chuàng)建action示例--->執(zhí)行exctute方法)

          1springstruts依賴庫(kù)配置

                   * 配置struts

                             --拷貝struts類庫(kù)和jstl類庫(kù)

                             --修改web.xml文件來(lái)配置ActionServlet

                             --提供struts-config.xml文件

                             --提供國(guó)際化資源文件

                   * 配置spring

                             --拷貝spring類庫(kù)

                             --提供spring配置文件

          2、因?yàn)?font face="Calibri">Action需要調(diào)用業(yè)務(wù)邏輯方法,所以需要在Action中提供setter方法,讓spring將業(yè)務(wù)邏輯對(duì)象注入過(guò)來(lái)

          3、在struts-config.xml文件中配置Action

                 * <action>標(biāo)簽中的type屬性需要修改為

                                      org.springframework.web.struts.DelegatingActionProxy

                 DelegatingActionProxy是一個(gè)Action,主要作用是取得BeanFactory,然后根據(jù)<action>中的path屬性值

                 IoC容器中取得本次請(qǐng)求對(duì)應(yīng)的Action

                

          4、在spring配置文件中需要定義strutsAction,如:

                   <bean name="/login" class="com.bjsxt.usermgr.actions.LoginAction" scope="prototype">

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

                   </bean>

                   * 必須使用name屬性,name屬性值必須和struts-config.xml文件中<action>標(biāo)簽的path屬性值一致

                   * 必須注入業(yè)務(wù)邏輯對(duì)象

                   * 建議將scope設(shè)置為prototype,這樣就避免了struts Action的線程安全問(wèn)題


          Feedback

          # re: Struts1和Spring的集成  回復(fù)  更多評(píng)論   

          2011-02-10 17:53 by 何楊
          標(biāo)記一下。

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 龙口市| 色达县| 大港区| 大竹县| 文登市| 平武县| 噶尔县| 鹿泉市| 上犹县| 理塘县| 上林县| 临泽县| 兴义市| 江北区| 淮北市| 茶陵县| 毕节市| 民权县| 遂昌县| 法库县| 晋州市| 筠连县| 丰城市| 巍山| 阳东县| 宁武县| 元阳县| 肥西县| 克拉玛依市| 洛浦县| 亳州市| 普定县| 二连浩特市| 武威市| 崇明县| 昆山市| 广水市| 阳东县| 达孜县| 施秉县| 保康县|