Spring整合Struts1.X的三種方法

來源:互聯網
上載者:User

使用Spring的ActionSupport 類整合Structs1.X,使用 Spring 的 DelegatingRequestProcessor覆蓋Struts的RequestProcessor,將StrutsAction管理委託給Spring架構

--

裝載應用環境:

無論您使用哪種技術,都需要使用 Spring 的 ContextLoaderPlugin 為 Struts 的 ActionServlet 裝載 Spring 應用程式環境

在struts-config.xml 檔案尾處添加該外掛程式:

<plug-in className=  "org.springframework.web.struts.ContextLoaderPlugIn">
    <set-propertyproperty= "contextConfigLocation"  

value="/WEB-INF/classes/applicationContext.xml"/>
</plug-in>

--

第一種:使用Spring的ActionSupport

該方法: 簡單快捷,但會導致struts和spring耦合在一起,如果要移值struts應用程式要重寫代碼.

例如:

  public class ActionName extends ActionSupport {

    public ActionForward execute(    ){

    ------------------------------------------------------------------------------
    ApplicationContext ctx= getWebApplicationContext();    

    BookService bookService = (BookService) ctx.getBean("bookService");     

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

   }
}

--

第二種:覆蓋 RequestProcessor

該方法使用 org.springframework.web.struts.DelegatingRequestProcessor 類來覆蓋 Struts 的 RequestProcessor 處理常式,通過 Spring 的 DelegatingRequestProcessor 進行整合,看下面的struts-config.xml檔案的主要配置部分:

<form-beans>
 <controller processorClass="org.springframework.web.struts.
       DelegatingRequestProcessor"/>

<plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"

      value="/WEB-INF/applicationContext.xml"/>
 </plug-in>

<form-beans/>

此方法比第一種要好,但如果您使用一個不同的 RequestProcessor,則需要手動整合 Spring 的 DelegatingRequestProcessor,添加的代碼會造成維護的麻煩並且將來會降低您的應用程式的靈活性。

--

第三種:將動作管理委託給 Spring

這裡列出struts-config.xml中的主要部分:

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

      ...............
      </action> 

<plug-in
    className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation"

       value="/WEB-INF/application.xml"/>
 </plug-in>

此方法是三種方法中最好的

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.