<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 【第十章】集成其它Web框架 之 10.2 集成Struts1.x ——跟我學spring3 先進行通用配置,?[【第十章】集成其它Web框架 之 10.1 概述](http://sishuok.com/forum/blogPost/list/2510.html "【第十章】集成其它Web框架 之 10.1 概述 ——跟我學spring3")? ## 10.2? 集成Struts1.x ### 10.2.1? 概述 Struts1.x是最早實現MVC(模型-視圖-控制器)模式的Web框架之一,其使用非常廣泛,雖然目前已經有Struts2.x等其他Web框架,但仍有很多公司使用Struts1.x框架。 集成Struts1.x也非常簡單,除了通用配置外,有兩種方式可以將Struts1.x集成到Spring中: * 最簡單集成:使用Spring提供的WebApplicationContextUtils工具類中的獲取Spring Web容器,然后通過Spring Web容器獲取Spring管理的Bean; * Struts1.x插件集成:利用Struts1.x中的插件ContextLoaderPlugin來將Struts1.x集成到Spring中。 **接下來讓我們首先讓我們準備Struts1x所需要的jar包:** **1.1、從下載的spring-framework-3.0.5.RELEASE-with-docs.zip中dist目錄查找如下jar包,該jar包用于提供集成struts1.x所需要的插件實現等:** + org.springframework.web.struts-3.0.5.RELEASE.jar? **1.2、從下載的spring-framework-3.0.5.RELEASE-dependencies.zip中查找如下依賴jar包,該組jar是struts1.x需要的jar包:** + com.springsource.org.apache.struts-1.2.9.jar ? ? ? ? ? ? ? ? ? ? ?//struts1.2.9實現包 + com.springsource.org.apache.commons.digester-1.8.1.jar? ??//用于解析struts配置文件 + com.springsource.org.apache.commons.beanutils-1.8.0.jar?? //用于請求參數綁定 + com.springsource.javax.servlet-2.5.0.jar ? ? ? ? ? ? ? ? ? ? ? ? ? ?//Servlet 2.5 API + antlr.jar ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//語法分析包(已有) + commons-logging.jar ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //日志記錄組件包(已有) + servlet-api.jar ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //Servlet API包(已有) + jsp-api.jar ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//JSP API包(已有,可選) + **commons-validator.jar ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //驗證包(可選)** + **commons-fileupload.jar ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? //文件上傳包(可選)** ### 10.2.2? 最簡單集成 只使用通用配置,利用WebApplicationContextUtils提供的獲取Spring Web容器方法獲取Spring Web容器,然后從Spring Web容器獲取Spring管理的Bean。 **1、?第一個Action實現:** ``` package cn.javass.spring.chapter10.struts1x.action; import org.apache.struts.action.Action; //省略部分import public class HelloWorldAction1 extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { WebApplicationContext ctx = WebApplicationContextUtils. getRequiredWebApplicationContext(getServlet().getServletContext()); String message = ctx.getBean("message", String.class); request.setAttribute("message", message); return mapping.findForward("hello"); } } ``` 此Action實現非常簡單,首先通過WebApplicationContextUtils獲取Spring Web容器,然后從Spring Web容器中獲取“message”Bean并將其放到request里,最后轉到“hello”所代表的jsp頁面。 **2、JSP頁面定義(webapp/WEB-INF/jsp/hello.jsp):** ``` <%@ page language="java" pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <title>Hello World</title> </head> <body> ${message} </body> </html> ``` **3、配置文件定義:** **3.1、Spring配置文件定義(resources/chapter10/applicationContext-message.xml):** 在此配置文件中定義我們使用的“message”Bean; ``` <bean id="message" class="java.lang.String"> <constructor-arg index="0" value="Hello Spring"/> </bean> ``` **3.2、struts配置文件定義(resources/chapter10/struts1x/struts-config.xml):** ``` <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <action-mappings> <action path="/hello" type="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction1"> <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/> </action> </action-mappings> </struts-config> ``` **3.3、web.xml部署描述符文件定義(webapp/WEB-INF/web.xml)添加如下內容:** ``` <!-- Struts1.x前端控制器配置開始 --> <servlet> <servlet-name>hello</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value> /WEB-INF/classes/chapter10/struts1x/struts-config.xml </param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hello</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- Struts1.x前端控制器配置結束 --> ``` Struts1.x前端控制器配置了ActionServlet前端控制器,其攔截以.do開頭的請求,Strut配置文件通過初始化參數“config”來指定,如果不知道“config”參數則默認加載的配置文件為“/WEB-INF/ struts-config.xml”。 **4、執行測試:**在Web瀏覽器中輸入http://localhost:8080/hello.do可以看到“Hello Spring”信息說明測試正常。 有朋友想問,我不想使用這種方式,我想在獨立環境內測試,沒關系,您只需將spring/lib目錄拷貝到spring/webapp/WEB-INF/下,然后將webapp拷貝到如tomcat中即可運行,嘗試一下吧。 Spring還提供ActionSupport類來簡化獲取WebApplicationContext,Spring為所有標準Action類及子類提供如下支持類,即在相應Action類后邊加上Support后綴: * ActionSupport * DispatchActionSupport * LookupDispatchActionSupport * MappingDispatchActionSupport 具體使用方式如下: **1、Action定義** ``` package cn.javass.spring.chapter10.struts1x.action; //省略import public class HelloWorldAction2 extends ActionSupport { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { WebApplicationContext ctx = getWebApplicationContext(); String message = ctx.getBean("message", String.class); request.setAttribute("message", message); return mapping.findForward("hello"); } } ``` 和第一個示例唯一不同的是直接調用**getWebApplicationContext()**即可獲得Spring Web容器。 **2、修改Struts配置文件(resources/chapter10/struts1x/struts-config.xml)添加如下Action定義:** ``` <action path="/hello2" type="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction2"> <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/> </action> ``` **3、啟動嵌入式Web服務器**并在Web瀏覽器中輸入http://localhost:8080/hello2.do可以看到“Hello Spring”信息說明Struts1集成成功。 這種集成方式好嗎?而且這種方式算是集成嗎?直接獲取Spring Web容器然后從該Spring Web容器中獲取Bean,暫且看作是集成吧,這種集成對于簡單操作可以接受,但更復雜的注入呢?接下來讓我們學習使用Struts插件進行集成。 ### 10.2.2? Struts1.x插件集成 Struts插件集成使用ContextLoaderPlugin類,該類用于為ActionServlet加載Spring配置文件。 **1、在Struts配置文件(resources/chapter10/struts1x/struts-config.xml)中配置插件:** ``` <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextClass" value="org.springframework.web.context.support.XmlWebApplicationContext"/> <set-property property="contextConfigLocation" value="/WEB-INF/hello-servlet.xml"/> <set-property property="namespace" value="hello"/> </plug-in> ``` * **contextClass:**可選,用于指定WebApplicationContext實現類,默認是XmlWebApplicationContext; * **contextConfigLocation:**指定Spring配置文件位置,如果我們的ActionServlet 在 web.xml 里面通過 &lt;servlet-name&gt;hello&lt;/servlet-name&gt;指定名字為“hello”,且沒有指定contextConfigLocation,則默認Spring配置文件是/WEB-INF/hello-servlet.xml; * **namespace:**因為默認使用ActionServlet在web.xml定義中的Servlet的名字,因此如果想要使用其他名字可以使用該變量指定,如指定“hello”,將加載的Spring配置文件為/WEB-INF/hello-servlet.xml; 由于我們的ActionServlet在web.xml中的名字為hello,而我們的配置文件在/WEB-INF/hello-servlet.xml,因此contextConfigLocation和namespace可以不指定,因此最簡單配置如下: ``` <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"/> ``` 通用配置的Spring Web容器將作為[ContextLoaderPlugin](http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/web/struts/ContextLoaderPlugIn.html)中創建的Spring Web容器的父容器存在,然而可以省略通用配置而直接在struts配置文件中通過[ContextLoaderPlugin](http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/web/struts/ContextLoaderPlugIn.html)插件指定所有配置文件。 插件已經配置了,那如何定義Action、配置Action、配置Spring管理Bean呢,即如何真正集成Spring+Struts1x呢?使用插件方式時Action將在Spring中配置而不是在Struts中配置了,Spring目前提供以下兩種方式: * 將Struts配置文件中的&lt;action&gt;的type屬性指定為DelegatingActionProxy,然后在Spring中配置同名的Spring管理的Action Bean; * 使用Spring提供的DelegatingRequestProcessor重載 Struts 默認的 RequestProcessor來從Spring容器中查找同名的Spring管理的Action Bean。 看懂了嗎?好像沒怎么看懂,那就直接上代碼,有代碼有真相。 **2、定義Action實現,由于Action將在Spring中配置,因此message可以使用依賴注入方式了:** ``` package cn.javass.spring.chapter10.struts1x.action; //省略 public class HelloWorldAction3 extends Action { private String message; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { request.setAttribute("message", message); return mapping.findForward("hello"); } public void setMessage(String message) {//有setter方法,大家是否想到setter注入 this.message = message; } } ``` **3、DelegatingActionProxy方式與Spring集成配置:** **3.1、在Struts配置文件(resources/chapter10/struts1x/struts-config.xml)中進行Action定義:** ``` <action path="/hello3" type="org.springframework.web.struts.DelegatingActionProxy"> <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/> </action> ``` **3.2、在Spring配置文件(webapp/WEB-INF/hello-servlet.xml)中定義Action對應的Bean:** ``` <bean name="/hello3" class="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3"> <property name="message" ref="message"/> </bean> ``` **3.3、啟動嵌入式Web服務器**并在Web瀏覽器中輸入http://localhost:8080/hello3.do可以看到“Hello Spring”信息說明測試正常。 從以上配置中可以看出: * Struts配置文件中&lt;action&gt;標簽的path屬性和Spring配置文件的name屬性應該完全一樣,否則錯誤; * Struts通過**DelegatingActionProxy**去到Spring Web容器中查找同名的Action Bean; 很簡單吧,DelegatingActionProxy是個代理Action,其實現了Action類,其內部幫我們查找相應的Spring管理Action Bean并把請求轉發給這個真實的Action。 **4、DelegatingRequestProcessor方式與Spring集成:** **4.1、首先要替換掉Struts默認的RequestProcessor,在Struts配置文件(resources/chapter10/struts1x/struts-config.xml)中添加如下配置:** ``` <controller> <set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor"/> </controller> ``` **4.2、在Struts配置文件(resources/chapter10/struts1x/struts-config.xml)中進行Action定義:** ``` <action path="/hello4" type=" cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3"> <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/> </action> ``` 或更簡單形式: ``` <action path="/hello4"> <forward name="hello" path="/WEB-INF/jsp/hello.jsp"/> </action> ``` **4.3、在Spring配置文件(webapp/WEB-INF/hello-servlet.xml)中定義Action對應的Bean:** ``` <bean name="/hello4" class="cn.javass.spring.chapter10.struts1x.action.HelloWorldAction3"> <property name="message" ref="message"/> </bean> ``` **4.4、啟動嵌入式Web服務器**并在Web瀏覽器中輸入http://localhost:8080/hello4.do可以看到“Hello Spring”信息說明Struts1集成成功。 從以上配置中可以看出: * Struts配置文件中&lt;action&gt;標簽的path屬性和Spring配置文件的name屬性應該完全一樣,否則錯誤; * Struts通過**DelegatingRequestProcessor**去到Spring Web容器中查找同名的Action Bean; 很簡單吧,只是由**DelegatingRequestProcessor**去幫我們查找相應的Action Bean,但沒有代理Action了,所以推薦使用該方式。 ![](https://box.kancloud.cn/2016-05-13_573547221ba61.JPG) 圖10-4 共享及專用Spring Web容器 Struts1x與Spring集成到此就完成了,在集成時需要注意一下幾點: * 推薦使用ContextLoaderPlugin+DelegatingRequestProcessor方式集成; * 當有多個Struts模塊時建議在通用配置部分配置通用部分,因為通用配置在正在Web容器中是可共享的,而在各個Struts模塊配置文件中配置是不可共享的,因此不推薦直接使用ContextLoaderPlugin中為每個模塊都指定所有配置,因為**ContextLoaderPlugin加載的Spring容器只對當前的ActionServlet有效對其他ActionServlet無效,**如圖10-4所示。 原創內容,轉載請注明出處【[http://sishuok.com/forum/blogPost/list/2511.html](http://sishuok.com/forum/blogPost/list/2511.html)】
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看