使用 Spring 更好地處理 Struts 動做

Struts Recipes 的合著者 George Franciscus 將介紹另外一個重大的 Struts 整合竅門 —— 此次是將 Struts 應用程序導入 Spring 框架。請跟隨 George,他將向您展現如何改變 Struts 動做,使得管理 Struts 動做就像管理 Spring beans 那樣。結果是一個加強的 web 框架,這個框架能夠方便地利用 Spring AOP 的優點。

您確定已經據說過控制反轉 (IOC) 設計模式,由於很長一段時間以來一直在流傳關於它的信息。若是您在任何功能中使用過 Spring 框架,那麼您就知道其原理的做用。在本文中,我利用這一原理把一個 Struts 應用程序注入 Spring 框架,您將親身體會到 IOC 模式的強大。

將一個 Struts 應用程序整合進 Spring 框架具備多方面的優勢。首先,Spring 是爲解決一些關於 JEE 的真實世界問題而設計的,好比複雜性、低性能和可測試性,等等。第二,Spring 框架包含一個 AOP 實現,容許您將面向方面技術應用於面向對象的代碼。第三,一些人可能會說 Spring 框架只有處理 Struts 比 Struts 處理本身好。可是這是觀點問題,我演示三種將 Struts 應用程序整合到 Spring 框架的方法後,具體由您本身決定使用哪種。

我所演示的方法都是執行起來相對簡單的,可是它們卻具備明顯不一樣的優勢。我爲每一種方法建立了一個獨立而可用的例子,這樣您就能夠徹底理解每種方法。請參閱 下載 部分得到完整例子源代碼。請參閱 參考資料,下載 Struts MVC 和 Spring 框架。

爲何 Spring 這麼了不得?

Spring 的創立者 Rod Johnson 以一種批判的眼光看待 Java™ 企業軟件開發,而且提議不少企業難題都可以經過戰略地使用 IOC 模式(也稱做依賴注入)來解決。當 Rod 和一個具備奉獻精神的開放源碼開發者團隊將這個理論應用於實踐時,結果就產生了 Spring 框架。簡言之,Spring 是一個輕型的容器,利用它可使用一個外部 XML 配置文件方便地將對象鏈接在一塊兒。每一個對象均可以經過顯示一個 JavaBean 屬性收到一個到依賴對象的引用,留給您的簡單任務就只是在一個 XML 配置文件中把它們鏈接好。
   
IOC 和 Spring

IOC 是一種使應用程序邏輯外在化的設計模式,因此它是被注入而不是被寫入客戶機代碼中。將 IOC 與接口編程應用結合,就像 Spring 框架那樣,產生了一種架構,這種架構可以減小客戶機對特定實現邏輯的依賴。請參閱 參考資料 瞭解更多關於 IOC 和 Spring 的信息。

依賴注入是一個強大的特性,可是 Spring 框架可以提供更多特性。Spring 支持可插拔的事務管理器,能夠給您的事務處理提供更普遍的選擇範圍。它集成了領先的持久性框架,而且提供一個一致的異常層次結構。Spring 還提供了一種使用面向方面代碼代替正常的面向對象代碼的簡單機制。

Spring AOP 容許您使用攔截器 在一個或多個執行點上攔截應用程序邏輯。增強應用程序在攔截器中的日誌記錄邏輯會產生一個更可讀的、實用的代碼基礎,因此攔截器普遍用於日誌記錄。您很快就會看到,爲了處理橫切關注點,Spring AOP 發佈了它本身的攔截器,您也能夠編寫您本身的攔截器。

整合 Struts 和 Spring

與 Struts 類似,Spring 能夠做爲一個 MVC 實現。這兩種框架都具備本身的優勢和缺點,儘管大部分人贊成 Struts 在 MVC 方面仍然是最好的。不少開發團隊已經學會在時間緊迫的時候利用 Struts 做爲構造高品質軟件的基礎。Struts 具備如此大的推進力,以致於開發團隊寧願整合 Spring 框架的特性,而不肯意轉換成 Spring MVC。不必進行轉換對您來講是一個好消息。Spring 架構容許您將 Struts 做爲 Web 框架鏈接到基於 Spring 的業務和持久層。最後的結果就是如今一切條件都具有了。

