亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Spring整合Struts2的兩種方法小結

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

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

<!-- 使用ContextLoaderListener初始化Spring容器 -->
 <listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener
  </listener-class>
 </listener>

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

如下獲?。?/strong>

WebApplicationContext ctx=WebApplicationContextUtils.getWebApplicationContext(servletContext)

讓Spring管理控制器

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

其中Struts2只是一個偽控制器,這個偽控制器的功能實際由Spring容器中的控制器來完成,這就實現(xiàn)了讓核心控制器調(diào)用Spring容器中的action來處理用戶請求。在這種策略下,處理用戶請求的Action由Spring插件負責創(chuàng)建,但Spring插件創(chuàng)建Action實例時。并不是利用配置Action時指定的class屬性來創(chuàng)建該action實例,而是從Spring容器中取出對應的Bean實例完成創(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請求。 -->
 <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">
 <!-- 定義一個業(yè)務邏輯組件,實現(xiàn)類為MyServiceImp -->
 <bean id="myService" 
  class="com.bh.service.impl.MyServiceImpl"/>
 <!-- 讓Spring管理的Action實例,因為每個action里包含請求的狀態(tài)信息,所以必須配置scope不能為單例 -->
 <bean id="loginAction" class="com.bh.action.LoginAction"
  scope="prototype">
  <!-- 依賴注入業(yè)務邏輯組件 -->
  <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">
  <!-- 定義處理用戶請求的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:根據(jù)屬性名自動裝配。Spring插件會查找容器中全部Bean,找到其中id屬性與Action所需的業(yè)務邏輯組件同名的Bean,將該bean實例注入到Action實例。

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

Auto:Spring插件會自動檢測需要使用哪種自動裝配方式。

Constructor:與type類似,區(qū)別是constructor使用構造器來構造注入的所需參數(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">
 <!-- 定義一個業(yè)務邏輯組件,實現(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">
  <!-- 定義處理用戶請求的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的兩種方法小結就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

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

    詳解Spring Boot 屬性配置和使用

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

    Spring操作JdbcTemplate數(shù)據(jù)庫的方法學習

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

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

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

    詳解java中的四種代碼塊

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

    SVN 安裝教程之服務器和客戶端

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

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

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

    rocketmq如何修改存儲路徑

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

    java數(shù)據(jù)結構之希爾排序

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

    java RMI詳細介紹及實例講解

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

    SpringBoot之自定義啟動異常堆棧信息打印方式

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

最新評論