最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring之WEB模塊配置詳解

 更新時間:2017年12月04日 15:13:11   作者:冰河winner  
這篇文章主要介紹了Spring之WEB模塊配置詳解,簡單介紹了其繼承方式,代理方式,以及相關(guān)詳細(xì)配置代碼,具有一定借鑒價值,需要的朋友可以了解下。

Spring框架七大模塊簡單介紹

Spring中MVC模塊代碼詳解

Spring的WEB模塊用于整合Web框架,例如Struts1、Struts2、JSF等

整合Struts1

繼承方式

Spring框架提供了ActionSupport類支持Struts1的Action。繼承了ActionSupport后就能獲取Spring的BeanFactory,從而獲得各種Spring容器內(nèi)的各種資源

import org.springframework.web.struts.ActionSupport; 
 
public class CatAction extends ActionSupport{ 
  public ICatService getCarService(){ 
    return (ICatService) getWebApplicationContext().getBean("catService"); 
  } 
  public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){ 
    CatForm catForm = (CatForm) form; 
    if("list".equals(catForm.getAction())){ 
     returnthis.list(mapping,form,request,response); 
    } 
  } 
 
  public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){ 
    CatForm catForm = (CatForm) form; 
    ICatService catService =getCatService(); 
    List<Cat> catList =catService.listCats(); 
    request.setAttribute("carList",catList); 
 
    return mapping.find("list"); 
  } 
} 

Spring在web.xml中的配置

<context-param><!-- Spring配置文件的位置--> 
  <param-name>contextConfigLocation</param-name> 
  <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
</context-param> 
 
<listener><!-- 使用Listener加載Spring配置文件--> 
  <listener-class> 
    org.springframework.web.context.ContextLoaderListener 
  </listener-class> 
</listener> 
 
<filter><!-- 使用Spring自帶的字符過濾器--> 
  <filter-name>CharacterEncodingFilter</filter-name> 
  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 
  <init-param> 
    <param-name>encoding</param-name> 
    <param-value>UTF-8</param-value> 
  </init-param> 
  <init-param> 
    <param-name>forceEncoding</param-name> 
    <param-value>true</param-value> 
  </init-param> 