在接下來的小竅門中,您將會了解到三種將 Struts MVC 整合到 Spring 框架的方法。我將揭示每種方法的缺陷而且對比它們的優勢。 一旦您瞭解到全部三種方法的做用,我將會向您展現一個使人興奮的應用程序,這個程序使用的是這三種方法中我最喜歡的一種。

三個小竅門

接下來的每種整合技術(或者竅門)都有本身的優勢和特色。我偏心其中的一種,可是我知道這三種都可以加深您對 Struts 和 Spring 的理解。在處理各類不一樣狀況的時候,這將給您提供一個廣闊的選擇範圍。方法以下:

 一、  使用 Spring 的 ActionSupport 類整合 Structs
 二、  使用 Spring 的 DelegatingRequestProcessor 覆蓋 Struts 的 RequestProcessor
 三、  將 Struts Action 管理委託給 Spring 框架


裝載應用程序環境

不管您使用哪一種技術,都須要使用 Spring 的 ContextLoaderPlugin 爲 Struts 的 ActionServlet 裝載 Spring 應用程序環境。就像添加任何其餘插件同樣,簡單地向您的 struts-config.xml 文件添加該插件,以下所示:

<plug-in className=
  "org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property=
      "contextConfigLocation" value="/WEB-INF/beans.xml"/>
 </plug-in>

前面已經提到過,在 下載 部分,您可以找到這三個徹底可以使用的例子的完整源代碼。每一個例子都爲一個書籍搜索應用程序提供一種不一樣的 Struts 和 Spring 的整合方法。您能夠在這裏看到例子的要點,可是您也能夠下載應用程序以查看全部的細節。

竅門 1. 使用 Spring 的 ActionSupport


手動建立一個 Spring 環境是一種整合 Struts 和 Spring 的最直觀的方式。爲了使它變得更簡單,Spring 提供了一些幫助。爲了方便地得到 Spring 環境,org.springframework.web.struts.ActionSupport 類提供了一個 getWebApplicationContext() 方法。您所作的只是從 Spring 的 ActionSupport 而不是 Struts Action 類擴展您的動做,如清單 1 所示:

清單 1. 使用 ActionSupport 整合 Struts

package ca.nexcel.books.actions;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.springframework.context.ApplicationContext;
import org.springframework.web.struts.ActionSupport;
import ca.nexcel.books.beans.Book;
import ca.nexcel.books.business.BookService;
public class SearchSubmit extends ActionSupport {   |(1)
  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {
    DynaActionForm searchForm = (DynaActionForm) form;
    String isbn = (String) searchForm.get("isbn");
       
    //the old fashion way
    //BookService bookService = new BookServiceImpl();
       
    ApplicationContext ctx =
      getWebApplicationContext();    |(2)
    BookService bookService =
      (BookService) ctx.getBean("bookService");   |(3)
       
  Book book = bookService.read(isbn.trim());
    if (null == book) {
      ActionErrors errors = new ActionErrors();
      errors.add(ActionErrors.GLOBAL_ERROR,new ActionError
        ("message.notfound"));
      saveErrors(request, errors);
      return mapping.findForward("failure") ;
  }
    request.setAttribute("book", book);
    return mapping.findForward("success");
  }
}


讓咱們快速思考一下這裏到底發生了什麼。在 (1) 處,我經過從 Spring 的 ActionSupport 類而不是 Struts 的 Action 類進行擴展,建立了一個新的 Action。在 (2) 處,我使用 getWebApplicationContext() 方法得到一個 ApplicationContext。爲了得到業務服務,我使用在 (2) 處得到的環境在 (3) 處查找一個 Spring bean。

這種技術很簡單而且易於理解。不幸的是,它將 Struts 動做與 Spring 框架耦合在一塊兒。若是您想替換掉 Spring,那麼您必須重寫代碼。而且,因爲 Struts 動做不在 Spring 的控制之下,因此它不能得到 Spring AOP 的優點。當使用多重獨立的 Spring 環境時,這種技術可能有用,可是在大多數狀況下,這種方法不如另外兩種方法合適。

竅門 2. 覆蓋 RequestProcessor


將 Spring 從 Struts 動做中分離是一個更巧妙的設計選擇。分離的一種方法是使用 org.springframework.web.struts.DelegatingRequestProcessor 類來覆蓋 Struts 的 RequestProcessor 處理程序,如清單 2 所示:

