资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

Spring之WEB模块配置的示例分析

小编给大家分享一下Spring之WEB模块配置的示例分析,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

10年的抚顺县网站建设经验,针对设计、前端、开发、售后、文案、推广等六对一服务,响应快,48小时及时工作处理。全网整合营销推广的优势是能够根据用户设备显示端的尺寸不同,自动调整抚顺县建站的显示方式,使网站能够适用不同显示终端,在浏览器中调整网站的宽度,无论在任何一种浏览器上浏览网站,都能展现优雅布局与设计,从而大程度地提升浏览体验。创新互联从事“抚顺县网站设计”,“抚顺县网站推广”以来,每个客户项目都认真落实执行。

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 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非常简单,但是缺点是代码与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配置

除了正常的配置之外,还需要添加名为struts.objectFactory的常量,把值设为spring,表示该Action由Spring产生。然后把的class属性改为catAction,Struts2将会到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 
  /* 

以上是“Spring之WEB模块配置的示例分析”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


网站标题:Spring之WEB模块配置的示例分析
文章起源:http://cdkjz.cn/article/ipdcge.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220