`
friend365
  • 浏览: 54357 次
  • 性别: Icon_minigender_2
  • 来自: 上海
最近访客 更多访客>>
社区版块
存档分类
最新评论

Struts和Spring集成文档

阅读更多

本文想通过一个简单的实例阐述如何集成Struts和Spring。

1.Struts和Spring

Struts 代表了MVC第二类架构的实现,在Struts中最重要的组件是ActionServlet,Action和 ActionForm 子类,ActionServlet 代表controller,他基于配置文件接受请求和把这些请求转发到相应的ActionForm和Action子类。 ActionForm把用户输入的数据传送到Action,Action调用商务层组件完成必要的操作,最后提交到view。ActionServlet 使用一个配置文件(struts-config.xml)加载Action子类的定义用以接受用户请求,基于请求URL, controller 找到一个action定义去接受这个请求,Struts构件处理用户请求, 检查配置文件, 完成相应的动作。

Spring是一种轻量级的容器,它使得使用一个外部XML配置文件非常容易绑定对象,每个对象能够通过列出JavaBean 属性得到一个依赖对象的指针,通过绑定XML配置文件使剩下的工作更加简单。依赖注入(DI)是非常强大的功能,Spring支持可插拔的事务管理器,提供事物管理方式更多的选择. 它集成了持久性的架构同时也提供了一个统一的exception 分类,Spring也提供面向方面(AOP)编程的简单机制。

2.Struts和Spring的集成

将Struts应用集成到Spring框架可以采用多种方法,首先Spring明显地被设计用于解决JEE的现实问题,如复杂性,性能低下,可测试性及其他;第二,Spring框架包括一个AOP实现让你可以使用面向方面的程序设计技术;第三, Spring 框架可以能够非常容易地管理和协调Struts;和Struts类似,Spring也包含MVC 实现,两个架构都有优缺点,Struts是MVC最重要的架构,很多开发团队学会了依靠Struts在规定时限内开发出高质量的软件,因此开发团队宁愿集成Spring的功能也不愿意转到Spring MVC;好消息是Spring的结构允许你集成Struts Web 框架、基于Spring的业务层和持久层,我们的方法是应用Spring中的ActionSupport类去集成Struts。

3.加载应用的context

首先我们需要使用Spring中的ContextLoaderPlugin为Struts ActionServlet去装载Spring应用的上下文,简单在struts-config.xml 文件中增加plug-in,如下(1)所示:

  1. ﹤ ?xml version="1.0" encoding="ISO-8859-1" ?﹥  
  2. ﹤ !DOCTYPE struts-config PUBLIC  
  3. "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"  
  4. "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"﹥  
  5. ﹤ struts-config﹥  
  6. ﹤ form-beans﹥  
  7. ﹤ form-bean name="searchForm" 
  8. type="org.apache.struts.validator.DynaValidatorForm"﹥  
  9. ﹤ form-property name="cardno" type="java.lang.String"/﹥  
  10. ﹤ /form-bean﹥  
  11. ﹤ /form-beans﹥  
  12. ﹤ global-forwards type="org.apache.struts.action.ActionForward"﹥  
  13. ﹤ forward name="welcome" path="/welcome.do"/﹥  
  14. ﹤ forward name="searchEntry" path="/searchEntry.do"/﹥  
  15. ﹤ forward name="searchSubmit" path="/searchSubmit.do"/﹥  
  16. ﹤ /global-forwards﹥  
  17. ﹤ action-mappings﹥  
  18. ﹤ action path="/welcome" forward="/WEB-INF/pages/welcome.htm"/﹥  
  19. ﹤ action path="/searchEntry" forward="/WEB-INF/pages/search.jsp"/﹥  
  20. ﹤ action path="/searchSubmit" 
  21. type=" com.infotek.Creditcard.actions.SearchSubmit" 
  22. input="/searchEntry.do" 
  23. validate="true" 
  24. name="searchForm"﹥  
  25. ﹤ forward name="success" path="/WEB-INF/pages/detail.jsp"/﹥  
  26. ﹤ forward name="failure" path="/WEB-INF/pages/search.jsp"/﹥  
  27. ﹤ /action﹥  
  28. ﹤ /action-mappings﹥  
  29. ﹤ message-resources parameter="ApplicationResources"/﹥  
  30. ﹤ plug-in className="org.apache.struts.validator.ValidatorPlugIn"﹥  
  31. ﹤ set-property property="pathnames" value="/WEB-INF/validator-rules.                                                              xml,/WEB-INF/validation.xml"/﹥  
  32. ﹤ /plug-in﹥  
  33. ﹤ plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"﹥ (1)  
  34. ﹤ set-property property="contextConfigLocation" value="/WEB-INF/beans.xml"/﹥  
  35. ﹤ /plug-in﹥  
  36. ﹤ /struts-config﹥﹤ struts-config﹤ forward > 

4.使用Spring的ActionSupport类

要用Spring去集成Struts,创建一个Spring 上下文是必须要做的。 org.springframework.web.struts.ActionSupport 类提供一个 getWebApplicationContext() 方法非常容易地获得Spring上下文,全部你需要去做的是从Spring的ActionSupport 代替Struts 中的Action类去延伸你的action,如下所示:

  1. package com.infotek.Creditcard.actions;  
  2. import java.io.IOException;  
  3. import javax.servlet.ServletException;  
  4. import javax.servlet.http.HttpServletRequest;  
  5. import javax.servlet.http.HttpServletResponse;  
  6. import org.apache.struts.action.ActionError;  
  7. import org.apache.struts.action.ActionErrors;  
  8. import org.apache.struts.action.ActionForm;  
  9. import org.apache.struts.action.ActionForward;  
  10. import org.apache.struts.action.ActionMapping;  
  11. import org.apache.struts.action.DynaActionForm;  
  12. import org.springframework.context.ApplicationContext;  
  13. import org.springframework.web.struts.ActionSupport;  
  14. import com. infotek.Creditcard.beans.Creditcard;  
  15. import com. infotek.Creditcard.business.CreditcardService;  
  16. public class SearchSubmit extends ActionSupport { |(1)  
  17. public ActionForward execute(ActionMapping mapping,ActionForm form,  
  18. HttpServletRequest request,HttpServletResponse response)  
  19. throws IOException, ServletException {  
  20. DynaActionForm searchForm = (DynaActionForm) form;  
  21. String isbn = (String) searchForm.get("cardno");  
  22. //the old fashion way  
  23. //CreditcardService creditcardService = new CreditcardServiceImpl();  
  24. ApplicationContext ctx = getWebApplicationContext(); |(2)  
  25. CreditcardService creditcardService =  
  26. (CreditcardService ) ctx.getBean("creditcardService"); |(3)  
  27. CreditCard creditard = CreditCardService.read(cardno.trim());  
  28. if (null == creditard) {  
  29. ActionErrors errors = new ActionErrors();  
  30. errors.add(ActionErrors.GLOBAL_ERROR,new ActionError ("message.notfound"));  
  31. saveErrors(request, errors);  
  32. return mapping.findForward("failure") ;  
  33. }  
  34. request.setAttribute("creditcard", creditcard);  
  35. return mapping.findForward("success");
  36. }  

在(1)中,我们通过延伸Spring ActionSupport 类而不是Struts Action 类创建了一个action;在(2)中,我们使用getWebApplicationContext()方法获得一个 ApplicationContext;为了获得商务服务, 在(3)中,我们使用ApplicationContext去查找Spring bean;这个技术非常容易理解,不幸的是它把Struts的action和Spring framework绑定了,如果你想替换Spring你不得不重写代码,而且Struts的action不在Spring的控制之下, 遗憾的是这种方法无法获得Spring AOP的好处。

5.结论

本文我们尝试使用Spring的ActionSupport,ContextLoaderPlugIn去集成Struts,这是一种最高效的和最简单的方式,另外还可用Spring中的代理子类去代理Struts中的RequestProcessor和代理Struts的actions.

更多学习资料:IT无忧

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics