探討Spring與Struts 的集成方案

 探討Spring與Struts 的集成方案

Spring是一個輕量級(大小和系統開支的角度)的IoC和AOP容器.它力圖簡化J2EE開發即J2EE without EJB.而且作爲幫助企業級開發的核心支柱,Spring爲模型層(OR持久層:Hibernate、JDO、iBatis等)服務層(EJB、JNDI、WebService)以及表現層(Struts、JSF、Velocity)都提供了良好的支持和集成方案. 訪問Spring官方站

    Jakarta-Struts是Apache軟件組織提供的一個開源項目.它爲Java Web應用提供了基於Model-View-Controller的MVC框架,尤其適用於開發大型可擴展的Web應用.儘管基於Java的MVC框架層出不窮,事實上Spring的MVC模型也提供了驅動應用系統Web層的能力,但Jakarta-Struts仍然是所有這些框架中的佼佼者.

    下面,將如何整合這兩個J2EE領域的經典項目給出兩套詳盡的集成方案.

    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"> http://www.javass.cn/javapeixunssh/index.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章