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
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java单例模式的线程安全,饿汉和懒汉模式详解
浅析C# 状态机Stateless
springboot + mybatis + druid + 多数据源的问题详解
![](https://img.aspxhome.com/file/2023/2/84282_0s.png)
解决IntelliJ IDEA中鼠标拖动选择为矩形区域问题
![](https://img.aspxhome.com/file/2023/6/65176_0s.png)
完成OSS.Http底层HttpClient重构封装 支持标准库
![](https://img.aspxhome.com/file/2023/0/119420_0s.png)
C#使用iTextSharp封装的PDF文件操作类实例
Android开发Jetpack组件DataBinding用例详解
![](https://img.aspxhome.com/file/2023/9/88349_0s.gif)
C#中除去所有在HTML元素中标记
Android实现多段颜色进度条效果
![](https://img.aspxhome.com/file/2023/1/138911_0s.gif)
java根据网络地址保存图片的方法
C#实现为视频添加水印
![](https://img.aspxhome.com/file/2023/2/119462_0s.png)
springboot整合mybatisplus的方法详解
![](https://img.aspxhome.com/file/2023/9/125559_0s.png)
自定义BufferedReader的实例
java 中动态代理机制的实例讲解
java的main方法中调用spring的service方式
浅谈@FeignClient中name和value属性的区别
![](https://img.aspxhome.com/file/2023/0/78270_0s.png)
android PopupWindow 和 Activity弹出窗口实现方式
![](https://img.aspxhome.com/file/2023/8/137498_0s.png)
C#实现带阴历显示的日期代码
Java 日期转换详解及实例代码
Android性能优化之JVMTI与内存分配
![](https://img.aspxhome.com/file/2023/8/128958_0s.jpg)