Spring的WEB模块用于整合Web框架,例如Struts 1、Struts 2、JSF等

整合Struts 1

继承方式

Spring框架提供了ActionSupport类支持Struts 1的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,Struts 2将会到Spring中寻找名为catAction的bean

<constant name=" struts.objectFactory" value="spring" />

<packagename="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模块的更多相关文章

  1. [02] Spring主要功能模块概述

    1.Spring主要功能模块   1.1 Core Container Spring的核心容器模块,其中包括: Beans Core Context SpEL Beans和Core模块,是框架的基础部 ...

  2. 四、Spring Boot Web开发

    四.Web开发 1.简介 使用SpringBoot: 1).创建SpringBoot应用,选中我们需要的模块: 2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可 ...

  3. 4.Spring Boot web开发

    1.创建一个web模块 (1).创建SpringBoot应用,选中我们需要的模块: (2).SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置就可以运行起来 (3).自己编 ...

  4. 解释WEB 模块?

    Spring的WEB模块是构建在application context 模块基础之上,提供一个适合web应用的上下文.这个模块也包括支持多种面向web的任务,如透明地处理多个文件上传请求和程序级请求参 ...

  5. 解释 WEB 模块?

    Spring 的 WEB 模块是构建在 application context 模块基础之上,提供一个适 合 web 应用的上下文.这个模块也包括支持多种面向 web 的任务,如透明地处理 多个文件上 ...

  6. spring源码分析之spring-web web模块分析

    0 概述 spring-web的web模块是更高一层的抽象,它封装了快速开发spring-web需要的基础组件.其结构如下: 1. 初始化Initializer部分 1.1  Servlet3.0 的 ...

  7. Spring Boot 多模块项目创建与配置 (一) (转)

    Spring Boot 多模块项目创建与配置 (一) 最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都 ...

  8. Spring boot 多模块项目 + Swagger 让你的API可视化

    Spring boot 多模块项目 + Swagger 让你的API可视化 前言 手写 Api 文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不 ...

  9. Spring Boot 多模块项目创建与配置 (一)

    最近在负责的是一个比较复杂项目,模块很多,代码中的二级模块就有9个,部分二级模块下面还分了多个模块.代码中的多模块是用maven管理的,每个模块都使用spring boot框架.之前有零零散散学过一些 ...

随机推荐

  1. js将一个数组插入另一个数组

    var cont =[1,2,3,4]; var res =[4,5,6] for(var i=0;i<res;i++){ cont.push( res.data.list[i]); } con ...

  2. [LeetCode] Valid Palindrome II 验证回文字符串之二

    Given a non-empty string s, you may delete at most one character. Judge whether you can make it a pa ...

  3. 实战分享:如何成功防护1.2T国内已知最大流量DDoS攻击

    作者:腾讯云宙斯盾安全团队&腾讯安全平台部 引言: DDoS攻击势头愈演愈烈,除了攻击手法的多样化发展之外,最直接的还是攻击流量的成倍增长.3月份国内的最大规模DDoS攻击纪录还停留在数百G规 ...

  4. C#中string的相关方法

    下面的方法一般都有很多重载形式,作为初学者的我先把我用过的记录下来吧...以后用到其他的可以一点点添加: 直接上例子吧.先定义两个字符串str1,str2(不要吐槽命名==) string str1, ...

  5. k8s踩坑记 - kubeadm join 之 token 失效

    抛砖引玉 环境 centos 7 amd64 两台 kubernetes 1.10 伴随着k8s1.10版本的发布,前天先在一台机器上搭建了k8s单机版集群,即既是master,也是node,按照经验 ...

  6. [HNOI 2014]道路堵塞

    Description A国有N座城市,依次标为1到N.同时,在这N座城市间有M条单向道路,每条道路的长度是一个正整数.现在,A国 交通部指定了一条从城市1到城市N的路径,并且保证这条路径的长度是所有 ...

  7. [FJOI2007]轮状病毒

    题目描述 轮状病毒有很多变种.许多轮状病毒都是由一个轮状基产生.一个n轮状基由圆环上n个不同的基原子和圆心的一个核原子构成.2个原子之间的边表示这2个原子之间的信息通道,如图1. n轮状病毒的产生规律 ...

  8. 【BZOJ2242】【SDOI2011】计算器

    Description 你被要求设计一个计算器完成以下三项任务: 1.给定y.z.p,计算y^z mod p 的值: 2.给定y.z.p,计算满足xy ≡z(mod p)的最小非负整数x: 3.给定y ...

  9. UVA4731:Cellular Network

    根据排序不等式可知,逆序和最小(就是两个向量坐标一个递增一个递减,那么乘起来就最小) 所以排一下序,然后做一下线性dp即可 #include<cstdio> #include<cst ...

  10. 2015 多校联赛 ——HDU5349(水)

    Problem Description A simple problem Problem Description You have a multiple set,and now there are t ...