先進行通用配置, 【第十章】集成其它Web框架 之 10.1 概述
10.3 集成Struts2.x
10.3.1 概述
Struts2前身是WebWork,核心並沒有改變,其實就是把WebWork改名爲struts2,與Struts1一點關係沒有。
Struts2中通過ObjectFactory接口實現創建及獲取Action實例,類似於Spring的IoC容器,所以Action實例可以由ObjectFactory實現來管理,因此集成Spring的關鍵點就是如何創建ObjectFactory實現來從Spring容器中獲取相應的Action Bean。
Struts2提供一個默認的ObjectFactory接口實現StrutsSpringObjectFactory,該類用於根據Struts2配置文件中相應Bean信息從Spring 容器中獲取相應的Action。
因此Struts2.x與Spring集成需要使用StrutsSpringObjectFactory類作爲中介者。
接下來讓我們首先讓我們準備Struts2x所需要的jar包
準備Struts2.x需要的jar包,到Struts官網http://struts.apache.org/下載struts-2.2.1.1版本,拷貝如下jar包到項目的lib目錄下並添加到類路徑:
lib\struts2-core-2.2.1.1.jar //核心struts2包 lib\xwork-core-2.2.1.1.jar //命令框架包,獨立於Web環境,爲Struts2 //提供核心功能的支持包 lib\freemarker-2.3.16.jar //提供模板化UI標籤及視圖技術支持 lib\ognl-3.0.jar //對象圖導航工具包,類似於SpEL lib\ struts2-spring-plugin-2.2.1.1.jar //集成Spring的插件包 lib\commons-logging-1.0.4.jar //日誌記錄組件包(已有) lib\commons-fileupload-1.2.1.jar //用於支持文件上傳的包 |
10.3.2 使用ObjectFactory集成
1、Struts2.x的Action實現:
- package cn.javass.spring.chapter10.struts2x.action;
- import org.apache.struts2.ServletActionContext;
- import com.opensymphony.xwork2.ActionSupport;
- public class HelloWorldAction extends ActionSupport {
- private String message;
- @Override
- public String execute() throws Exception {
- ServletActionContext.getRequest().setAttribute("message", message);
- return "hello";
- }
- public void setMessage(String message) {//setter注入
- this.message = message;
- }
- }
2、JSP頁面定義,使用Struts1x中定義的JSP頁面“webapp/WEB-INF/jsp/hello.jsp”;
3、Spring一般配置文件定義(resources/chapter10/applicationContext-message.xml):
在此配置文件中定義我們使用的“message”Bean;
- <bean id="message" class="java.lang.String">
- <constructor-arg index="0" value="Hello Spring"/>
- </bean>
4、Spring Action 配置文件定義(resources/chapter10/hello-servlet.xml):
- <bean name="helloAction" class="cn.javass.spring.chapter10.struts2x.action.HelloWorldAction" scope="prototype">
- <property name="message" ref="message"/>
- </bean>
Struts2的Action在Spring中配置,而且應該是prototype,因爲Struts2的Action是有狀態的,定義在Spring中,那Struts如何找到該Action呢?
5、struts2配置文件定義(resources/chapter10/struts2x/struts.xml):
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE struts PUBLIC
- "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
- "http://struts.apache.org/dtds/struts-2.0.dtd">
- <struts>
- <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>
- <constant name="struts.devMode" value="true"/>
- <package name="default" extends="struts-default">
- <action name="hello" class="helloAction">
- <result name="hello" >/WEB-INF/jsp/hello.jsp</result>
- </action>
- </package>
- </struts>
- struts.objectFactory:通過在Struts配置文件中使用常量屬性struts.objectFactory來定義Struts將要使用的ObjectFactory實現,此處因爲需要從Spring容器中獲取Action對象,因此需要使用StrutsSpringObjectFactory來集成Spring;
- <action name="hello" class="helloAction">:StrutsSpringObjectFactory對象工廠將根據<action>標籤的class屬性去Spring容器中查找同名的Action Bean;即本例中將到Spring容器中查找名爲helloAction的Bean。
6、web.xml部署描述符文件定義(webapp/WEB-INF/web.xml):
6.1、由於Struts2只能使用通用配置,因此需要在通用配置中加入Spring Action配置文件(chapter10/struts2x/struts2x-servlet.xml):
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>
- classpath:chapter10/applicationContext-message.xml,
- classpath:chapter10/struts2x/struts2x-servlet.xml
- </param-value>
- </context-param>
Struts2只能在通用配置中指定所有Spring配置文件,並沒有如Struts1自己指定Spring配置文件的實現。
6.2、Strut2前端控制器定義,在web.xml中添加如下配置:
- <!-- Struts2.x前端控制器配置開始 -->
- <filter>
- <filter-name>struts2x</filter-name>
- <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>
- struts-default.xml,struts-plugin.xml,chapter10/struts2x/struts.xml
- </param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>struts2x</filter-name>
- <url-pattern>*.action</url-pattern>
- </filter-mapping>
- <!-- Struts2.x前端控制器配置結束 -->
- FilterDispatcher:Struts2前端控制器爲FilterDispatcher,是Filter實現,不是Servlet;
- config:通過初始化參數config指定配置文件爲struts-default.xml,struts-plugin.xml,chapter10/struts2x/struts.xml;如果不知道該參數則默認加載struts-default.xml,struts-plugin.xml,struts.xml(位於webapp/WEB-INF/classes下);顯示指定時需要將struts-default.xml,struts-plugin.xml也添加上。
- *.action:將攔截以“.action”結尾的HTTP請求;
- struts2x:FilterDispatcher前端控制器的名字爲struts2x,因此相應的Spring配置文件名爲struts2x-servlet.xml。
7、執行測試,在Web瀏覽器中輸入http://localhost:8080/hello.action可以看到“Hello Spring”信息說明Struts2集成成功。
集成Strut2也是非常簡單,在此我們總結一下吧:
- 配置文件位置:
Struts配置文件默認加載“struts-default.xml,struts-plugin.xml, struts.xml”,其中struts-default.xml和struts-plugin.xml是Struts自帶的,而struts.xml是我們指定的,默認位於webapp/WEB-INF/classes下;
如果需要將配置文件放到其他位置,需要在web.xml的<filter>標籤下,使用初始化參數config指定,如“struts-default.xml,struts-plugin.xml,chapter10/struts2x/struts.xml”,其中“struts-default.xml和struts-plugin.xml”是不可省略的,默認相對路徑是類路徑。
- 集成關鍵ObjectFactory:在Struts配置文件或屬性文件中使用如下配置知道使用StrutsSpringObjectFactory來獲取Action實例:
在struts.xml中指定:
- <constant name="struts.objectFactory" value="org.apache.struts2.spring.StrutsSpringObjectFactory"/>
或在struts.properties文件(webapp/WEB-INF/classes/)中:
- struts.objectFactory=org.apache.struts2.spring.StrutsSpringObjectFactory
- 集成關鍵Action定義:
StrutsSpringObjectFactory將根據Struts2配置文件中的<action class=””>標籤的classes屬性名字去到Spring配置文件中查找同名的Bean定義,這也是集成的關鍵。
- Spring配置文件中Action定義:由於Struts2的Action是有狀態的,因此應該將Bean定義爲prototype。
如圖10-5,Sturt2與Spring集成的關鍵就是StrutsSpringObjectFactory,注意圖只是說明Struts與Spring如何通過中介者StrutsSpringObjectFactory來實現集成,不能代表實際的類交互。
圖10-5 Strut2與Spring集成
原文地址:http://sishuok.com/forum/blogPost/list/2512.html