欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Spring之WEB模块配置详解

程序员文章站 2023-12-04 11:54:16
spring框架七大模块简单介绍 spring中mvc模块代码详解 spring的web模块用于整合web框架,例如struts1、struts2、jsf等 整合st...

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由sprin*生。然后把<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> 

总结

以上就是本文关于spring之web模块配置详解的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。

参考:

浅谈springmvc中的页面跳转问题

spring aop入门demo分享

spring框架web项目实战全代码分享