小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Spring,Struts整合方法

 longssl 2007-10-15
 
關(guān)于Spring , Struts結(jié)合學習。
一、前言
剛剛接觸了日本一個項目,用的框架名稱是Northland Framework,主要用到以下部分
Struts、Spring、iBATIS、Velocity。Struts、Spring如何結(jié)合在一起?
二、Spring提供了三種整合Struts的方法:
使用 Spring ActionSupport 類整合 Structs
使用 Spring DelegatingRequestProcessor 覆蓋 Struts RequestProcessor
Struts Action 管理委托給 Spring 框架
(參見Get a better handle on Struts actions, with Spring
對應還有譯文:
三、我只關(guān)心第三種整合方法:
這種方法通過Spring提供的兩個和Struts相關(guān)類來實現(xiàn):org.springframework.web.struts. DelegatingActionProxy,org.springframework.web.struts. ContextLoaderPlugIn
ContextLoaderPlugIn實現(xiàn)StrutsPlugIn接口,只要在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相關(guān)的beans.xml,和beans.xml中相關(guān)的一個東西叫做WebApplicationContext , (Spring里關(guān)鍵就是取得WebApplicationContext,取得這個也就可以用Spring管理業(yè)務(wù)),在ContextLoaderPlugIn中是這樣保存WebApplicationContext
String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac);
再看DelegatingActionProxy,它繼承于StrutsAction,以后struts-config.xml中所有的
Action-mapping都要指向它,只是每個Action-mappingpath不同,將來也是用這個path來區(qū)分究竟需要執(zhí)行beans.xml中的那個類。如下代碼:
public ActionForward execute(){
                Action delegateAction = getDelegateAction(mapping);
                return delegateAction.execute(mapping, form, request, response);
        }
這里的delegateAction就是beans.xml中一個相關(guān)的類(beans.xml也要求類繼承于StrutsAction) 去看看怎么得到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 正是 前邊提到的ContextLoaderPlugInattrName
現(xiàn)在這個原理一目了然,ContextLoaderPlugInactionServlet初始化過程中保存
起來留到后面供DelegatingActionProxy用。
 
四、在另一篇文章中提到在上面的方法中OpenSessionInView Filter不能用
 (參照http://wyyhzc./),這個東西我也不熟悉,是不是有不少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先不用管,去查看相關(guān)文檔。
Web服務(wù)啟動的時候,我們?nèi)タ纯?/span>ContextLoaderListener作了什么:
WebApplicationContext = createWebApplicationContext(servletContext, parent);
servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, this.context);
同樣是保存WebApplicationContext,但是keyROOT_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中將actiontype指向自己的
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的功能差不多。
 

    本站是提供個人知識管理的網(wǎng)絡(luò)存儲空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點。請注意甄別內(nèi)容中的聯(lián)系方式、誘導購買等信息,謹防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點擊一鍵舉報。
    轉(zhuǎn)藏 分享 獻花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多