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

          Struts1和Spring的集成

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

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

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

          1springstruts依賴庫配置

                   * 配置struts

                             --拷貝struts類庫和jstl類庫

                             --修改web.xml文件來配置ActionServlet

                             --提供struts-config.xml文件

                             --提供國際化資源文件

                   * 配置spring

                             --拷貝spring類庫

                             --提供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、通過BeanFactory取得業(yè)務對象,調(diào)用業(yè)務邏輯方法

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

          擴展學習:

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

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

          <%

          String path = request.getContextPath();

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

          %>

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

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

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

          lServlet Listener

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

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

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

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

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

          第二種集成方案

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

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

          1springstruts依賴庫配置

                   * 配置struts

                             --拷貝struts類庫和jstl類庫

                             --修改web.xml文件來配置ActionServlet

                             --提供struts-config.xml文件

                             --提供國際化資源文件

                   * 配置spring

                             --拷貝spring類庫

                             --提供spring配置文件

          2、因為Action需要調(diào)用業(yè)務邏輯方法,所以需要在Action中提供setter方法,讓spring將業(yè)務邏輯對象注入過來

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

                 * <action>標簽中的type屬性需要修改為

                                      org.springframework.web.struts.DelegatingActionProxy

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

                 IoC容器中取得本次請求對應的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>標簽的path屬性值一致

                   * 必須注入業(yè)務邏輯對象

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


          Feedback

          # re: Struts1和Spring的集成  回復  更多評論   

          2011-02-10 17:53 by 何楊
          標記一下。

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


          網(wǎng)站導航:
           
          主站蜘蛛池模板: 广丰县| 大丰市| 乃东县| 新建县| 定州市| 凤庆县| 上饶市| 独山县| 临夏县| 乐陵市| 门源| 武夷山市| 津市市| 太白县| 黎平县| 睢宁县| 岢岚县| 巩义市| 兰考县| 嵊泗县| 长治市| 临朐县| 曲沃县| 山西省| 临江市| 深水埗区| 靖州| 师宗县| 辽宁省| 鹤庆县| 绍兴市| 镇远县| 赣州市| 镇安县| 扬州市| 灌南县| 夏津县| 祁阳县| 岚皋县| 体育| 沛县|