Spring與Struts的結合運用

來源:互聯網
上載者:User

  Jakarta-Struts是Apache軟體組織提供的一個開源項目.它為Java Web應用提供了基於Model-View-Controller的MVC架構,尤其適用於開發大型可擴充的Web應用.儘管基於Java的MVC架構層出不窮,事實上Spring的MVC模型也提供了驅動應用系統Web層的能力,但Jakarta-Struts仍然是所有這些架構中的佼佼者.
    Spring是一個輕量級(大小和系統開支的角度)的IoC和AOP容器.它力圖簡化J2EE開發即J2EE without EJB.而且作為協助企業級開發的核心支柱,Spring為模型層(OR持久層:Hibernate、JDO、iBatis等)服務層(EJB、JNDI、WebService)以及表現層(Struts、JSF、Velocity)都提供了良好的支援和整合方案. 

    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">

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.