# 【第十章】集成其它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 里面通過 <servlet-name>hello</servlet-name>指定名字為“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配置文件中的<action>的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配置文件中<action>標簽的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配置文件中<action>標簽的path屬性和Spring配置文件的name屬性應該完全一樣,否則錯誤;
* Struts通過**DelegatingRequestProcessor**去到Spring Web容器中查找同名的Action Bean;
很簡單吧,只是由**DelegatingRequestProcessor**去幫我們查找相應的Action Bean,但沒有代理Action了,所以推薦使用該方式。

圖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)】
- 跟我學 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