清單 2. 經過 Spring 的 DelegatingRequestProcessor 進行整合

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
 <form-beans>
    <form-bean name="searchForm"
      type="org.apache.struts.validator.DynaValidatorForm">
               <form-property name="isbn"    type="java.lang.String"/>
    </form-bean>
 
  </form-beans>
 <global-forwards type="org.apache.struts.action.ActionForward">
     <forward   name="welcome"                path="/welcome.do"/>
     <forward   name="searchEntry"            path="/searchEntry.do"/>
     <forward   name="searchSubmit"           path="/searchSubmit.do"/>
 </global-forwards>
 <action-mappings>
    <action    path="/welcome" forward="/WEB-INF/pages/welcome.htm"/>
    <action    path="/searchEntry" forward="/WEB-INF/pages/search.jsp"/>
    <action    path="/searchSubmit"
               type="ca.nexcel.books.actions.SearchSubmit"
               input="/searchEntry.do"
               validate="true"
               name="searchForm">
              <forward name="success" path="/WEB-INF/pages/detail.jsp"/>
              <forward name="failure" path="/WEB-INF/pages/search.jsp"/>
    </action> 
 </action-mappings>
 <message-resources parameter="ApplicationResources"/>
 <controller processorClass="org.springframework.web.struts.
   DelegatingRequestProcessor"/> |(1)
 <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property property="pathnames"
      value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
 </plug-in>
 <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="csntextConfigLocation" value="/WEB-INF/beans.xml"/>
 </plug-in>
 
</struts-config>

我利用了 <controller> 標記來用 DelegatingRequestProcessor 覆蓋默認的 Struts RequestProcessor。下一步是在個人 Spring 配置文件中註冊該動做,如清單 3 所示:

清單 3. 在 Spring 配置文件中註冊一個動做

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
  "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/>
  <bean name="/searchSubmit"
    class="ca.nexcel.books.actions.SearchSubmit"> |(1)
     <property name="bookService">
        <ref bean="bookService"/>
     </property>
  </bean>
</beans>

注意:在 (1) 處,我使用名稱屬性註冊了一個 bean,以匹配 struts-config 動做映射名稱。SearchSubmit 動做揭示了一個 JavaBean 屬性,容許 Spring 在運行時填充屬性,如清單 4 所示:

清單 4. 具備 JavaBean 屬性的 Struts 動做

package ca.nexcel.books.actions;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import ca.nexcel.books.beans.Book;
import ca.nexcel.books.business.BookService;
public class SearchSubmit extends Action {
   
  private BookService bookService;
  public BookService getBookService() {
    return bookService;
  }
  public void setBookService(BookService bookService) { | (1)
    this.bookService = bookService;
  }
  public ActionForward execute(
    ActionMapping mapping,
    ActionForm form,
    HttpServletRequest request,
    HttpServletResponse response)
    throws IOException, ServletException {
    DynaActionForm searchForm = (DynaActionForm) form;
    String isbn = (String) searchForm.get("isbn");
       
  Book book = getBookService().read(isbn.trim());  |(2)
    if (null == book) {
      ActionErrors errors = new ActionErrors();
      errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("message.notfound"));
      saveErrors(request, errors);
      return mapping.findForward("failure") ;
  }
      request.setAttribute("book", book);
      return mapping.findForward("success");
  }
}

在清單 4 中,您能夠了解到如何建立 Struts 動做。在 (1) 處,我建立了一個 JavaBean 屬性。DelegatingRequestProcessor自動地配置這種屬性。這種設計使 Struts 動做並不知道它正被 Spring 管理,而且使您可以利用 Sping 的動做管理框架的全部優勢。因爲您的 Struts 動做注意不到 Spring 的存在,因此您不須要重寫您的 Struts 代碼就可使用其餘控制反轉容器來替換掉 Spring。

DelegatingRequestProcessor 方法的確比第一種方法好,可是仍然存在一些問題。若是您使用一個不一樣的 RequestProcessor,則須要手動整合 Spring 的 DelegatingRequestProcessor。添加的代碼會形成維護的麻煩而且未來會下降您的應用程序的靈活性。此外,還有過一些使用一系列命令來代替 Struts RequestProcessor 的傳聞。 這種改變將會對這種解決方法的使用壽命形成負面的影響。

