Spring是一个轻量级(大小和系统开支的角度)的IoC和AOP容器。它力图简化
J2EE开 发即J2EE without EJB。而且作为帮助企业级开发的核心支柱,Spring为模型层(OR持久层:Hibernate、JDO、iBatis等)服务层(EJB、 JNDI、WebService)以及表现层(Struts、JSF、Velocity)都提供了良好的支持和集成方案。
Jakarta-Struts是Apache软件组织提供的一个开源项目。它为Java Web应用提供了基于Model-View-Controller的MVC框架,尤其适用于开发大型可扩展的Web应用。尽管基于Java的MVC框架层出不穷,事实上Spring的MVC模型也提供了驱动应用系统Web层的能力,但Jakarta-Struts仍然是所有这些框架中的佼佼者。
下面,将如何整合这两个J2EE领域的经典项目给出两套详尽的集成方案。
1.首先我们来看一个Spring-Struts整合应用下的控制器Action类源代码。
<ccid_code>public class CourceAction extends Action { private CourceService courceService; public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Set allCources = courceService.getAllCources(); //..........the other statements request.setAttribute("cources", allCources); return mapping.findForward("jspView"); } }</ccid_code>
|
|
分析:CourceService为一个业务实现的接口,此接口声明了一系列的业务处理方法。该方法的实现配置为Spring上下问的一个Bean。由此看来,我们大家都可能会产生一个疑问:Struts action如何取得一个包含在Spring上下文中的Bean呢?为了回答这个问题,Spring提供了两种与Struts集成的方式:
(1).从一个知晓Spring上下文的基类派生我们自己的Struts Action类。然后,在派生类中就可以使用super.XX()方法来获得一个对Spring受控Bean的引用。
(2).将请求委托给作为Spring Bean管理的Struts Action来处理。
2.注册Spring插件:
为了使Struts Action能够访问由Spring管理的Bean,我们就必须要注册一个知道Spring应用上下文的Struts插件。可以在struts-config.xml中通过如下的方式来完成注册。
<ccid_code><plug-in classname="org.springframework.web.struts.ContextLoadPlugin">
<set-property property="contextConfigLocation" value="WEB-INF/Yhcip.xml,......">
</set-property>
</ccid_code>ContextLoadPlugin()负责装载一个Spring应用上下文。(具体的说:是一个WebApplicationContext)。value属性值为要加载的配置Spring受控Bean的xml文件的URI。
3.完成第一种集成方案:实现一个知晓Spring的Action基类。
这种集成方案是从一个公共的能够访问Spring应用上下文的基类中派生所有的Struts Action,但值得庆幸的是:我们不用自己去编写这个知晓Spring应用上下文的基类,因为Spring已经提供了 org.springframework.web.struts.ActionSupport:一个 org.apache.struts.action.Action的抽象实现。它重载了setServlet()方法以从 ContextLoaderPlugin中获取WebapplicationContext。因此,任何时候我们只需要调用super.getBean ()方法即可获得一Spring上下文中的一个Bean的引用。
我们再来看一段Action源代码:
<ccid_code>public class CourceAction extends ActionSupport { public ActionForward execute( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { //取得Spring上下文 ApplicationContext context = super.getWebApplicationContext(); //取得CourceService Bean CourseService courseService = (CourseService) context.getBean("courseService"); Set allCources = courceService.getAllCources(); request.setAttribute("cources", allCources); //..........the other statements. return mapping.findForward("jspView"); }}</ccid_code>
|
|
分析:这个Action类由ActionSupport派生,当CourceAction需要一个Spring受控Bean时:它首先调用基类 的getWebApplicationContext()方法以取得一个Spring应用上下文的引用;接着它调用getBean()方法来获取由 Spring管理的courceService Bean的一个引用。
小结
至此,我们已经用第一种方案圆满的完成了Spring与Struts的集成工作。这种集成方式的好处在于直观简洁容易上手。除了需要从 ActionSupport中派生,以及需要从应用上下文中获取Bean之外,其他都与在非Spring的Struts中编写和配置Action的方法相 似。但这种集成方案也有不利的一面。最为显著的是:我们的Action类将直接使用Spring提供的特定类,这样会使我们的Struts Action(即控制层)的代码与Spring紧密耦合在一起。这是我们不情愿看到的。另外,Action类也负责查找由Spring管理的Bean,这 违背了反向控制(IoC)的原则。
分享到:
相关推荐
struts-spring-other-lib 等jar包struts-spring-other-lib 等jar包struts-spring-other-lib 等jar包struts-spring-other-lib 等jar包struts-spring-other-lib 等jar包struts-spring-other-lib 等jar包struts-spring-...
struts-spring-hibernate-_-integration-2.zip
struts2-spring-plugin-2.3.15.2.jar ; struts2-json-plugin-2.3.16.3.jarstruts2-spring-plugin-2.3.15.2.jar ; struts2-json-plugin-2.3.16.3.jar
Java-Spring+Struts+Hibernate-SSH框架整合01-Spring+Hibernate(一步一步的教你敲代码)(博客园,博客名称:Java-Spring+Struts+Hibernate-SSH框架整合01-Spring+Hibernate)
JavaEE源代码 struts2-spring-plugin-2.0.11JavaEE源代码 struts2-spring-plugin-2.0.11JavaEE源代码 struts2-spring-plugin-2.0.11JavaEE源代码 struts2-spring-plugin-2.0.11JavaEE源代码 struts2-spring-plugin-...
spring-webmvc-struts-2.5.6-sources
struts2-spring-plugin-2.3.31.jar
最新struts2-spring-plugin-2.3.24.1.jar
SSI(Struts2 + Spring + IBatis)框架集成。
struts2-struts1-plugin-2.3.30.jar, struts2-testng-plugin-2.3.30.jar, struts2-tiles-plugin-2.3.30.jar, struts2-tiles3-plugin-2.3.30.jar, tiles-api-2.2.2.jar, tiles-compat-3.0.1.jar, tiles-core-2.2.2....
struts2-spring-plugin-2.2.1.jarstruts2-spring-plugin-2.2.1.jarstruts2-spring-plugin-2.2.1.jar
Struts-spring-Hibernate.Struts-spring-Hibernate.Struts-spring-Hibernate.
struts2-spring-plugin-2.0.6.jar
struts2+spring整合插件 struts2-spring-plugin-2.0.12.jar
org.springframework.web.struts-sources-3.0.4.RELEASE.jar
struts-2.5.1-spring-4.2.7-hibernate-5.0.9 整合的项目 可以直接用 . spring 4.3 + hibernate 5.0 测试不可用 冲头
韩顺平spring 雇员管理系统 struts-1.3.8-lib 所有jar包
整合了spring和struts加mysql数据库驱动,没有的快过来看看了~
spring.jar spring-aop.jar spring-aop.jar spring-beans.jar spring-hibernate3.jar spring-jdbc.jar spring-struts.jar spring-web.jar
spring4整合struts2所需要的jar包,缺少这个jar包则会报错