關於這兩大框架的整合的好處最主要的就是在整合後,可以使用Spring的配置文件applicationContext.xml來描述以來關係,在Struts2的配置文件struts.xml來使用Spring創建bean,從而免去每次自己實例化對象。
下面就詳細介紹下具體過程實現:
- 準備開發環境(導入依賴包)
分別導入Spring和Struts必須包,在此不一一列舉了,因爲版本的不同包的數量也是有差異的。但是尤爲注意的就是要導入commons-logging和struts2-spring-plugin包,否則啓動時就會拋異常。
在src下建立struts.xml、applicationContext.xml文件(建議拷貝),如果寫的項目挺大建議新建一個名字爲config的Source Folder,把配置文件放在裏面。
- 配置web.xml
既然用到Struts2的框架,那麼配置核心攔截器也是不可或缺的。代碼如下:
- 配置web.xml
<!-- 配置Struts2 的主過濾器 -->
<filter>
<filter-name>Struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>Struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
爲了是Spring容器啓動時自動加載applicationContext配置,所以此時還要在web.xml中配置ContextLoaderListener監聽器,主要原因就是他實現了ServletContextListener這個接口。具體代碼如下:
<!-- 配置Sping的用於初始化ApplicationContext的監聽器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
說明:默認情況下,會加載WEB-INF/applicationContext.xml這個文件,我們可以通過配置contextConfigLocation參數改變配置文件的路徑,這裏我選用更爲方便的classpath屬性作爲工程文件的WEB-INF路徑。
3.開發環境搭建好,現在就寫測試文件包括配置文件(一下代碼注意文件所在的包位置)
首先寫一個Action:TestAction 代碼如下:
package com.sun.action;
import javax.annotation.Resource;
import javax.servlet.ServletContext;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.service.UserService;
//添加註解使其到Spring容器中。
@Controller("testAction")
//設置多例
@Scope("prototype")
public class TestAction extends ActionSupport {
@Resource
private UserService userService;
@Override
public String execute() throws Exception {
userService.saveUser(new Object());// 模擬一個操作
System.out.println("===--===--->>TestAction.execute()");
return SUCCESS;
}
}
4.接着根據TestAction編寫strurs2,applicationContext.xml配置文件 代碼如下:
<?xml version="1.0"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
"http://struts.apache.org/dtds/struts-2.1.7.dtd">
<struts>
<!-- 通過常量方式改變struts默認的屬性配置 -->
<constant name="struts.action.extension" value="do"/>
<!-- 設置開發模式,重新加載國際化資源文件和配置文件 -->
<constant name="struts.devMode" value="true"/>
<package name="default" namespace="/" extends="struts-default">
<!-- 配置action,在沒有與Spring整合時,class屬性寫的是類的全限定名 (包括包名)
當與Spring整合後,class屬性寫的是bean的名稱。
注意:這個bean一定要是多例的模式(prototype)。
class:要設置根據Action類名首字母改爲小寫
-->
<action name="test" class="testAction">
<result name="success">/test.jsp</result>
</action>
</package>
</struts>
<?xml version="1.0" encoding="UTF-8"?>
<!--
- Middle tier application context definition for the image database.
-->
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 自動掃描與裝配bean -->
<context:component-scan base-package="com.sun"></context:component-scan>
</beans>
5.編寫測試類 代碼如下:
package com.sun.test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.sun.listener.MyEvent;
import com.sun.service.UserService;
public class UserServiceTest {
@Test
public void testSaveUser() {
ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = (UserService) ac.getBean("userService");
System.out.println(userService);
}
}
具體jsp文件在此不贅述了,那麼量大框架整合完畢。