竅門 3. 將動做管理委託給 Spring

一個更好的解決方法是將 Strut 動做管理委託給 Spring。您能夠經過在 struts-config 動做映射中註冊一個代理來實現。代理負責在 Spring 環境中查找 Struts 動做。因爲動做在 Spring 的控制之下,因此它能夠填充動做的 JavaBean 屬性,併爲應用諸如 Spring 的 AOP 攔截器之類的特性帶來了可能。

清單 5 中的 Action 類與清單 4 中的相同。可是 struts-config 有一些不一樣:

清單 5. Spring 整合的委託方法

<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
          "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
          "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
 <form-beans>
    <form-bean name="searchForm"
      type="org.apache.struts.validator.DynaValidatorForm">
               <form-property name="isbn"    type="java.lang.String"/>
    </form-bean>
 
  </form-beans>
 <global-forwards type="org.apache.struts.action.ActionForward">
     <forward   name="welcome"                path="/welcome.do"/>
     <forward   name="searchEntry"            path="/searchEntry.do"/>
     <forward   name="searchSubmit"           path="/searchSubmit.do"/>
 </global-forwards>
 <action-mappings>
    <action    path="/welcome" forward="/WEB-INF/pages/welcome.htm"/>
    <action    path="/searchEntry" forward="/WEB-INF/pages/search.jsp"/>
    <action    path="/searchSubmit"
             type="org.springframework.web.struts.DelegatingActionProxy" |(1)
             input="/searchEntry.do"
             validate="true"
             name="searchForm">
             <forward name="success" path="/WEB-INF/pages/detail.jsp"/>
             <forward name="failure" path="/WEB-INF/pages/search.jsp"/>
    </action> 
 </action-mappings>
 <message-resources parameter="ApplicationResources"/>
 <plug-in className="org.apache.struts.validator.ValidatorPlugIn">
    <set-property
    property="pathnames"
    value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
 </plug-in>
 <plug-in
    className="org.springframework.web.struts.ContextLoaderPlugIn">
    <set-property property="contextConfigLocation" value="/WEB-INF/beans.xml"/>
 </plug-in>
 
</struts-config>

清單 5 是一個典型的 struts-config.xml 文件,只有一個小小的差異。它註冊 Spring 代理類的名稱,而不是聲明動做的類名,如(1)處所示。DelegatingActionProxy 類使用動做映射名稱查找 Spring 環境中的動做。這就是咱們使用 ContextLoaderPlugIn 聲明的環境。

將一個 Struts 動做註冊爲一個 Spring bean 是很是直觀的,如清單 6 所示。我利用動做映射使用 <bean> 標記的名稱屬性(在這個例子中是 "/searchSubmit")簡單地建立了一個 bean。這個動做的 JavaBean 屬性像任何 Spring bean 同樣被填充:

清單 6. 在 Spring 環境中註冊一個 Struts 動做

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
 "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/>
  <bean name="/searchSubmit"  
        class="ca.nexcel.books.actions.SearchSubmit">
     <property name="bookService">
        <ref bean="bookService"/>
     </property>
  </bean>
</beans>

動做委託的優勢

動做委託解決方法是這三種方法中最好的。Struts 動做不了解 Spring,不對代碼做任何改變就可用於非 Spring 應用程序中。RequestProcessor 的改變不會影響它,而且它能夠利用 Spring AOP 特性的優勢。

動做委託的優勢不止如此。一旦讓 Spring 控制您的 Struts 動做,您就可使用 Spring 給動做補充更強的活力。例如,沒有 Spring 的話,全部的 Struts 動做都必須是線程安全的。若是您設置 <bean> 標記的 singleton 屬性爲「false」,那麼無論用何種方法,您的應用程序都將在每個請求上有一個新生成的動做對象。您可能不須要這種特性,可是把它放在您的工具箱中也很好。您也能夠利用 Spring 的生命週期方法。例如,當實例化 Struts 動做時,<bean> 標記的 init-method 屬性被用於運行一個方法。相似地,在從容器中刪除 bean 以前,destroy-method 屬性執行一個方法。這些方法是管理昂貴對象的好辦法,它們以一種與 Servlet 生命週期相同的方式進行管理。

攔截 Struts


