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

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

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

    posts - 33, comments - 46, trackbacks - 0, articles - 2

    集成struts+spring的新思路

    Posted on 2007-01-11 17:27 在路上... 閱讀(2206) 評論(7)  編輯  收藏 所屬分類: JAVA相關

    網上有好多種struts+spring的集成思路,例如 http://dev.csdn.net/author/hql638/35679289a9a94e4f97e999508df064db.html
    這篇文章就介紹得很詳細,介紹了下面三種方法:

    使用 Spring 的 ActionSupport 類整合 Structs
    使用 Spring 的 DelegatingRequestProcessor 覆蓋 Struts 的 RequestProcessor
    將 Struts Action 管理委托給 Spring 框架
    其實在使用spring+struts時,我們往往就是想使用Ioc的特性,減少業務邏輯組件之間的依賴關系,通過高度靈活的XML配置提高業務的靈活性和擴展性。步驟如下:
    首先依舊加入spring的context plugin到struts-config.xml中

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


    然后包裝一下struts的DispatchAction,提供一個方法可以直接獲取Spring的WebApplicationContext對象。

    package ?com.cngd.dataview.action;?

    import ?org.apache.struts.actions.DispatchAction;
    import ?org.springframework.web.context.WebApplicationContext;
    import ?org.springframework.web.struts.DelegatingActionUtils;?

    /** ? */ /**
    ?*?Date:?2007-1-11?16:57:48
    ?*
    ?*?
    @author ?midea0978
    ?*?
    @version ?1.0
    ?
    */

    public ? class ?CommDispatchAction? extends ?DispatchAction? {
    ????
    protected ?WebApplicationContext?getAppContext()? {
    ????????WebApplicationContext?context?
    = ?DelegatingActionUtils.findRequiredWebApplicationContext( this .getServlet(),? null );
    ????????
    return ?context;
    ????}

    }
    ?


    然后自己的action可以直接從CommDispatchAction繼承通過this.getAppContext();獲取WebApplicationContext,這樣對原有的
    struts程序架構體系幾乎沒有太大的變化,同時可以引入spring的Ioc特性到現有系統中,這個與ActionSupport 中的
    getWebApplicationContext()方法類似了,但是可以不必拘泥于在兩者之間轉來轉去的。

    package ?com.cngd.dataview.action;?

    import ?com.spring.bo.WeatherService;
    import ?org.apache.log4j.Logger;
    import ?org.apache.struts.action.ActionForm;
    import ?org.apache.struts.action.ActionForward;
    import ?org.apache.struts.action.ActionMapping;
    import ?org.springframework.jdbc.core.JdbcTemplate;
    import ?org.springframework.jdbc.datasource.DriverManagerDataSource;
    import ?org.springframework.web.context.WebApplicationContext;?

    import ?javax.servlet.http.HttpServletRequest;
    import ?javax.servlet.http.HttpServletResponse;?

    /** ? */ /**
    ?*?Date:?2007-1-11?16:19:15
    ?*
    ?*?
    @author ?midea0978
    ?*?
    @version ?1.0
    ?
    */

    public ? class ?DataViewAction? extends ?CommDispatchAction? {
    ????
    static ?Logger?logger? = ?Logger.getLogger(DataViewAction. class .getName());?

    ????
    /** ? */ /**
    ?????*?
    @param ?actionMapping
    ?????*?
    @param ?actionForm
    ?????*?
    @param ?request
    ?????*?
    @param ?response
    ?????*?
    @return
    ?????*?
    @throws ?Exception
    ?????
    */

    ????
    public ?ActionForward?genReport(ActionMapping?actionMapping,?ActionForm?actionForm,?HttpServletRequest?request,?HttpServletResponse?response)
    ????????????
    throws ?Exception? {
    ????????String?yymm?
    = ?request.getParameter( " yymm " );
    ????????String?opname?
    = ?request.getParameter( " opname " );
    ????????logger.info(
    " 參數: " ? + ?yymm? + ? " , " ? + ?opname);
    ????????WebApplicationContext?ctx?
    = ? this .getAppContext();
    ????????WeatherService?srv?
    = ?(WeatherService)?ctx.getBean( " weatherServiceBean " );
    ????????srv.showWeather();
    ????????DriverManagerDataSource?ds?
    = ?(DriverManagerDataSource)?ctx.getBean( " datasource " );
    ????????JdbcTemplate?jt?
    = ? new ?JdbcTemplate(ds);
    ????????String?sql?
    = ? " select?count(*)?from?tab " ;
    ????????
    int ?rows? = ?jt.queryForInt(sql);
    ????????System.out.println(rows);
    ????????
    return ?actionMapping.findForward( " viewresult " );
    ????}
    ?


    }
    ?

    Feedback

    # re: 集成struts+spring的新思路  回復  更多評論   

    2007-01-12 09:04 by jackhlp
    這樣集成的話,具體的每一個Action都與Spring耦合在一起了,因為你繼承DispatchAction的類里面返回的是一個WebApplicationContext。

    # re: 集成struts+spring的新思路  回復  更多評論   

    2007-01-13 21:53 by Christ Chang
    這個也算新思路?????????

    # re: 集成struts+spring的新思路  回復  更多評論   

    2007-01-13 22:47 by 千山鳥飛絕
    同意一樓的說法,你這樣做,并沒有很好的利用spring的輕耦合的特點。
    IOC的目的之一也就是降低耦合。

    所以不覺得你這樣做有多好。

    # re: 集成struts+spring的新思路[未登錄]  回復  更多評論   

    2007-01-22 14:02 by hiswing
    不管怎么說,還是值得鼓勵的。

    # re: 集成struts+spring的新思路  回復  更多評論   

    2007-01-27 19:02 by 別出風頭了你
    其實這是模仿webwork和springMVC的一種和手段,目的就是為了,讓action中service不需要任何方式,只需要getter和setter的方式就可以得到,其實這也是為病入膏肓的struts1,一種補救,
    其實如果有必要的話,你可以嘗試使用struts2,基于webwork的技術,完全整合如spring,不需要任何東西,其實struts2的好處,不僅僅這么點,具體的需要你自己領悟了。

    # re: 集成struts+spring的新思路  回復  更多評論   

    2007-01-28 12:50 by 剛才整合兩種
    我也是你這么整合的,但好像他們說這樣不好。具體我也不知道。。郁悶呀。那大家提供一個好的方法來整合咯。。我要 學習!

    # re: 集成struts+spring的新思路  回復  更多評論   

    2007-07-15 12:02 by sclsch
    如果你可繼承一個action,而不是dispatchaction,怎么辦?
    主站蜘蛛池模板: 亚洲高清在线视频| 精品97国产免费人成视频| 久久精品国产99精品国产亚洲性色| 在线观看免费为成年视频| 18pao国产成视频永久免费| 国产一级一毛免费黄片| 日韩电影免费在线观看网址 | 日韩大片免费观看视频播放| 亚洲熟女精品中文字幕| 亚洲女人影院想要爱| 亚洲国产综合专区电影在线| 国产亚洲AV手机在线观看| 免费在线一级毛片| 日韩免费一区二区三区| 国产精品免费观看久久| 希望影院高清免费观看视频| 最近中文字幕电影大全免费版 | 亚洲精品国产自在久久| 国产成人aaa在线视频免费观看 | 亚洲看片无码在线视频| 亚洲精品美女在线观看播放| 亚洲毛片在线观看| 亚洲AV日韩AV高潮无码专区| 亚洲AV无码乱码在线观看裸奔 | 在线免费观看h片| 久久免费观看视频| 久久久精品国产亚洲成人满18免费网站 | 亚洲国产成人精品无码区在线秒播| 图图资源网亚洲综合网站| 亚洲avav天堂av在线不卡| 亚洲Av无码精品色午夜| 亚洲国产女人aaa毛片在线| 亚洲大片在线观看| 亚洲精品视频在线免费| 亚洲成电影在线观看青青| 亚洲欧洲国产精品久久| 77777亚洲午夜久久多喷| 亚洲高清一区二区三区电影| 亚洲av无码专区首页| 高潮毛片无遮挡高清免费| 丝瓜app免费下载网址进入ios |