Spring之WEB模块

Spring的WEB模块用于整合Web框架,例如Struts 1、Struts 2、JSF等

整合Struts 1

继承方式

Spring框架提供了ActionSupport类支持Struts 1的Action。继承了ActionSupport后就能获取Spring的BeanFactory,从而获得各种Spring容器内的各种资源

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. import  org.springframework.web.struts.ActionSupport;  
  2.    
  3. public class CatAction extends ActionSupport{  
  4.       public ICatService getCarService(){  
  5.              return (ICatService) getWebApplicationContext().getBean("catService");  
  6.       }  
  7.       public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){  
  8.              CatForm catForm = (CatForm) form;  
  9.              if("list".equals(catForm.getAction())){  
  10.                     returnthis.list(mapping,form,request,response);  
  11.              }  
  12.       }  
  13.    
  14.       public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){  
  15.              CatForm catForm = (CatForm) form;  
  16.              ICatService catService =getCatService();  
  17.              List<Cat> catList =catService.listCats();  
  18.              request.setAttribute("carList",catList);  
  19.    
  20.              return mapping.find("list");  
  21.       }  
  22. }  


Spring在web.xml中的配置

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <context-param><!--  Spring配置文件的位置-->  
  2.       <param-name>contextConfigLocation</param-name>  
  3.       <param-value>/WEB-INF/classes/applicationContext.xml</param-value>  
  4. </context-param>  
  5.    
  6. <listener><!--  使用Listener加载Spring配置文件-->  
  7.       <listener-class>  
  8.              org.springframework.web.context.ContextLoaderListener  
  9.       </listener-class>  
  10. </listener>  
  11.    
  12. <filter><!--  使用Spring自带的字符过滤器-->  
  13.       <filter-name>CharacterEncodingFilter</filter-name>  
  14.       <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  15.       <init-param>  
  16.              <param-name>encoding</param-name>  
  17.              <param-value>UTF-8</param-value>  
  18.       </init-param>  
  19.       <init-param>  
  20.              <param-name>forceEncoding</param-name>  
  21.              <param-value>true</param-value>  
  22.       </init-param>  
  23. </filter>  
  24. <filter-mapping>  
  25.       <filter-name>CharacterEncodingFilter</filter-name>  
  26.       <url-pattern>/*</url-pattern>  
  27. </filter-mapping>  

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

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <filter>  
  2.       <filter-name>hibernateFilter</filter-name>  
  3.       <filter-class>org.springframework.orm.hibernate3.support. OpenSessionInViewFilter</filter-class>  
  4. </filter>  
  5. <filter-mapping>  
  6.       <filter-name> hibernateFilter</filter-name>  
  7.       <url-pattern>*.do</url-pattern><!--  对Struts 1的Action启用-->  
  8. </filter-mapping>  

 

代理方式

继承方式融入Spring非常简单,但是缺点是代码与Spring发生了耦合,并且Action并没有交给Spring管理,因此不能使用Spring的AOP、IoC特性,使用代理方式则可以避免这些缺陷

 

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public class CatAction extends Action{  //此处继承的Struts 1的Action  
  2.       private ICatService catService;  
  3.       //setter、getter略  
  4.    
  5.       public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){  
  6.              CatForm catForm = (CatForm) form;  
  7.              if("list".equals(catForm.getAction())){  
  8.                     returnthis.list(mapping,form,request,response);  
  9.              }  
  10.       }  
  11.    
  12.       public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){  
  13.              CatForm catForm = (CatForm) form;  
  14.              ICatService catService =getCatService();  
  15.              List<Cat> catList =catService.listCats();  
  16.              request.setAttribute("carList",catList);  
  17.    
  18.              return mapping.find("list");  
  19.       }  
  20. }  

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

 

struts-congfig.xml配置

 

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <form-beans>  
  2.       <form-bean name="catForm" type="com.clf.spring.CatForm">  
  3. </form-beans>  
  4.    
  5. <action-mappings>  
  6.       <action name=" catForm"  path="/cat" type="com.clf.spring.CatAction">  
  7.              <forward name="list" path="/jsp/listCat.jsp"></forward>  
  8.       </action>  
  9. </action-mappings>  
  10.    
  11. <!--  最核心的配置,该配置把Struts的Action交给Spring代理-->  
  12. <controller processorClass="org.springframework.web.struts.DelegatingRequestProcessor" />  
  13.    
  14. <!-- controller配置生效后,Action的type属性就是去作用了,Struts不会用type属性指定的类来创建CatAction,而是到Spring配置中寻找,因此Spring中必须配置CatAction -->  
  15. <!--  Spring中配置Action使用的是name属性而不是id,Spring会截获"/cat.do"的请求,将catService通过setter方法注入到CatAction中,并调用execute()方法-->  
  16. <bean name="/cat" class=" com.clf.spring.CatAction">  
  17.       <property name="catService" ref="catService" />  
  18. </bean>  

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

 

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

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <bean id="catBeforeInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvodor">  
  2.       <property name="advice">  
  3.              <bean class="com.clf.spring.MethodBeforeInterceptor" />  
  4.       </property>  
  5.       <property name="mappedName" value="*"></property>  
  6. </bean>  
  7.    
  8. <bean id="catAfterInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvodor">  
  9.       <property name="advice">  
  10.              <bean class="com.clf.spring.MethodAfterInterceptor" />  
  11.       </property>  
  12.       <property name="mappedName" value="*"></property>  
  13. </bean>  
  14.    
  15. <bean name="/cat" class="org.springframework.aop.framework.ProxyFactoryBean">  
  16.       <property name="interceptorNames">  
  17.              <list>  
  18.                     <value> catBeforeInterceptor</value>  
  19.                     <value> catAfterInterceptor</value>  
  20.              </list>  
  21.       </property>  
  22.       <property name="target">  
  23.              <bean class="com.clf.spring.CatAction">  
  24.                     <property name="catService" ref="catService"></property>  
  25.              </bean>  
  26.       </property>  
  27. </bean>  



整合Struts 2

Spring整合Struts 2需要struts2-spring-2.011.jar包

[java]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. public class CatAction{  
  2.       private ICatService catService;  
  3.       private Cat cat;  
  4.       //setter、getter略  
  5.    
  6.       public String list(){  
  7.              catService.listCats();  
  8.              return "list";  
  9.       }  
  10.        
  11.       public String add(){  
  12.              catService.createCat(cat);  
  13.              return list();  
  14.       }  
  15. }  

struts.xml配置

除了正常的配置之外,还需要<contstant/>添加名为struts.objectFactory的常量,把值设为spring,表示该Action由Spring产生。然后把<action/>的class属性改为catAction,Struts 2将会到Spring中寻找名为catAction的bean

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <constant name=" struts.objectFactory" value="spring" />  
  2.    
  3. <packagenamepackagename="cat" extends="struts-default">  
  4. <action name="*_cat" method="{1}" class="catAction">  
  5.       <param name="action" >{1}</param>  
  6.       <result>/list.jsp</result>  
  7.       <result name="list">/list.jsp</result>  
  8. </action>  
  9. </package>  
 

Spring配置

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <bean id="catAction" scope="prototype" class="com.clf.spring.CatAction">  
  2.       <property name="catService" ref="catService"></property>  
  3. </bean>  
 

web.xml配置

[html]  view plain copy print ? 在CODE上查看代码片 派生到我的代码片
  1. <context-param><!--  Spring配置文件的位置-->  
  2.       <param-name>contextConfigLocation</param-name>  
  3.       <param-value>/WEB-INF/classes/applicationContext.xml</param-value>  
  4. </context-param>  
  5.    
  6. <listener><!--  使用Listener加载Spring配置文件-->  
  7.       <listener-class>  
  8.              org.springframework.web.context.ContextLoaderListener  
  9.       </listener-class>  
  10. </listener>  
  11.    
  12. <filter>  
  13.       <filter-name>Struts2</filter-name>  
  14.       <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>  
  15. </filter>  
  16. <filter-mapping>  
  17.       <filter-name> Struts2</filter-name>  
  18.       <url-pattern>/*</url-pattern>  
  19. </filter-mapping>  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值