spring提供了一個ContextLoaderListener,該監聽類實現了ServletContextListener接口。該類可以作為Listener使用,它會在創建時自動查找WEB-INF/下的applicationContext.xml文件,因此如果只有一個配置文件且配置文件命名為applicationContext.xml,則只需在web.xml文件中增加如下配置片段:
1
|
2
3
4
5
|
<!-- 使用ContextLoaderListener初始化Spring容器 --> < listener > < listener-class >org.springframework.web.context.ContextLoaderListener </ listener-class > </ listener > |
如果有多個配置文件需要載入,則考慮使用<context-param.../>元素確定配置文件的文件名。,COntextLoaderListener加載時,會查找名為contextConfigLocation的初始化參數,因此配置<context-param.../>時應指定參數名為contextConfigLocation。
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<? xml version = "1.0" encoding = "GBK" ?> < web-app xmlns = " http://java.sun.com/xml/ns/javaee " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd " version = "3.0" > < context-param > < param-name > contectCOnfigLocation </ param-name > < param-value >/WEB-INF/daocontext.xml,/WEB-INF/applicationCotext.xml </ param-value > </ context-param > <!-- 使用ContextLoaderListener初始化Spring容器 --> < listener > < listener-class >org.springframework.web.context.ContextLoaderListener </ listener-class > </ listener > </ web-app > |
Spring根據配置文件創建WebApplicationContext對象,并將其保存在Web應用的ServletContext中。如果要獲取應用中的ApplicationContext實例,則可以根據
如下獲?。?/strong>
1
|
|
WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext) |
讓Spring管理控制器
當Struts2將請求轉發給指定的Action時,Struts2中的該Action只是一個傀儡,他只是一個代號,并沒有指定實際的實現類,當然也不可能創建Action實例,二隱藏在該action下的是Spring容器中的Action實例,他才是真正處理用戶請求的控制器。
其中Struts2只是一個偽控制器,這個偽控制器的功能實際由Spring容器中的控制器來完成,這就實現了讓核心控制器調用Spring容器中的action來處理用戶請求。在這種策略下,處理用戶請求的Action由Spring插件負責創建,但Spring插件創建Action實例時。并不是利用配置Action時指定的class屬性來創建該action實例,而是從Spring容器中取出對應的Bean實例完成創建。
web.xml
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<? xml version = "1.0" encoding = "GBK" ?> < web-app xmlns = " http://java.sun.com/xml/ns/javaee " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd " version = "3.0" > <!-- 使用ContextLoaderListener初始化Spring容器 --> < listener > < listener-class >org.springframework.web.context.ContextLoaderListener </ listener-class > </ listener > <!-- 定義Struts 2的FilterDispathcer的Filter --> < filter > < filter-name >struts2</ filter-name > < filter-class >org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</ filter-class > </ filter > <!-- FilterDispatcher用來初始化Struts 2并且處理所有的WEB請求。 --> < filter-mapping > < filter-name >struts2</ filter-name > < url-pattern >/*</ url-pattern > </ filter-mapping > </ web-app > |
applicationcontext.xml
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<? xml version = "1.0" encoding = "GBK" ?> <!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd語義約束 --> < beans xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xmlns = " http://www.springframework.org/schema/beans " xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <!-- 定義一個業務邏輯組件,實現類為MyServiceImp --> < bean id = "myService" class = "com.bh.service.impl.MyServiceImpl" /> <!-- 讓Spring管理的Action實例,因為每個action里包含請求的狀態信息,所以必須配置scope不能為單例 --> < bean id = "loginAction" class = "com.bh.action.LoginAction" scope = "prototype" > <!-- 依賴注入業務邏輯組件 --> < property name = "ms" ref = "myService" /> </ bean > </ beans > |
struts.xml
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<? xml version = "1.0" encoding = "GBK" ?> <!-- 指定Struts 2配置文件的DTD信息 --> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" " http://struts.apache.org/dtds/struts-2.1.7.dtd "> <!-- Struts 2配置文件的根元素 --> < struts > <!-- 配置了系列常量 --> < constant name = "struts.i18n.encoding" value = "GBK" /> < constant name = "struts.devMode" value = "true" /> < package name = "lee" extends = "struts-default" > <!-- 定義處理用戶請求的Action,該Action的class屬性不是實際處理類 , 而是Spring容器中的Bean實例--> < action name = "loginPro" class = "loginAction" > <!-- 為兩個邏輯視圖配置視圖頁面 --> < result name = "error" >/WEB-INF/content/error.jsp</ result > < result name = "success" >/WEB-INF/content/welcome.jsp</ result > </ action > <!-- 讓用戶直接訪問該應用時列出所有視圖頁面 --> < action name = "*" > < result >/WEB-INF/content/{1}.jsp</ result > </ action > </ package > </ struts > |
使用自動裝配
通過設置struts.objectFactory.spring.autoWire常量可以改變Spring插件額自動裝配策略,該常量可以接受如下幾個值:
Name:根據屬性名自動裝配。Spring插件會查找容器中全部Bean,找到其中id屬性與Action所需的業務邏輯組件同名的Bean,將該bean實例注入到Action實例。
Type:根據屬性類型自動裝配。Spring插件會查找容器中全部Bean,找出其類型恰好與Action所需的業務邏輯組件相同的Bean,將該Bean實例注入到Action實例。
Auto:Spring插件會自動檢測需要使用哪種自動裝配方式。
Constructor:與type類似,區別是constructor使用構造器來構造注入的所需參數而不是使用設值注入方式。
web.xml
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<? xml version = "1.0" encoding = "GBK" ?> < web-app xmlns = " http://java.sun.com/xml/ns/javaee " xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd " version = "3.0" > < listener > < listener-class >org.springframework.web.context.ContextLoaderListener </ listener-class > </ listener > < 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 > </ web-app > |
applicationcontext.xml
1
|
2
3
4
5
6
7
8
9
|
<? xml version = "1.0" encoding = "GBK" ?> <!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd語義約束 --> < beans xmlns:xsi = " http://www.w3.org/2001/XMLSchema-instance " xmlns = " http://www.springframework.org/schema/beans " xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd "> <!-- 定義一個業務邏輯組件,實現類為MyServiceImp --> < bean id = "ms" class = "com.bh.service.impl.MyServiceImpl" /> </ beans > |
struts.xml
1
|
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
<? xml version = "1.0" encoding = "GBK" ?> <!-- 指定Struts 2配置文件的DTD信息 --> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN" " http://struts.apache.org/dtds/struts-2.1.7.dtd "> <!-- Struts 2配置文件的根元素 --> < struts > <!-- 配置了系列常量 --> < constant name = "struts.i18n.encoding" value = "GBK" /> < constant name = "struts.devMode" value = "true" /> < package name = "lee" extends = "struts-default" > <!-- 定義處理用戶請求的Action --> < action name = "loginPro" class = "com.bh.action.LoginAction" > <!-- 為兩個邏輯視圖配置視圖頁面 --> < result name = "error" >/WEB-INF/content/error.jsp</ result > < result name = "success" >/WEB-INF/content/welcome.jsp</ result > </ action > <!-- 讓用戶直接訪問該應用時列出所有視圖頁面 --> < action name = "*" > < result >/WEB-INF/content/{1}.jsp</ result > </ action > </ package > </ struts > |
以上這篇Spring整合Struts2的兩種方法小結就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。