查看: 158|回复: 0

[Java学习] Spring之WEB模块配置详解

发表于 4 天前

Spring框架七大模块简单介绍

Spring中MVC模块代码详解

Spring的WEB模块用于整合Web框架,例如Struts1、Struts2、JSF等

整合Struts1

继承方式

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

  1. import org.springframework.web.struts.ActionSupport;
  2. public class CatAction extends ActionSupport{
  3. public ICatService getCarService(){
  4. return (ICatService) getWebApplicationContext().getBean("catService");
  5. }
  6. public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){
  7. CatForm catForm = (CatForm) form;
  8. if("list".equals(catForm.getAction())){
  9. returnthis.list(mapping,form,request,response);
  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. return mapping.find("list");
  18. }
  19. }
复制代码

Spring在web.xml中的配置

  1. <context-param><!-- Spring配置文件的位置-->
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  4. </context-param>
  5. <listener><!-- 使用Listener加载Spring配置文件-->
  6. <listener-class>
  7. org.springframework.web.context.ContextLoaderListener
  8. </listener-class>
  9. </listener>
  10. <filter><!-- 使用Spring自带的字符过滤器-->
  11. <filter-name>CharacterEncodingFilter</filter-name>
  12. <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
  13. <init-param>
  14. <param-name>encoding</param-name>
  15. <param-value>UTF-8</param-value>
  16. </init-param>
  17. <init-param>
  18. <param-name>forceEncoding</param-name>
  19. <param-value>true</param-value>
  20. </init-param>
  21. </filter>
  22. <filter-mapping>
  23. <filter-name>CharacterEncodingFilter</filter-name>
  24. <url-pattern>/*</url-pattern>
  25. </filter-mapping>
复制代码

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

  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特性,使用代理方式则可以避免这些缺陷

  1. public class CatAction extends Action{ //此处继承的Struts 1的Action
  2. private ICatService catService;
  3. //setter、getter略
  4. public ActionForward execute(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){
  5. CatForm catForm = (CatForm) form;
  6. if("list".equals(catForm.getAction())){
  7. returnthis.list(mapping,form,request,response);
  8. }
  9. }
  10. public ActionForward list(ActionMappingmapping,ActionForm form,HttpServletRequest request,HttpServletResponseresponse){
  11. CatForm catForm = (CatForm) form;
  12. ICatService catService =getCatService();
  13. List<Cat> catList =catService.listCats();
  14. request.setAttribute("carList",catList);
  15. return mapping.find("list");
  16. }
  17. }
复制代码

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

struts-congfig.xml配置

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

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

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

  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. <bean id="catAfterInterceptor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvodor">
  8. <property name="advice">
  9. <bean class="com.clf.spring.MethodAfterInterceptor" />
  10. </property>
  11. <property name="mappedName" value="*"></property>
  12. </bean>
  13. <bean name="/cat" class="org.springframework.aop.framework.ProxyFactoryBean">
  14. <property name="interceptorNames">
  15. <list>
  16. <value> catBeforeInterceptor</value>
  17. <value> catAfterInterceptor</value>
  18. </list>
  19. </property>
  20. <property name="target">
  21. <bean class="com.clf.spring.CatAction">
  22. <property name="catService" ref="catService"></property>
  23. </bean>
  24. </property>
  25. </bean>
复制代码

整合Struts 2

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

  1. public class CatAction{
  2. private ICatService catService;
  3. private Cat cat;
  4. //setter、getter略
  5. public String list(){
  6. catService.listCats();
  7. return "list";
  8. }
  9. public String add(){
  10. catService.createCat(cat);
  11. return list();
  12. }
  13. }
复制代码

struts.xml配置

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

  1. <constant name=" struts.objectFactory" value="spring" />
  2. <packagenamepackagename="cat" extends="struts-default">
  3. <action name="*_cat" method="小贝" class="catAction">
  4. <param name="action" >小贝</param>
  5. <result>/list.jsp</result>
  6. <result name="list">/list.jsp</result>
  7. </action>
  8. </package>
复制代码

Spring配置

  1. <bean id="catAction" scope="prototype" class="com.clf.spring.CatAction">
  2. <property name="catService" ref="catService"></property>
  3. </bean>
复制代码

web.xml配置

  1. <context-param><!-- Spring配置文件的位置-->
  2. <param-name>contextConfigLocation</param-name>
  3. <param-value>/WEB-INF/classes/applicationContext.xml</param-value>
  4. </context-param>
  5. <listener><!-- 使用Listener加载Spring配置文件-->
  6. <listener-class>
  7. org.springframework.web.context.ContextLoaderListener
  8. </listener-class>
  9. </listener>
  10. <filter>
  11. <filter-name>Struts2</filter-name>
  12. <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  13. </filter>
  14. <filter-mapping>
  15. <filter-name> Struts2</filter-name>
  16. <url-pattern>/*</url-pattern>
  17. </filter-mapping>
复制代码

总结

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

参考:

浅谈Springmvc中的页面跳转问题

Spring AOP入门Demo分享

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



回复

使用道具 举报