</filter> 
<filter-mapping> 
  <filter-name>CharacterEncodingFilter</filter-name> 
  <url-pattern>/*</url-pattern> 
</filter-mapping> 

如果與Hibernate結(jié)合使用,需要在web.xml中添加OpenSessionInViewFilter過濾器,將session范圍擴大到JSP層,防止拋出延遲加載異常

<filter> 
  <filter-name>hibernateFilter</filter-name> 
  <filter-class>org.springframework.orm.hibernate3.support. OpenSessionInViewFilter</filter-class> 
</filter> 
<filter-mapping> 
  <filter-name> hibernateFilter</filter-name> 
  <url-pattern>*.do</url-pattern><!-- 對Struts 1的Action啟用--> 
</filter-mapping> 

代理方式

繼承方式融入Spring非常簡單,但是缺點是代碼與Spring發(fā)生了耦合,并且Action并沒有交給Spring管理,因此不能使用Spring的AOP、IoC特性,使用代理方式則可以避免這些缺陷

public class CatAction extends Action{ //此處繼承的Struts 1的Action 
  private ICatService catService; 
  //setter、getter略 
 
  public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){ 
    CatForm catForm = (CatForm) form; 
    if("list".equals(catForm.getAction())){ 
     returnthis.list(mapping,form,request,response); 
    } 
  } 
 
  public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){ 
    CatForm catForm = (CatForm) form; 
    ICatService catService =getCatService(); 
    List<Cat> catList =catService.listCats(); 
    request.setAttribute("carList",catList); 
 
    return mapping.find("list"); 
  } 
} 

這個Action沒有與Spring發(fā)生耦合,只是定義了一個ICatService屬性,然后由Spring負(fù)責(zé)注入

struts-congfig.xml配置

<form-beans> 
  <form-bean name="catForm" type="com.clf.spring.CatForm"> 
</form-beans> 
 
<action-mappings> 
  <action name=" catForm" path="/cat" type="com.clf.spring.CatAction"> 
    <forward name="list" path="/jsp/listCat.jsp"></forward> 
  </action> 
</action-mappings> 
 
<!-- 最核心的配置,該配置把Struts的Action交給Spring代理--> 
<controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" /> 
 
<!-- controller配置生效后,Action的type屬性就是去作用了,Struts不會用type屬性指定的類來創(chuàng)建CatAction,而是到Spring配置中尋找,因此Spring中必須配置CatAction --> 
<!-- Spring中配置Action使用的是name屬性而不是id,Spring會截獲"/cat.do"的請求,將catService通過setter方法注入到CatAction中,并調(diào)用execute()方法--> 
<bean name="/cat" class=" com.clf.spring.CatAction"> 
  <property name="catService" ref="catService" /> 
</bean> 

web.xml的配置與上面的繼承方式相同

使用代理方式的Action可以配置攔截器等Spring特性,例如給CatAction配置方法前攔截器和返回后攔截器

<bean id="catBeforeInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvodor"> 
  <property name="advice"> 
    <bean class="com.clf.spring.MethodBeforeInterceptor" /> 
  </property> 
  <property name="mappedName" value="*"></property> 
</bean> 
 
<bean id="catAfterInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvodor"> 
  <property name="advice"> 
    <bean class="com.clf.spring.MethodAfterInterceptor" /> 
  </property> 
  <property name="mappedName" value="*"></property> 
</bean> 
 
<bean name="/cat" class="org.springframework.aop.framework.ProxyFactoryBean"> 
  <property name="interceptorNames"> 
    <list> 
     <value> catBeforeInterceptor</value> 
     <value> catAfterInterceptor</value> 
    </list> 
  </property> 
  <property name="target"> 
    <bean class="com.clf.spring.CatAction"> 
     <property name="catService" ref="catService"></property> 
    </bean> 
  </property> 
</bean> 

整合Struts 2

Spring整合Struts 2需要struts2-spring-2.011.jar包

public class CatAction{ 
  private ICatService catService; 
  private Cat cat; 
  //setter、getter略 
 
  public String list(){ 
    catService.listCats(); 
    return "list"; 
  } 
  
  public String add(){ 
    catService.createCat(cat); 
    return list(); 
  } 
} 

struts.xml配置

除了正常的配置之外,還需要<contstant/>添加名為struts.objectFactory的常量,把值設(shè)為spring,表示該Action由Spring產(chǎn)生。然后把<action/>的class屬性改為catAction,Struts2將會到Spring中尋找名為catAction的bean

<constant name=" struts.objectFactory" value="spring" /> 
 
<packagenamepackagename="cat" extends="struts-default"> 
<action name="*_cat" method="{1}" class="catAction"> 
  <param name="action" >{1}</param> 
  <result>/list.jsp</result> 
  <result name="list">/list.jsp</result> 
</action> 
</package> 

Spring配置

<bean id="catAction" scope="prototype" class="com.clf.spring.CatAction"> 
  <property name="catService" ref="catService"></property> 
</bean> 

web.xml配置

<context-param><!-- Spring配置文件的位置--> 
  <param-name>contextConfigLocation</param-name> 
  <param-value>/WEB-INF/classes/applicationContext.xml</param-value> 
</context-param> 
 
<listener><!-- 使用Listener加載Spring配置文件--> 
  <listener-class> 
    org.springframework.web.context.ContextLoaderListener 
  </listener-class> 
</listener> 
 
<filter> 
  <filter-name>Struts2</filter-name> 
  <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class> 
</filter> 
<filter-mapping> 
  <filter-name> Struts2</filter-name> 
  <url-pattern>/*</url-pattern> 
</filter-mapping> 

總結(jié)

以上就是本文關(guān)于Spring之WEB模塊配置詳解的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站其他相關(guān)專題,如有不足之處,歡迎留言指出。

參考:

淺談Springmvc中的頁面跳轉(zhuǎn)問題

Spring AOP入門Demo分享

Spring框架web項目實戰(zhàn)全代碼分享

相關(guān)文章

  • SpringCloud開發(fā)課程查詢功能

    SpringCloud開發(fā)課程查詢功能

    這篇文章主要介紹了SpringCloud開發(fā)課程查詢功能,本文通過圖文實例相結(jié)合給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • JDK1.8下載、安裝和環(huán)境配置超詳細(xì)教程(最新最完整)

    JDK1.8下載、安裝和環(huán)境配置超詳細(xì)教程(最新最完整)

    jdk1.8是一款功能強大的Java語音軟件開發(fā)工具包,JDK是學(xué)好Java的第一步,本文重點給大家介紹JDK1.8下載、安裝和環(huán)境配置教程,需要的朋友可以參考下
    2022-11-11
  • 基于Springboot實現(xiàn)定時發(fā)送郵件功能

    基于Springboot實現(xiàn)定時發(fā)送郵件功能

    這篇文章主要為大家詳細(xì)介紹了基于Springboot實現(xiàn)定時發(fā)送郵件功能的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-03-03
  • dubbo如何設(shè)置連接zookeeper權(quán)限

    dubbo如何設(shè)置連接zookeeper權(quán)限

    這篇文章主要介紹了dubbo如何設(shè)置連接zookeeper權(quán)限問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Java中生成隨機數(shù)的4種方式與區(qū)別詳解

    Java中生成隨機數(shù)的4種方式與區(qū)別詳解

    生成隨機數(shù)是我們?nèi)粘i_發(fā)經(jīng)常會遇到的一個功能,這篇文章主要給大家介紹了關(guān)于Java中生成隨機數(shù)的4種方式與區(qū)別、應(yīng)用場景的相關(guān)資料,4個方式分別是Random、ThreadLocalRandom、SecureRandom以及Math,需要的朋友可以參考下
    2021-06-06
  • Java8中LocalDateTime與時間戳timestamp的互相轉(zhuǎn)換

    Java8中LocalDateTime與時間戳timestamp的互相轉(zhuǎn)換

    這篇文章主要給大家介紹了關(guān)于Java8中LocalDateTime與時間戳timestamp的互相轉(zhuǎn)換,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-03-03
  • Java實現(xiàn)PDF文件的分割與加密功能

    Java實現(xiàn)PDF文件的分割與加密功能

    這篇文章主要為大家分享了如何利用Java語言實現(xiàn)PDF文件的分割與加密以及封面圖的生成,文中的示例代碼簡潔易懂,感興趣的可以了解一下
    2022-04-04
  • Java 回調(diào)函數(shù)詳解及使用

    Java 回調(diào)函數(shù)詳解及使用

    這篇文章主要介紹了Java 回調(diào)函數(shù)詳解及使用,附有簡單實例,需要的朋友可以參考下
    2017-03-03
  • Java泛型常見面試題(面試必問)

    Java泛型常見面試題(面試必問)

    泛型在java中有很重要的地位,在面向?qū)ο缶幊碳案鞣N設(shè)計模式中有非常廣泛的應(yīng)用。java泛型知識點也是Java開發(fā)崗位必問的一個話題,今天小編就給大家普及下Java泛型常見面試題,感興趣的朋友一起看看吧
    2021-06-06
  • Java中的ConcurrentLinkedQueue松散隊列解析

    Java中的ConcurrentLinkedQueue松散隊列解析

    這篇文章主要介紹了Java中的ConcurrentLinkedQueue松散隊列解析,鏈表是松散的,鏈表節(jié)點并不都是有效的,允許存在無效節(jié)點val=null,但是只有最后一個節(jié)點才能next=null,需要的朋友可以參考下
    2023-12-12

最新評論

开封市| 兴文县| 巢湖市| 策勒县| 邵阳县| 维西| 扎鲁特旗| 中西区| 伊通| 拉萨市| 伊春市| 汉阴县| 保德县| 勐海县| 满城县| 泽州县| 云霄县| 龙门县| 哈密市| 台中市| 咸阳市| 冕宁县| 抚顺县| 宝应县| 湘潭市| 黄梅县| 招远市| 建瓯市| 基隆市| 固阳县| 运城市| 阳泉市| 宁海县| 当雄县| 朔州市| 乐山市| 沈阳市| 峡江县| 诏安县| 峡江县| 阿拉善右旗|