Spring之WEB模块配置详解

 更新时间:2017年12月04日 15:13:11   作者:冰河winner  
这篇文章主要介绍了Spring之WEB模块配置详解,简单介绍了其继承方式,代理方式,以及相关详细配置代码,具有一定借鉴价值,需要的朋友可以了解下。

Spring框架七大模块简单介绍

Spring中MVC模块代码详解

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

整合Struts1

继承方式

Spring框架提供了ActionSupport类支持Struts1的Action。继承了ActionSupport后就能获取Spring的BeanFactory,从而获得各种Spring容器内的各种资源

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结合使用,需要在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发生了耦合,并且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发生耦合,只是定义了一个ICatService属性,然后由Spring负责注入

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属性指定的类来创建CatAction,而是到Spring配置中寻找,因此Spring中必须配置CatAction --> 
<!-- Spring中配置Action使用的是name属性而不是id,Spring会截获"/cat.do"的请求,将catService通过setter方法注入到CatAction中,并调用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的常量,把值设为spring,表示该Action由Spring产生。然后把<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> 

总结

以上就是本文关于Spring之WEB模块配置详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。

参考:

浅谈Springmvc中的页面跳转问题

Spring AOP入门Demo分享

Spring框架web项目实战全代码分享

相关文章

  • 深入了解Java中Synchronized的各种使用方法

    深入了解Java中Synchronized的各种使用方法

    在Java当中synchronized关键字通常是用来标记一个方法或者代码块。本文将通过示例为大家详细介绍一下Synchronized的各种使用方法,需要的可以参考一下
    2022-08-08
  • IDEA生成项目maven-tree依赖目录树结构方式

    IDEA生成项目maven-tree依赖目录树结构方式

    这篇文章主要介绍了IDEA生成项目maven-tree依赖目录树结构方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-12-12
  • Java开发岗位面试被问到嵌套类怎么办

    Java开发岗位面试被问到嵌套类怎么办

    本篇文章主要介绍了深入理解Java嵌套类和内部类,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-07-07
  • Java实现简单画画画板

    Java实现简单画画画板

    这篇文章主要为大家详细介绍了Java实现简单画画画板,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-06-06
  • Nacos简介最新收藏版

    Nacos简介最新收藏版

    Nacos 是阿里巴巴推出来的一个新开源项目,这是一个更易于构建云原生应用的动态服务发现、配置管理和服务管理平台,Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台,对Nacos相关基本知识感兴趣的朋友一起看看吧
    2023-08-08
  • Spring boot Rabbitmq消息防丢失实践

    Spring boot Rabbitmq消息防丢失实践

    这篇文章主要介绍了Spring boot Rabbitmq消息防丢失实践,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下
    2022-09-09
  • Java正则表达式的语法及示例解析

    Java正则表达式的语法及示例解析

    这篇文章主要介绍了Java正则表达式的语法及示例解析,需要的朋友可以参考下
    2017-07-07
  • Java整合RabbitMQ实现五种常见消费模型

    Java整合RabbitMQ实现五种常见消费模型

    本文将深入介绍RabbitMQ的五种常见消费模型,包括简单队列模型、工作队列模型、发布/订阅模型、路由模型和主题模型,删除线格式并探讨它们各自的优缺点和适用场景,感兴趣的可以了解一下
    2023-11-11
  • 从最基本的Java工程搭建SpringMVC+SpringDataJPA+Hibernate

    从最基本的Java工程搭建SpringMVC+SpringDataJPA+Hibernate

    本文会介绍从一个最基本的java工程,到Web工程,到集成Spring、SpringMVC、SpringDataJPA+Hibernate,本文介绍的非常详细,具有参考借鉴价值,感兴趣的朋友一起学习吧
    2016-05-05
  • Spring Boot自定义 Starter并推送到远端公服的详细代码

    Spring Boot自定义 Starter并推送到远端公服的详细代码

    这篇文章主要介绍了Spring Boot自定义 Starter并推送到远端公服,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-09-09

最新评论