欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時(shí)間:2017年07月25日 08:56:47   投稿:jingxian  
下面小編就為大家?guī)硪黄猄pring整合Struts2的兩種方法小結(jié)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧

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

<!-- 使用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。

<?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ù)

如下獲?。?/strong>

WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext)

讓Spring管理控制器

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

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

web.xml

<?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請(qǐng)求。 -->
 <filter-mapping>
  <filter-name>struts2</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

applicationcontext.xml

<?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">
 <!-- 定義一個(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

<?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è)邏輯視圖配置視圖頁面 -->
   <result name="error">/WEB-INF/content/error.jsp</result>
   <result name="success">/WEB-INF/content/welcome.jsp</result>
  </action>
  <!-- 讓用戶直接訪問該應(yīng)用時(shí)列出所有視圖頁面 -->
  <action name="*">
   <result>/WEB-INF/content/{1}.jsp</result>
  </action>
 </package>
</struts>

使用自動(dòng)裝配

通過設(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)造器來構(gòu)造注入的所需參數(shù)而不是使用設(shè)值注入方式。

web.xml

<?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

<?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">
 <!-- 定義一個(gè)業(yè)務(wù)邏輯組件,實(shí)現(xiàn)類為MyServiceImp -->
 <bean id="ms" class="com.bh.service.impl.MyServiceImpl"/>
</beans>

struts.xml

<?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è)邏輯視圖配置視圖頁面 -->
   <result name="error">/WEB-INF/content/error.jsp</result>
   <result name="success">/WEB-INF/content/welcome.jsp</result>
  </action>
  <!-- 讓用戶直接訪問該應(yīng)用時(shí)列出所有視圖頁面 -->
  <action name="*">
   <result>/WEB-INF/content/{1}.jsp</result>
  </action>
 </package>
</struts>

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

相關(guān)文章

  • 詳解Spring Boot 屬性配置和使用

    詳解Spring Boot 屬性配置和使用

    本篇文章主要介紹了詳解Spring Boot 屬性配置和使用,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-06-06
  • Spring操作JdbcTemplate數(shù)據(jù)庫(kù)的方法學(xué)習(xí)

    Spring操作JdbcTemplate數(shù)據(jù)庫(kù)的方法學(xué)習(xí)

    這篇文章主要為大家介紹了Spring操作JdbcTemplate數(shù)據(jù)庫(kù)方法學(xué)習(xí),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • 使用SpringEvent解決WebUploader大文件上傳解耦問題

    使用SpringEvent解決WebUploader大文件上傳解耦問題

    Spring Event是Spring框架內(nèi)建的一種發(fā)布/訂閱模式的實(shí)現(xiàn),它允許應(yīng)用內(nèi)部不同組件之間通過事件進(jìn)行通信,本文以WebUploader大文件上傳組件為例,在大文件處理的場(chǎng)景中使用SpringEvent的事件發(fā)布機(jī)制,靈活的擴(kuò)展對(duì)文件的處理需求,需要的朋友可以參考下
    2024-07-07
  • 詳解java中的四種代碼塊

    詳解java中的四種代碼塊

    這篇文章主要介紹了詳解java中的四種代碼塊,具有一定借鑒價(jià)值,需要的朋友可以參考下。
    2017-12-12
  • SVN 安裝教程之服務(wù)器和客戶端

    SVN 安裝教程之服務(wù)器和客戶端

    這篇文章主要介紹了SVN 安裝教程之服務(wù)器和客戶端的相關(guān)資料,這里對(duì)安裝步驟進(jìn)行了詳細(xì)介紹,需要的朋友可以參考下
    2016-11-11
  • MyBatis-Plus中MetaObjectHandler沒生效完美解決

    MyBatis-Plus中MetaObjectHandler沒生效完美解決

    在進(jìn)行測(cè)試時(shí)發(fā)現(xiàn)配置的MyMetaObjectHandler并沒有生效,本文主要介紹了MyBatis-Plus中MetaObjectHandler沒生效完美解決,具有一定的參考價(jià)值,感興趣的可以了解一下
    2023-11-11
  • rocketmq如何修改存儲(chǔ)路徑

    rocketmq如何修改存儲(chǔ)路徑

    這篇文章主要介紹了rocketmq如何修改存儲(chǔ)路徑的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • java數(shù)據(jù)結(jié)構(gòu)之希爾排序

    java數(shù)據(jù)結(jié)構(gòu)之希爾排序

    這篇文章主要為大家詳細(xì)介紹了java數(shù)據(jù)結(jié)構(gòu)之希爾排序的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-11-11
  • java RMI詳細(xì)介紹及實(shí)例講解

    java RMI詳細(xì)介紹及實(shí)例講解

    這篇文章主要介紹了java RMI詳細(xì)介紹及實(shí)例講解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07
  • SpringBoot之自定義啟動(dòng)異常堆棧信息打印方式

    SpringBoot之自定義啟動(dòng)異常堆棧信息打印方式

    這篇文章主要介紹了SpringBoot之自定義啟動(dòng)異常堆棧信息打印方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08

最新評(píng)論