博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring之WEB模块
阅读量:7039 次
发布时间:2019-06-28

本文共 5211 字,大约阅读时间需要 17 分钟。

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
catList =catService.listCats(); request.setAttribute("carList",catList); return mapping.find("list"); }}

Spring在web.xml中的配置

contextConfigLocation
/WEB-INF/classes/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
CharacterEncodingFilter
org.springframework.web.filter.CharacterEncodingFilter
encoding
UTF-8
forceEncoding
true
CharacterEncodingFilter
/*

假设与Hibernate结合使用。须要在web.xml中加入OpenSessionInViewFilter过滤器,将session范围扩大到JSP层。防止抛出延迟载入异常

hibernateFilter
org.springframework.orm.hibernate3.support. OpenSessionInViewFilter
hibernateFilter
*.do

 

代理方式

继承方式融入Spring很easy,可是缺点是代码与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
catList =catService.listCats(); request.setAttribute("carList",catList); return mapping.find("list"); }}

这个Action没有与Spring发生耦合。仅仅是定义了一个ICatService属性,然后由Spring负责注入

 

struts-congfig.xml配置

 

web.xml的配置与上面的继承方式同样

 

使用代理方式的Action能够配置拦截器等Spring特性,比如给CatAction配置方法前拦截器和返回后拦截器

catBeforeInterceptor
catAfterInterceptor

整合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

{1}
/list.jsp
/list.jsp
 

Spring配置

 

web.xml配置

contextConfigLocation
/WEB-INF/classes/applicationContext.xml
org.springframework.web.context.ContextLoaderListener
Struts2
org.apache.struts2.dispatcher.FilterDispatcher
Struts2
/*

转载地址:http://jkxal.baihongyu.com/

你可能感兴趣的文章
干货 :手把手教你在试验中修正机器学习模型
查看>>
.NET Core2.1下采用EFCore比较原生IOC、AspectCore、AutoFac之间的性能 ...
查看>>
JDK8新特性之Optional
查看>>
Spark2.4.0源码分析之WorldCount 触发作业提交(二)
查看>>
Python零基础学习笔记(四十)—— datetime和Calendar ...
查看>>
事故现场:MySQL 中一个双引号的错位引发的血案 ...
查看>>
MaxCompute_UDF_开发指南
查看>>
云MSP服务案例丨某知名制造集团的Oracle RAC部署实践 ...
查看>>
如何基于ReplayKit实现低延迟rtmp推屏
查看>>
说说JSON和JSONP,也许你会豁然开朗
查看>>
没有所谓好与不好,只是能否适用和用的好
查看>>
程序员写简历时必须注意的技术词汇拼写(持续更新...)
查看>>
ams光学传感器助力小米手机创新发展
查看>>
Python 特色介绍
查看>>
JavaScript_知识点梳理note1
查看>>
PostgreSQL 开启with-llvm(JIT)后,新增插件异常(clang: Command not found)处理
查看>>
思考设计SQL优化方案
查看>>
tomcat 调优-生产环境必备
查看>>
浅析C++的引用与const指针与各种传递方式
查看>>
Java并发编程75道面试题及答案
查看>>