前面提到過,經過將 Struts 動做委託給 Spring 框架而整合 Struts 和 Spring 的一個主要的優勢是:您能夠將 Spring 的 AOP 攔截器應用於您的 Struts 動做。經過將 Spring 攔截器應用於 Struts 動做,您能夠用最小的代價處理橫切關注點。

雖然 Spring 提供不少內置攔截器,可是我將向您展現如何建立本身的攔截器並把它應用於一個 Struts 動做。爲了使用攔截器,您須要作三件事:

   1. 建立攔截器。
   2. 註冊攔截器。
   3. 聲明在何處攔截代碼。

這看起來很是簡單的幾句話卻很是強大。例如,在清單 7 中,我爲 Struts 動做建立了一個日誌記錄攔截器。 這個攔截器在每一個方法調用以前打印一句話:

清單 7. 一個簡單的日誌記錄攔截器

package ca.nexcel.books.interceptors;
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class LoggingInterceptor implements MethodBeforeAdvice {
   public void before(Method method, Object[] objects, Object o) throws Throwable {
        System.out.println("logging before!");
    }
}

這個攔截器很是簡單。before() 方法在攔截點中每一個方法以前運行。在本例中,它打印出一句話,其實它能夠作您想作的任何事。下一步就是在 Spring 配置文件中註冊這個攔截器,如清單 8 所示:

清單 8. 在 Spring 配置文件中註冊攔截器

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
  "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
  <bean id="bookService" class="ca.nexcel.books.business.BookServiceImpl"/>
  <bean name="/searchSubmit"
        class="ca.nexcel.books.actions.SearchSubmit">
     <property name="bookService">
        <ref bean="bookService"/>
     </property>
  </bean>
  <!--  Interceptors -->
  <bean name="logger"   
    class="ca.nexcel.books.interceptors.LoggingInterceptor"/> |(1)
  <!-- AutoProxies -->
  <bean name="loggingAutoProxy"
        class="org.springframework.aop.framework.autoproxy.
          BeanNameAutoProxyCreator"> |(2)
    <property name="beanNames">
          <value>/searchSubmit</valuesgt; |(3)
    </property>
    <property name="interceptorNames">
        <list>
          <value>logger</value> |(4)
        </list>
    </property>
   </bean>
</beans>

您可能已經注意到了,清單 8 擴展了 清單 6 中所示的應用程序以包含一個攔截器。具體細節以下:

    * 在 (1) 處,我註冊了這個攔截器。
    * 在 (2) 處,我建立了一個 bean 名稱自動代理,它描述如何應用攔截器。還有其餘的方法定義攔截點,可是這種方法常見而簡便。
    * 在 (3) 處,我將 Struts 動做註冊爲將被攔截的 bean。若是您想要攔截其餘的 Struts 動做,則只須要在 "beanNames" 下面建立附加的 <value> 標記。
    * 在 (4) 處,當攔截髮生時,我執行了在 (1) 處建立的攔截器 bean 的名稱。這裏列出的全部攔截器都應用於「beanNames」。

就是這樣。就像這個例子所展現的,將您的 Struts 動做置於 Spring 框架的控制之下,爲處理您的 Struts 應用程序提供了一系列全新的選擇。在本例中,使用動做委託能夠輕鬆地利用 Spring 攔截器提升 Struts 應用程序中的日誌記錄能力。

結束語

在本文中,您已經學習了將 Struts 動做整合到 Spring 框架中的三種竅門。使用 Spring 的 ActionSupport 來整合 Struts(第一種竅門中就是這樣作的)簡單而快捷,可是會將 Struts 動做與 Spring 框架耦合在一塊兒。若是您須要將應用程序移植到一個不一樣的框架,則須要重寫代碼。第二種解決方法經過委託 RequestProcessor 巧妙地解開代碼的耦合,可是它的可擴展性不強,而且當 Struts 的 RequestProcessor 變成一系列命令時,這種方法就持續不了很長時間。第三種方法是這三種方法中最好的:將 Struts 動做委託給 Spring 框架可使代碼解耦,從而使您能夠在您的 Struts 應用程序中利用 Spring 的特性(好比日誌記錄攔截器)。

三種 Struts-Spring 整合竅門中的每一種都被實現成一個完整可用的應用程序。請參閱 下載 部分仔細研究它們。
java

相關文章
相關標籤/搜索