<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.1 概述 ——跟我學spring3 ## 10.1? 概述 ### 10.1.1? Spring和Web框架 Spring框架不僅提供了一套自己的Web框架實現,還支持集成第三方Web框架(如Struts1x、Struts2x)。 Spring實現的SpringMVC Web框架將在第十八章詳細介紹。 由于現在有很大部分公司在使用第三方Web框架,對于并不熟悉SpringMVC Web框架的公司,為了充分利用開發人員已掌握的技術并相使用Spring的功能,想集成所使用的Web框架;由于Spring框架的高度可配置和可選擇性,因此集成這些第三方Web框架是非常簡單的。 之所以想把這些第三方Web框架集成到Spring中,最核心的價值是享受Spring的某些強大功能,如一致的數據訪問,事務管理,IOC,AOP等等。 Spring為所有Web框架提供一致的通用配置,從而不管使用什么Web框架都使用該通用配置。 ### 10.1.2 ?通用配置 Spring對所有Web框架抽象出通用配置,以減少重復配置,其中主要有以下配置: **1、Web環境準備:** **1.1、在spring項目下創建如圖10-1目錄結構:** ![](https://box.kancloud.cn/2016-05-13_57354721c1e7b.JPG) 圖10-1 web目錄結構 **1.2、右擊spring項目選擇【Propeties】,然后選擇【Java Build Path】中的【Source】選項卡,將類輸出路徑修改為“spring/webapp/WEB-INF/classes”,如圖10-2所示:** ![](https://box.kancloud.cn/2016-05-13_57354721d498e.JPG)? 圖10-2 修改類輸出路徑 **1.3、web.xml初始內容如下:** ``` <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> </web-app> ``` &lt;web-app version="2.4"&gt;表示采用Servlet 2.4規范的Web程序部署描述格式 **2、?指定Web應用上下文實現:**在Web環境中,Spring提供WebApplicationContext(繼承ApplicationContext)接口用于配置Web應用,該接口應該被實現為在Web應用程序運行時只讀,即在初始化完畢后不能修改Spring Web容器(WebApplicationContext),但可能支持重載。 Spring提供XmlWebApplicationContext實現,并在Web應用程序中默認使用該實現,可以通過在web.xml配置文件中使用如下方式指定: ``` <context-param> <param-name>contextClass</param-name> <param-value> org.springframework.web.context.support.XmlWebApplicationContext </param-value> </context-param> ``` 如上指定是可選的,只有當使用其他實現時才需要顯示指定。 **3、?指定加載文件位置:** 前邊已經指定了Spring Web容器實現,那從什么地方加載配置文件呢? 默認情況下將加載/WEB-INF/applicationContext.xml配置文件,當然也可以使用如下形式在web.xml中定義要加載自定義的配置文件,多個配置文件用“,”分割: ``` <context-param> <param-name>contextConfigLocation</param-name> <param-value> classpath:chapter10/applicationContext-message.xml </param-value> </context-param> ``` 通用Spring配置文件(resources/chapter10/applicationContext-message.xml)內容如下所示: ``` <bean id="message" class="java.lang.String"> <constructor-arg index="0" value="Hello Spring"/> </bean> ``` **4、?加載和關閉Spring Web容器:** 我們已經指定了Spring Web容器實現和配置文件,那如何才能讓Spring使用相應的Spring Web容器實現加載配置文件呢? Spring使用ContextLoaderListener監聽器來加載和關閉Spring Web容器,即使用如下方式在web.xml中指定: ``` <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener> ``` ContextLoaderListener監聽器將在Web應用啟動時使用指定的配置文件初始化Spring Web容器,在Web應用關閉時銷毀Spring Web容器。 注:監聽器是從Servlet 2.3才開始支持的,因此如果Web應用所運行的環境是Servlet 2.2版本則可以使用ContextLoaderServlet來完成,但從Spring3.x版本之后ContextLoaderServlet被移除了。 **5、?在Web環境中獲取Spring Web容器:** 既然已經定義了Spring Web容器,那如何在Web中訪問呢?Spring提供如下方式來支持獲取Spring Web容器(WebApplicationContext): ``` WebApplicationContextUtils.getWebApplicationContext(servletContext); 或 WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); ``` 如果當前Web應用中的ServletContext 中沒有相應的Spring Web容器,對于getWebApplicationContext()方法將返回null,而getRequiredWebApplicationContext()方法將拋出異常,建議使用第二種方式,因為缺失Spring Web容器而又想獲取它,很明顯是錯誤的,應該拋出異常。 **6、?通用jar包,從下載的spring-framework-3.0.5.RELEASE-with-docs.zip中dist目錄查找如下jar包:** + org.springframework.web-3.0.5.RELEASE.jar? 此jar包為所有Web框架所共有,提供WebApplicationContext及實現等。 **7、Web服務器選擇及測試:** 目前比較流行的支持Servlet規范的開源Web服務器包括Tomcat、Resin、Jetty等,Web服務器有獨立運行和嵌入式運行之分,嵌入式Web服務器可以在測試用例中運行不依賴于外部環境,因此我們使用嵌入式Web服務器。 Jetty是一個非常輕量級的Web服務器,并且提供嵌入式運行支持,在此我們選用Jetty作為測試使用的Web服務器。 **7.1、準備Jetty嵌入式Web服務器運行需要的jar包:** ``` 到http://dist.codehaus.org/jetty/網站下載jetty-6.1.24,在下載的jetty-6.1.24.zip包中拷貝如下jar包到項目的lib/jetty目錄下,并添加到類路徑中:?? ``` ![](https://box.kancloud.cn/2016-05-13_57354721e86c9.JPG) **7.2、在單元測試中啟動Web服務器:** ``` package cn.javass.spring.chapter10; import org.junit.Test; import org.mortbay.jetty.Server; import org.mortbay.jetty.webapp.WebAppContext; public class WebFrameWorkIntegrateTest { @Test public void testWebFrameWork() throws Exception { Server server = new Server(8080); WebAppContext webapp = new WebAppContext(); webapp.setResourceBase("webapp"); //webapp.setDescriptor("webapp/WEB-INF/web.xml"); webapp.setContextPath("/"); webapp.setClassLoader(Thread.currentThread().getContextClassLoader()); server.setHandler(webapp); server.start(); server.join(); //server.stop(); } } ``` * **創建內嵌式Web服務器:**使用new Server(8080)新建一個Jetty服務器,監聽端口為8080; * **創建一個Web應用:**使用new WebAppContext()新建一個Web應用對象,一個Web應用可以認為就是一個WebAppContext對象; * **指定Web應用的目錄:**使用webapp.setResourceBase("webapp")指定Web應用位于項目根目錄下的“webapp”目錄下; * **指定部署描述符:**使用webapp.setDescriptor("webapp/WEB-INF/web.xml");此處指定部署描述符為項目根目錄下的“webapp/WEB-INF/web.xml”,該步驟是可選的,如果web.xml位于Web應用的WEB-INF下。 * **指定Web應用請求上下文:**使用webapp.setContextPath("/")指定請求上下文為“/”,從而訪問該Web應用可以使用如“http://localhost:8080/hello.do”形式訪問; * 指定類裝載器:因為Jetty自帶的ClassLoader在內嵌環境中對中文路徑處理有問題,因此我們使用Eclispe的ClassLoader,即通過“webapp.setClassLoader(Thread.currentThread().getContextClassLoader()) ”指定; * **啟動Web服務器:**使用“server.start()”啟動并使用“server.join()”保證Web服務器一直運行; * **關閉Web服務器:**可以通過某種方式執行“server.stop()”來關閉Web服務器;另一種方式是通過【Console】控制臺面板的【Terminate】終止按鈕關閉,如圖10-3所示: ![](https://box.kancloud.cn/2016-05-13_5735472206ad5.JPG)? 圖10-3 點擊紅色按鈕關閉Web服務器 原創內容,轉載請注明出處【[http://sishuok.com/forum/blogPost/list/0/2510.html](http://sishuok.com/forum/blogPost/list/0/2510.html#7233)】
                  <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>

                              哎呀哎呀视频在线观看