<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    posts - 1,  comments - 0,  trackbacks - 0

    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">
    (轉)

    posted on 2009-03-02 18:02 ID刀 閱讀(173) 評論(0)  編輯  收藏 所屬分類: Spring

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    常用鏈接

    留言簿(2)

    隨筆檔案(1)

    文章分類(21)

    文章檔案(17)

    最新隨筆

    搜索

    •  

    積分與排名

    • 積分 - 13651
    • 排名 - 2092

    最新評論

    主站蜘蛛池模板: 亚洲国产成人久久精品大牛影视| 亚洲国产午夜精品理论片在线播放 | 99久久久精品免费观看国产 | 成年美女黄网站色大免费视频| 老司机午夜免费视频| 亚洲色中文字幕无码AV| 亚洲一区二区免费视频| 色哟哟国产精品免费观看| 西西人体44rt高清亚洲| 成在人线AV无码免费| baoyu122.永久免费视频| 456亚洲人成在线播放网站| 国产aⅴ无码专区亚洲av麻豆 | 午夜精品免费在线观看| 亚洲av无码专区在线电影天堂| 亚洲精品无码久久久久sm| 最近免费中文字幕大全| 中文字幕在线免费视频| 亚洲国产日韩精品| 亚洲午夜久久久久妓女影院 | 色播在线永久免费视频| 日本免费一区二区久久人人澡| 亚洲一区二区三区写真| 亚洲成人午夜在线| 伊人久久亚洲综合影院| 在线a级毛片免费视频| 人人揉揉香蕉大免费不卡| 黄页免费视频播放在线播放| 亚洲人成综合在线播放| 亚洲五月综合缴情在线观看| 日韩一级免费视频| 免费观看激色视频网站bd| aaa毛片视频免费观看| 天天综合亚洲色在线精品| 亚洲AV综合色区无码二区偷拍| 国产亚洲精品a在线无码| 四虎影视永久免费观看地址 | 久久久久久亚洲AV无码专区| 亚洲精品久久久www| 午夜免费福利在线观看| 日韩中文字幕精品免费一区|