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

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

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


    關于Spring , Struts結合學習。
    一、前言
    剛剛接觸了日本一個項目,用的框架名稱是Northland Framework,主要用到以下部分
    Struts、Spring、iBATISVelocityStrutsSpring如何結合在一起?
    二、 Spring 提供了三種整合 Struts 的方法:
    使用 Spring ActionSupport 類整合 Structs
    使用 Spring DelegatingRequestProcessor 覆蓋 Struts RequestProcessor
    Struts Action 管理委托給 Spring 框架
    ( 參見 Get a better handle on Struts actions, with Spring
    http://www-128.ibm.com/developerworks/java/library/j-sr2.html?ca=drs-tp4105
    對應還有譯文:
    http://gocom.primeton.com/modules/techresource/article504.htm?utm_campaign=searchengine&utm_source=baidu&utm_medium=jjpm&utm_term=Spring+Struts )
    三、我只關心第三種整合方法:
    這種方法通過 Spring 提供的兩個和 Struts 相關類來實現: org.springframework.web.struts. DelegatingActionProxy org.springframework.web.struts. ContextLoaderPlugIn
    ContextLoaderPlugIn 實現 Struts PlugIn 接口,只要在 struts-config.xml 中有如下配置:
    ?
    < action ??? path = "/searchSubmit" >
    type = "ca.nexcel.books.actions.DelegatingActionProxy"
    ???????????? 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 >
    < plug-in className = "org.springframework.web.struts.ContextLoaderPlugIn" >
    ??? < set-property property = "contextConfigLocation" value = "/WEB-INF/beans.xml" />
    ? </ plug-in >
    ActionServlet 裝載的時候就可以順便裝載和 Spring 相關的 beans.xml ,和 beans.xml 中相關的一個東西叫做 WebApplicationContext , ( Spring 里關鍵就是取得 WebApplicationContext ,取得這個也就可以用 Spring 管理業務 ) ,在 ContextLoaderPlugIn 中是這樣保存 WebApplicationContext
    String attrName = getServletContextAttributeName();
    getServletContext().setAttribute(attrName, wac);
    再看 DelegatingActionProxy ,它繼承于 Struts Action ,以后 struts-config.xml 中所有的
    Action-mapping 都要指向它,只是每個 Action-mapping path 不同,將來也是用這個 path 來區分究竟需要執行 beans.xml 中的那個類。如下代碼:
    public ActionForward execute(){
    ??????????????? Action delegateAction = getDelegateAction(mapping);
    ??????????????? return delegateAction.execute(mapping, form, request, response);
    ??????? }
    這里的 delegateAction 就是 beans.xml 中一個相關的類 (beans.xml 也要求類繼承于 Struts Action) 去看看怎么得到 delegateAction
    protected Action getDelegateAction(ActionMapping mapping) throws BeansException {
    WebApplicationContext wac = getWebApplicationContext(getServlet(),
    ??? mapping.getModuleConfig());
    String beanName = determineActionBeanName(mapping);
    return (Action) wac.getBean(beanName, Action.class);
    }
    ?
    是如何取得 WebApplicationContext 呢:
    wac=(WebApplicationContext)actionServlet.getServletContext().getAttribute(??????? ContextLoaderPlugIn.SERVLET_CONTEXT_PREFIX);
    ?
    SERVLET_CONTEXT_PREFIX 正是 前邊提到的 ContextLoaderPlugIn attrName
    現在這個原理一目了然, ContextLoaderPlugIn actionServlet 初始化過程中保存
    起來留到后面供 DelegatingActionProxy 用。
    ?
    四、在另一篇文章中提到在上面的方法中 OpenSessionInView Filter 不能用
    ?( 參照 http://wyyhzc.itpub.net/) ,這個東西我也不熟悉,是不是有不少 Spring 的東西在這種方式中都不能用呢? 這就說到另一種取得 Spring WebApplicationContext 的方法:
    web.xml 中配置 ContextLoaderListener
    < context-param >
    ??? < param-name > contextConfigLocation </ param-name >
    ??? < param-value >
    ????? /WEB-INF/beans.xml
    ??? </ param-value >
    ? </ context-param >
    ? < listener >
    < listener-class > org.springframework.web.util.Log4jConfigListener </ listener-class >
    ? </ listener >
    ? < listener >
    < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
    ? </ listener >
    對應的 beans.xml 和前邊那個一樣, Log4jConfigListener 先不用管,去查看相關文檔。
    Web 服務啟動的時候,我們去看看 ContextLoaderListener 作了什么:
    WebApplicationContext = createWebApplicationContext(servletContext, parent);
    servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
    同樣是保存 WebApplicationContext ,但是 key ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE
    ?
    怎么才能不用 ContextLoaderPlugIn 而只用 ContextLoaderListener 下面我修改
    org.springframework.web.struts. DelegatingActionProxy 把它修改成
    ca.nexcel.books.actions. DelegatingActionProxy 并且修改一下代碼:
    修改 getWebApplicationContext 方法
    Return DelegatingActionUtils.findRequiredWebApplicationContext(actionServlet,
    ?moduleConfig); 換成下邊方法 ?
    ?
    ServletContext sc = actionServlet.getServletContext();
    WebApplicationContext wac = null;
    wac = WebApplicationContextUtils.getWebApplicationContext(sc);
    return wac;
    并且在 struts-config.xml 中將 action type 指向自己的
    ca.nexcel.books.actions. DelegatingActionProxy PlugIn 刪除 web.xml 加上剛才提到的 Listener ,啟動 tomcat 運行一切正常。
    ?
    五、我把 northland 的配置文件貼出來。
    Struts-config.xml
    < action-mappings >
    ??? < action
    ??????? path = "/list"
    ??????? input = "/list.jsp"
    ??????? name = "_list"
    ??????? scope = "request"
    ??????? type = "jp.co.nec.hnes.northland.web.struts.FlowAction"
    ??????? >
    ????? < display-name > 一覧畫面 </ display-name >
    ??? </ action >
    ??? < action
    ??????? path = "/register"
    ??????? input = "/register.jsp"
    ??????? name = "_register"
    ??????? scope = "request"
    ??????? type = "jp.co.nec.hnes.northland.web.struts.FlowAction"
    ??????? >
    ????? < display-name > 登録畫面 </ display-name >
    ??? </ action >
    ?
    Web.xml:
    ? < context-param >
    ??? < param-name > contextConfigLocation </ param-name >
    ??? < param-value >
    ????? classpath:flowConfig.xml,
    ????? classpath:viewConfig.xml,
    ????? classpath:applicationContext.xml,
    ?????classpath:applicationContext-extra.xml
    ??? </ param-value >
    ? </ context-param >  
    < listener >
    < listener-class > org.springframework.web.util.Log4jConfigListener </ listener-class >
    ? </ listener >
    ? < listener >
    < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
    ? </ listener >
    ? < servlet >
    ??? < servlet-name > ActionServlet </ servlet-name >
    < servlet-class > org.apache.struts.action.ActionServlet </ servlet-class >
    ??? < init-param >
    ????? < param-name > config </ param-name >
    ????? < param-value > /WEB-INF/struts-config.xml </ param-value >
    ??? </ init-param >
    ? </ servlet >
    ?
    從中可以看到
    其中的 jp.co.nec.hnes.northland.web.struts.FlowAction
    ca.nexcel.books.actions. DelegatingActionProxy 的功能差不多。
    ?
    posted on 2006-09-21 15:04 jackstudio 閱讀(3155) 評論(0)  編輯  收藏 所屬分類: commonstrutsspring
    主站蜘蛛池模板: 无码专区—VA亚洲V天堂| 男女一边桶一边摸一边脱视频免费 | a级成人毛片免费图片| 亚洲男人天堂2022| 亚洲国产女人aaa毛片在线| 免费播放春色aⅴ视频| 无码区日韩特区永久免费系列| 91视频免费观看高清观看完整| 在线亚洲v日韩v| 亚洲黄色激情视频| 亚洲色欲或者高潮影院| 亚洲va中文字幕无码久久不卡| 亚洲国产精品一区二区三区久久| 成人黄页网站免费观看大全| 8090在线观看免费观看| 久久国产精品免费专区| 国产亚洲精品免费视频播放| 美女18毛片免费视频| 亚洲av无码成人精品国产| 亚洲宅男精品一区在线观看| 亚洲短视频在线观看| 久久久久亚洲av无码专区导航| 亚洲av日韩av激情亚洲| 国产亚洲AV无码AV男人的天堂| 亚洲乱码中文字幕综合234 | 免费国产高清毛不卡片基地| 亚洲午夜精品久久久久久app| 亚洲a∨无码男人的天堂| 亚洲精品**中文毛片| 亚洲欧洲在线播放| 亚洲成a人片在线观| 亚洲综合激情九月婷婷| 亚洲精品无码久久久久久久| 亚洲欧洲精品一区二区三区| 亚洲色欲www综合网| 亚洲成人黄色在线观看| 亚洲一级片在线播放| 亚洲综合色丁香婷婷六月图片 | 日韩成人免费在线| 久久久亚洲欧洲日产国码是AV| 国产成A人亚洲精V品无码性色|