Struts2學習筆記——Struts2與Spring整合


Struts2與Spring整合后,可以使用Spring的配置文件applicationContext.xml來描述依賴關系,在Struts2的配置文件struts.xml來使用Spring創建的bean。

 

1、導入依賴包

除了導入Struts2和Spring的核心庫之外,還要導入commons-logging和struts2-spring-plugin包,否則啟動會出異常

 

2、web.xml的配置

既然有Struts2,核心攔截器的配置是不可少的

<filter>
     <filter-name>struts2</filter-name>
     <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
</filter>
<filter-mapping>
     <filter-name>struts2</filter-name>
     <url-pattern>/*</url-pattern>
</filter-mapping>

 

通過配置ContextLoaderListener監聽器,使容器啟動時,自動加載applicationContext配置,

因為它實現了ServletContextListener這個接口,容器啟動時會自動執行它實現的方法。

<listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

 

默認情況下,會加載WEB-INF/applicationContext.xml這個文件,我們可以通過配置contextConfigLocation參數改變配置文件的路徑

 

 

<context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>WEB-INF/classes/applicationContext.xml</param-value>
</context-param>

以上配置均在web.xml文件的<web-app></web-app>區域

 

3、測試類

在瀏覽器請求一個Action方法,在Action方法內向一個對象請求一個List,然后轉到index.jsp頁面,在頁面中輸出Action請求到的List。

通過Spring依賴配置,控制Action請求的對象。

首先要編寫一個接口,Action方法依賴這個接口,通過調用接口中的方法獲取List

public interface IocTestInterface {
     public List getList();
}

下面編寫Action類,這個類繼承ActionSupport類,並覆蓋其中的execute方法,

execute方法執行時,調用實現了上述接口對象的getList方法獲取List

public class IocAction extends ActionSupport {
	private IocTestInterface iti;
	private List list;
	
	public List getList() {
		return list;
	}
	public void setList(List list) {
		this.list = list;
	}
	public IocTestInterface getIti() {
		return iti;
	}
	public void setIti(IocTestInterface iti) {
		this.iti = iti;
	}
	
	public String execute() throws Exception {
		this.setList(iti.getList());
		return super.execute();
	}
}

編寫用來顯示運行結果的jsp文件

遍歷list,並將每個元素作為一行來顯示

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="s" uri="/struts-tags" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
  <body>
    This is my JSP page. <br><br>
    
    <s:iterator value="list" id="current">
    	<li><s:property value="current"/></li>
    </s:iterator>
    
  </body>
</html>

系統的結構就是這樣。下面編寫兩個實現IocTestInterface接口的類,用來提供數據

public class IocTestImpl implements IocTestInterface {
	public List getList() {
		List l = new ArrayList();
		l.add("abc");
		l.add("def");
		l.add("hig");
		return l;
	}
}

 

public class IocTest2Impl implements IocTestInterface {
	public List getList() {
		List l = new ArrayList();
		l.add("123");
		l.add("456");
		l.add("789");
		return l;
	}
}

4、編寫applicationContext.xml配置依賴關系

<beans xmlns ="http://www.springframework.org/schema/beans" 
    xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation ="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    
    <bean name="IocAction" class="sy.struts2.ioc.IocAction">
    	<property name="iti">
    		<bean class="sy.struts2.ioc.IocTestImpl"></bean>
    	</property>
    </bean>
    
</beans>

文件配置了id為IocAction的bean,路徑為剛剛編寫的Action類的路徑,其中iti對象(請求數據的對象)配置為IocTestImpl(這里使用了匿名bean)

 

5、編寫struts.xml配置文件

首先要告知Struts 2運行時使用Spring來創建對象

在<struts></struts>區域加入以下配置

<constant name="struts.objectFactory" value="spring" />

創建package並配置Action

<package name="hs" extends="struts-default">
     <action name="ioc" class="IocAction">
          <result>/index.jsp</result>
     </action>
</package>

 

6、發布並運行

發布后啟動Tomcat,用瀏覽器打開地址http://localhost:8080/StrutsIoc/ioc.action,獲得了下面的頁面

image

修改spring配置文件applicationContext.xml中配置

<bean name="IocAction" class="sy.struts2.ioc.IocAction">
     <property name="iti">
          <bean class="sy.struts2.ioc.IocTest2Impl"></bean>
     </property>
</bean>

只是將注入到IocAction中的IocTestImpl修改為IocTest2Impl,也就是使用了另一個實現了IocTestInterface接口的類

重啟服務器,再次打開剛才的地址

image

這也就是spring的“控制反轉”


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM