一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - Spring整合Struts2的兩種方法小結

Spring整合Struts2的兩種方法小結

2020-12-06 15:04Java教程網 Java教程

下面小編就為大家帶來一篇Spring整合Struts2的兩種方法小結。小編覺得挺不錯的,現在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

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的兩種方法小結就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持服務器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产90后美女露脸在线观看 | 午夜a一级毛片 | 成年人免费观看视频网站 | 男女男精品网站 | 久久久久久免费高清电影 | 视频国产91| 国产日韩欧美在线一二三四 | 九九九精品视频 | 国产麻豆在线观看网站 | 久久99热狠狠色一区二区 | 久久99re2在线视频精品 | 满城尽带黄金甲大胸片 | 欧美大片一区二区三区 | 欧美又大又粗又爽视频 | 国产一区二区三区四卡 | 国产一卡二卡四卡免费 | 天堂漫画破解版 | 国产99久久九九精品免费 | 色五夜婷婷 | ass巨大胖女人sias | 国产高清不卡视频在线播放 | 国产精品视频免费一区二区三区 | 九九热在线观看视频 | 国内精品在线观看视频 | 2020年最新国产精品视频免费 | 特级www| 538免费精品视频搬运工 | 亚洲美女人黄网成人女 | 吻戏辣妞范1000免费体验 | 热久久天天拍天天拍热久久2018 | 女张腿男人桶羞羞漫画 | 国产成人福利美女观看视频 | 日本欧美大码a在线视频播放 | 国产成人咱精品视频免费网站 | 免费看男人使劲躁女人小说 | 99热.com| 天天gan| freesex1718处xx| 亚洲天堂男人天堂 | 国产日产欧产精品精品软件 | 视频一区 日韩 |