# 【第十章】集成其它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目錄結構:**

圖10-1 web目錄結構
**1.2、右擊spring項目選擇【Propeties】,然后選擇【Java Build Path】中的【Source】選項卡,將類輸出路徑修改為“spring/webapp/WEB-INF/classes”,如圖10-2所示:**
?
圖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>
```
<web-app version="2.4">表示采用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目錄下,并添加到類路徑中:??
```

**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所示:
?
圖10-3 點擊紅色按鈕關閉Web服務器
原創內容,轉載請注明出處【[http://sishuok.com/forum/blogPost/list/0/2510.html](http://sishuok.com/forum/blogPost/list/0/2510.html#7233)】
- 跟我學 Spring3
- 【第二章】 IoC 之 2.1 IoC基礎 ——跟我學Spring3
- 【第二章】 IoC 之 2.2 IoC 容器基本原理 ——跟我學Spring3
- 【第二章】 IoC 之 2.3 IoC的配置使用——跟我學Spring3
- 【第三章】 DI 之 3.1 DI的配置使用 ——跟我學spring3
- 【第三章】 DI 之 3.2 循環依賴 ——跟我學spring3
- 【第三章】 DI 之 3.3 更多DI的知識 ——跟我學spring3
- 【第三章】 DI 之 3.4 Bean的作用域 ——跟我學spring3
- 【第四章】 資源 之 4.1 基礎知識 ——跟我學spring3
- 【第四章】 資源 之 4.2 內置Resource實現 ——跟我學spring3
- 【第四章】 資源 之 4.3 訪問Resource ——跟我學spring3
- 【第四章】 資源 之 4.4 Resource通配符路徑 ——跟我學spring3
- 【第五章】Spring表達式語言 之 5.1 概述 5.2 SpEL基礎 ——跟我學spring3
- 【第五章】Spring表達式語言 之 5.3 SpEL語法 ——跟我學spring3
- 【第五章】Spring表達式語言 之 5.4在Bean定義中使用EL—跟我學spring3
- 【第六章】 AOP 之 6.1 AOP基礎 ——跟我學spring3
- 【第六章】 AOP 之 6.2 AOP的HelloWorld ——跟我學spring3
- 【第六章】 AOP 之 6.3 基于Schema的AOP ——跟我學spring3
- 【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我學spring3
- 【第六章】 AOP 之 6.5 AspectJ切入點語法詳解 ——跟我學spring3
- 【第六章】 AOP 之 6.6 通知參數 ——跟我學spring3
- 【第六章】 AOP 之 6.7 通知順序 ——跟我學spring3
- 【第六章】 AOP 之 6.8 切面實例化模型 ——跟我學spring3
- 【第六章】 AOP 之 6.9 代理機制 ——跟我學spring3
- 【第七章】 對JDBC的支持 之 7.1 概述 ——跟我學spring3
- 【第七章】 對JDBC的支持 之 7.2 JDBC模板類 ——跟我學spring3
- 【第七章】 對JDBC的支持 之 7.3 關系數據庫操作對象化 ——跟我學spring3
- 【第七章】 對JDBC的支持 之 7.4 Spring提供的其它幫助 ——跟我學spring3【私塾在線原創】
- 【第七章】 對JDBC的支持 之 7.5 集成Spring JDBC及最佳實踐 ——跟我學spring3
- 【第八章】 對ORM的支持 之 8.1 概述 ——跟我學spring3
- 【第八章】 對ORM的支持 之 8.2 集成Hibernate3 ——跟我學spring3
- 【第八章】 對ORM的支持 之 8.3 集成iBATIS ——跟我學spring3
- 【第八章】 對ORM的支持 之 8.4 集成JPA ——跟我學spring3
- 【第九章】 Spring的事務 之 9.1 數據庫事務概述 ——跟我學spring3
- 【第九章】 Spring的事務 之 9.2 事務管理器 ——跟我學spring3
- 【第九章】 Spring的事務 之 9.3 編程式事務 ——跟我學spring3
- 【第九章】 Spring的事務 之 9.4 聲明式事務 ——跟我學spring3
- 【第十章】集成其它Web框架 之 10.1 概述 ——跟我學spring3
- 【第十章】集成其它Web框架 之 10.2 集成Struts1.x ——跟我學spring3
- 【第十章】集成其它Web框架 之 10.3 集成Struts2.x ——跟我學spring3
- 【第十章】集成其它Web框架 之 10.4 集成JSF ——跟我學spring3
- 【第十一章】 SSH集成開發積分商城 之 11.1 概述 ——跟我學spring3
- 【第十一章】 SSH集成開發積分商城 之 11.2 實現通用層 ——跟我學spring3
- 【第十一章】 SSH集成開發積分商城 之 11.3 實現積分商城層 ——跟我學spring3
- 【第十二章】零配置 之 12.1 概述 ——跟我學spring3
- 【第十二章】零配置 之 12.2 注解實現Bean依賴注入 ——跟我學spring3
- 【第十二章】零配置 之 12.3 注解實現Bean定義 ——跟我學spring3
- 【第十二章】零配置 之 12.4 基于Java類定義Bean配置元數據 ——跟我學spring3
- 【第十二章】零配置 之 12.5 綜合示例-積分商城 ——跟我學spring3
- 【第十三章】 測試 之 13.1 概述 13.2 單元測試 ——跟我學spring3
- 【第十三章】 測試 之 13.3 集成測試 ——跟我學spring3
- 跟我學 Spring MVC
- SpringMVC + spring3.1.1 + hibernate4.1.0 集成及常見問題總結
- Spring Web MVC中的頁面緩存支持 ——跟我學SpringMVC系列
- Spring3 Web MVC下的數據類型轉換(第一篇)——《跟我學Spring3 Web MVC》搶先看
- Spring3 Web MVC下的數據格式化(第二篇)——《跟我學Spring3 Web MVC》搶先看
- 第一章 Web MVC簡介 —— 跟開濤學SpringMVC
- 第二章 Spring MVC入門 —— 跟開濤學SpringMVC
- 第三章 DispatcherServlet詳解 ——跟開濤學SpringMVC
- 第四章 Controller接口控制器詳解(1)——跟著開濤學SpringMVC
- 第四章 Controller接口控制器詳解(2)——跟著開濤學SpringMVC
- 第四章 Controller接口控制器詳解(3)——跟著開濤學SpringMVC
- 第四章 Controller接口控制器詳解 (4)——跟著開濤學SpringMVC
- 第四章 Controller接口控制器詳解(5)——跟著開濤學SpringMVC
- 跟著開濤學SpringMVC 第一章源代碼下載
- 第二章 Spring MVC入門 源代碼下載
- 第四章 Controller接口控制器詳解 源代碼下載
- 第四章 Controller接口控制器詳解(6)——跟著開濤學SpringMVC
- 第四章 Controller接口控制器詳解(7 完)——跟著開濤學SpringMVC
- 第五章 處理器攔截器詳解——跟著開濤學SpringMVC
- 源代碼下載 第五章 處理器攔截器詳解——跟著開濤學SpringMVC
- 注解式控制器運行流程及處理器定義 第六章 注解式控制器詳解——跟著開濤學SpringMVC
- 源代碼下載 第六章 注解式控制器詳解
- SpringMVC3強大的請求映射規則詳解 第六章 注解式控制器詳解——跟著開濤學SpringMVC
- Spring MVC 3.1新特性 生產者、消費者請求限定 —— 第六章 注解式控制器詳解——跟著開濤學SpringMVC
- SpringMVC強大的數據綁定(1)——第六章 注解式控制器詳解——跟著開濤學SpringMVC
- SpringMVC強大的數據綁定(2)——第六章 注解式控制器詳解——跟著開濤學SpringMVC
- SpringMVC數據類型轉換——第七章 注解式控制器的數據驗證、類型轉換及格式化——跟著開濤學SpringMVC
- SpringMVC數據格式化——第七章 注解式控制器的數據驗證、類型轉換及格式化——跟著開濤學SpringMVC
- SpringMVC數據驗證——第七章 注解式控制器的數據驗證、類型轉換及格式化——跟著開濤學SpringMVC