国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - Spring整合Struts2的兩種方法小結(jié)

Spring整合Struts2的兩種方法小結(jié)

2020-12-06 15:04Java教程網(wǎng) Java教程

下面小編就為大家?guī)?lái)一篇Spring整合Struts2的兩種方法小結(jié)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

spring提供了一個(gè)ContextLoaderListener,該監(jiān)聽(tīng)類實(shí)現(xiàn)了ServletContextListener接口。該類可以作為L(zhǎng)istener使用,它會(huì)在創(chuàng)建時(shí)自動(dòng)查找WEB-INF/下的applicationContext.xml文件,因此如果只有一個(gè)配置文件且配置文件命名為applicationContext.xml,則只需在web.xml文件中增加如下配置片段:

 
?
1
 
2
3
4
5
<!-- 使用ContextLoaderListener初始化Spring容器 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

如果有多個(gè)配置文件需要載入,則考慮使用<context-param.../>元素確定配置文件的文件名。,COntextLoaderListener加載時(shí),會(huì)查找名為contextConfigLocation的初始化參數(shù),因此配置<context-param.../>時(shí)應(yīng)指定參數(shù)名為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根據(jù)配置文件創(chuàng)建WebApplicationContext對(duì)象,并將其保存在Web應(yīng)用的ServletContext中。如果要獲取應(yīng)用中的ApplicationContext實(shí)例,則可以根據(jù)

如下獲取:

 
?
1
 
WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext)

讓Spring管理控制器

當(dāng)Struts2將請(qǐng)求轉(zhuǎn)發(fā)給指定的Action時(shí),Struts2中的該Action只是一個(gè)傀儡,他只是一個(gè)代號(hào),并沒(méi)有指定實(shí)際的實(shí)現(xiàn)類,當(dāng)然也不可能創(chuàng)建Action實(shí)例,二隱藏在該action下的是Spring容器中的Action實(shí)例,他才是真正處理用戶請(qǐng)求的控制器。

其中Struts2只是一個(gè)偽控制器,這個(gè)偽控制器的功能實(shí)際由Spring容器中的控制器來(lái)完成,這就實(shí)現(xiàn)了讓核心控制器調(diào)用Spring容器中的action來(lái)處理用戶請(qǐng)求。在這種策略下,處理用戶請(qǐng)求的Action由Spring插件負(fù)責(zé)創(chuàng)建,但Spring插件創(chuàng)建Action實(shí)例時(shí)。并不是利用配置Action時(shí)指定的class屬性來(lái)創(chuàng)建該action實(shí)例,而是從Spring容器中取出對(duì)應(yīng)的Bean實(shí)例完成創(chuàng)建。

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用來(lái)初始化Struts 2并且處理所有的WEB請(qǐng)求。 -->
 <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語(yǔ)義約束 -->
<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">
 <!-- 定義一個(gè)業(yè)務(wù)邏輯組件,實(shí)現(xiàn)類為MyServiceImp -->
 <bean id="myService"
  class="com.bh.service.impl.MyServiceImpl"/>
 <!-- 讓Spring管理的Action實(shí)例,因?yàn)槊總€(gè)action里包含請(qǐng)求的狀態(tài)信息,所以必須配置scope不能為單例 -->
 <bean id="loginAction" class="com.bh.action.LoginAction"
  scope="prototype">
  <!-- 依賴注入業(yè)務(wù)邏輯組件 -->
  <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">
  <!-- 定義處理用戶請(qǐng)求的Action,該Action的class屬性不是實(shí)際處理類
   , 而是Spring容器中的Bean實(shí)例-->
  <action name="loginPro" class="loginAction">
   <!-- 為兩個(gè)邏輯視圖配置視圖頁(yè)面 -->
   <result name="error">/WEB-INF/content/error.jsp</result>
   <result name="success">/WEB-INF/content/welcome.jsp</result>
  </action>
  <!-- 讓用戶直接訪問(wèn)該應(yīng)用時(shí)列出所有視圖頁(yè)面 -->
  <action name="*">
   <result>/WEB-INF/content/{1}.jsp</result>
  </action>
 </package>
</struts>

使用自動(dòng)裝配

通過(guò)設(shè)置struts.objectFactory.spring.autoWire常量可以改變Spring插件額自動(dòng)裝配策略,該常量可以接受如下幾個(gè)值:

Name:根據(jù)屬性名自動(dòng)裝配。Spring插件會(huì)查找容器中全部Bean,找到其中id屬性與Action所需的業(yè)務(wù)邏輯組件同名的Bean,將該bean實(shí)例注入到Action實(shí)例。

Type:根據(jù)屬性類型自動(dòng)裝配。Spring插件會(huì)查找容器中全部Bean,找出其類型恰好與Action所需的業(yè)務(wù)邏輯組件相同的Bean,將該Bean實(shí)例注入到Action實(shí)例。

Auto:Spring插件會(huì)自動(dòng)檢測(cè)需要使用哪種自動(dòng)裝配方式。

Constructor:與type類似,區(qū)別是constructor使用構(gòu)造器來(lái)構(gòu)造注入的所需參數(shù)而不是使用設(shè)值注入方式。

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語(yǔ)義約束 -->
<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">
 <!-- 定義一個(gè)業(yè)務(wù)邏輯組件,實(shí)現(xiàn)類為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">
  <!-- 定義處理用戶請(qǐng)求的Action -->
  <action name="loginPro"
   class="com.bh.action.LoginAction">
   <!-- 為兩個(gè)邏輯視圖配置視圖頁(yè)面 -->
   <result name="error">/WEB-INF/content/error.jsp</result>
   <result name="success">/WEB-INF/content/welcome.jsp</result>
  </action>
  <!-- 讓用戶直接訪問(wèn)該應(yīng)用時(shí)列出所有視圖頁(yè)面 -->
  <action name="*">
   <result>/WEB-INF/content/{1}.jsp</result>
  </action>
 </package>
</struts>

以上這篇Spring整合Struts2的兩種方法小結(jié)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持服務(wù)器之家。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 天天操天天添 | 精品视频 | 久久久久久麻豆 | 欧美视频第一区 | 91视频观看 | 视频在线一区二区 | 亚洲精品一区二区三区蜜桃久 | 成人欧美一区二区三区在线播放 | 国产视频一区在线 | 国产一区二区三区视频在线观看 | 欧美成人综合在线 | 亚洲精品高潮呻吟久久av | 欧美精品入口蜜桃 | 三级黄色片在线免费观看 | 在线观看国产视频 | 免费看国产 | 99国产精品99久久久久久 | 中文字幕一区二区三区在线视频 | 中文字幕一区二区三区在线视频 | 成人av一级 | www中文字幕 | 欧美精品一区二区三区四区在线 | 久久久精品亚洲 | 亚洲一区二区精品 | 久久久久久久久99精品 | 日韩中文字幕一区 | 国产成人三区 | 小泽玛丽娅 | 亚洲激情在线 | 国产一区二区三区久久 | 国产日韩视频在线观看 | 寡妇激情毛片免费视频 | 国产成人福利在线 | 亚洲日本va中文字幕 | 99国产精品久久久久久久久久 | 免费av片网站 | 一级片欧美| 日韩精品一区二区在线观看 | 婷婷亚洲五月 | 九色在线观看 | 中文字幕日韩视频 |