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项目实战全代码分享

(0)

相关推荐

  • 七个Spring核心模块详解

    Spring的七个核心模块,供大家参考,具体内容如下 1.Spring core:核心容器 核心容器提供spring框架的基本功能.Spring以bean的方式组织和管理Java应用中的各个组件及其关系.Spring使用BeanFactory来产生和管理Bean,它是工厂模式的实现.BeanFactory使用控制反转(IoC)模式将应用的配置和依赖性规范与实际的应用程序代码分开.BeanFactory使用依赖注入的方式提供给组件依赖.主要实现控制反转IoC和依赖注入DI.Bean配置以及加载.

  • SpringBoot创建maven多模块项目实战代码

    工作中一直都是一个人奋战一人一个项目,使用maven管理,看这个也挺好,但是总感觉没有充分发挥maven的功能,于是研究了一下这个,网上关于这个的文章很多,虽然不是很好,但我从中收获了很多,在这集百家所长,写一份实战记录,大家跟着我一块做吧! 声明:构建多模块不是最难的,难点是如果把多模块打包成一个执行jar. SpringBoot官方推崇的是富jar,也就是jar文件启动项目,所以如果在这里打war包我不具体介绍,如果需要的朋友可以给我留言,我回复. 建议clone项目后,在看教程(有不足的地

  • Spring框架七大模块简单介绍

    Spring 是一个开源框架,是为了解决企业应用程序开发复杂性而创建的.框架的主要优势之一就是其分层架构,分层架构允许您选择使用哪一个组件,同时为 J2EE 应用程序开发提供集成的框架. Spring框架的7个模块 组成 Spring框架的每个模块(或组件)都可以单独存在,或者与其他一个或多个模块联合实现.每个模块的功能如下: 1核心模块 SpringCore模块是Spring的核心容器,它实现了IOC模式,提供了Spring框架的基础功能.此模块中包含的BeanFactory类是Spring的

  • 详解Maven 搭建spring boot多模块项目(附源码)

    本文介绍了Maven 搭建spring boot多模块项目,分享给大家,具体如下: 备注:所有项目都在idea中创建 1.idea创建maven项目 1-1: 删除src,target目录,只保留pom.xml 1-2: 根目录pom.xml可被子模块继承,因此项目只是demo,未考虑太多性能问题,所以将诸多依赖.都写在根级`pom.xml`,子模块只需继承就可以使用. 1-3: 根级pom.xml文件在附录1 1-4: 依赖模块 mybatis spring-boot相关模块 2.创建子模块(

  • Spring之ORM模块代码详解

    Spring框架七大模块简单介绍 Spring中MVC模块代码详解 ORM模块对Hibernate.JDO.TopLinkiBatis等ORM框架提供支持 ORM模块依赖于dom4j.jar.antlr.jar等包 在Spring里,Hibernate的资源要交给Spring管理,Hibernate以及其SessionFactory等知识Spring一个特殊的Bean,有Spring负责实例化与销毁.因此DAO层只需要继承HibernateDaoSupport,而不需要与Hibernate的AP

  • 深入浅析Spring-boot-starter常用依赖模块

    Spring-boot的2大优点: 1.基于Spring框架的"约定优先于配置(COC)"理念以及最佳实践之路. 2.针对日常企业应用研发各种场景的Spring-boot-starter自动配置依赖模块,且"开箱即用"(约定spring-boot-starter- 作为命名前缀,都位于org.springframenwork.boot包或者命名空间下). 应用日志和spring-boot-starter-logging 常见的日志系统大致有:java.util默认提

  • 浅谈springboot多模块(modules)开发

    为何模块开发 先举个栗子,同一张数据表,可能要在多个项目中或功能中使用,所以就有可能在每个模块都要搞一个mybatis去配置.如果一开始规定说这张表一定不可以改字段属性,那么没毛病.但是事实上, 一张表从项目开始到结束,不知道被改了多少遍,所以,你有可能在多个项目中去改mybatis改到吐血! 在举一个栗子,一个web服务里包含了多个功能模块,比如其中一个功能可能会消耗大量资源和时间,当用户调用这个功能的时候,可能会影响到其他功能的正常使用,这个时候,如果把各个功能模块分出来单独部署,然后通过h

  • jsp、struts、spring、mybatis实现前端页面功能模块化拆分的方案

    前端页面功能模块化拆分 当一个系统的功能很多时,不可能所有功能模块的页面都写在一个页面里面,这时就需要将不同功能模块的页面拆分出去,就像模板一样,需要哪块的功能就调用哪块的页面,然后加载相应数据并展示到相应的页面. 本应用的使用spring+struts+mybatis+jsp的方式,用两种方案来完成前端页面功能的拆分. 方案一: 在JSP页面中,利用EL表达式或者Java代码的方式,在后台完成页面数据的填充.然后在js中来完成页面的切换. jsp代码: 业务详情模块页面:riskDetailI

  • Spring中MVC模块代码详解

    SpringMVC的Controller用于处理用户的请求.Controller相当于Struts1里的Action,他们的实现机制.运行原理都类似 Controller是个接口,一般直接继承AbstrcatController,并实现handleRequestInternal方法.handleRequestInternal方法相当于Struts1的execute方法 import org.springframework.web.servlet.ModelAndView; import org.

  • SpringBoot 监控管理模块actuator没有权限的问题解决方法

    SpringBoot 1.5.9 版本加入actuator依赖后,访问/beans 等敏感的信息时候报错,如下 Tue Mar 07 21:18:57 GMT+08:00 2017 There was an unexpected error (type=Unauthorized, status=401). Full authentication is required to access this resource. 肯定是权限问题了.有两种方式: 1.关闭权限:application.prop

随机推荐