Spring之WEB模块配置详解
作者:冰河winner 发布时间:2023-12-05 13:21:48
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>
总结
参考:
浅谈Springmvc中的页面跳转问题
Spring AOP入门Demo分享
Spring框架web项目实战全代码分享
来源:http://blog.csdn.net/u012152619/article/details/44258343


猜你喜欢
- Mapper:@Mapper@OracleRepositorypublic interface OracleRadiusMapper{@In
- 概述AOP(Aspect Orient Programming),我们一般称为面向方面(切面)编程,作为面向对象的一种补充,用于处理系统中分
- 一、程序运行环境编译环境:IntelliJ IDEA所需测试文件:PDF、.pfx数字证书及密钥、PDF Jar包(Free Spire.P
- 起因曾经用过西门子出的 * , 好处是直接有SDK开发包, 不会硬件开发也能直接使用缺点也是明显的, 就是只支持Windows系统, 另外就
- 问题我需要从一个java的集合中,根据另一个集合的内容,删除第一个集合中不特定的元素。这看上去非常简单,但却遇到了问题。这是我要写的方法的头
- 在实际的工作中直接使用反射的机会比较少,有印象的就是一次自己做的WinForms小工具的时候利用反射来动态获取窗体上的每个控件,并且为必要的
- 本篇文章介绍SpringBoot的上传和下载功能。一、创建SpringBoot工程,添加依赖compile("org.spring
- PPT中的动画效果可分为已有内置动画以及自定义动画。设置内置动画,只需直接指定动画效果类型即可。本文主要介绍如何实现自定义动画,即自定义形状
- 今天给大家介绍一下Java实现钢琴的小程序,程序虽小,功能挺多,支持循环播放,录音等功能,首先简单介绍下源码结构:先看看钢琴界面实现,添加相
- 使用Post添加数据到数据库出现方块乱码解决方法,在web.xml里最前面添加过滤器,代码如下,放在最前面,因为有优先级,要首先拦截<
- bean.xml文件p标签使用报错The prefix "p" for attribute "p:某属性&qu
- webservice 可以用于分布式应用程序之间的交互,和不同程序之间的交互。概念性的东西就不说太多,下面开始创建一个简单的webservi
- 前言最近遇到个小问题,要为几十个文本框添加相同的失去焦点事件,常规的办法是在VS的事件管理器里面添加,但那样太繁琐了,几十个文本框,要加几十
- 介绍在分布式系统、微服务架构大行其道的今天,服务间互相调用出现失败已经成为常态。如何处理异常,如何保证数据一致性,成为微服务设计过程中,绕不
- 前面介绍了Spring Boot 整合mybatis 使用注解的方式实现数据库操作,介绍了如何自动生成注解版的mapper 和pojo类。
- 什么是代理模式?代理模式:在调用处不直接调用目标类进行操作,而是调用代理类,然后通过代理类来调用目标类进行操作。在代理类调用目标类的前后可以
- 近期项目中需要使用到一种类似手机电池充电进度的动画效果,以前没学属性动画的时候,是用图片+定时器的方式来完成的,最近一直在学习动画这一块,再
- 缓存是HTTP协议的一个强大功能,但由于某些原因,它主要用于静态资源,如图像,CSS样式表或JavaScript文件,但是,HTTP缓存不仅
- 概述在学习Spring的时候,在了解基本用法的时候,如果有时间一定要深入源码了解Spring的底层原理,这样在做一些适配工作、写一些轮子的时
- 在APP项目的开发过程中,经常会用到分享图片的功能,有时候还需要根据当前用户信息获取指定的分享图片,比如要求在用户分享图中显示用户名、Uid