《Spring开发指南》只写了一种与struts整合的方法,另一种到Spring2.0 Demo自带的Doc中查找到Action直接继承ActionSupport。详细信息:
To integrate your Struts application with Spring, you have two options:
-
Configure Spring to manage your Actions as beans, using the ContextLoaderPlugin, and set their dependencies in a Spring context file.
-
Subclass Spring's ActionSupport classes and grab your Spring-managed beans explicitly using a getWebApplicationContext() method.
16.3.2. ActionSupport Classes
As previously mentioned, you can retrieve the WebApplicationContext from the ServletContext using the WebApplicationContextUtils class. An easier way is to extend Spring's Action classes for Struts. For example, instead of subclassing Struts' Action class, you can subclass Spring's ActionSupport class.
The ActionSupport class provides additional convenience methods, like getWebApplicationContext(). Below is an example of how you might use this in an Action:
public class UserAction extends DispatchActionSupport { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { if (log.isDebugEnabled()) { log.debug("entering 'delete' method..."); } WebApplicationContext ctx = getWebApplicationContext(); UserManager mgr = (UserManager) ctx.getBean("userManager"); // talk to manager for business logic return mapping.findForward("success"); }}
Spring includes subclasses for all of the standard Struts Actions - the Spring versions merely have Support appended to the name:
The recommended strategy is to use the approach that best suits your project. Subclassing makes your code more readable, and you know exactly how your dependencies are resolved. However, using the ContextLoaderPlugin allow you to easily add new dependencies in your context XML file. Either way, Spring provides some nice options for integrating the two frameworks.
可见第二种方法更加简便。
JPetstore例子中并没有使用此方法,而使用了第三种方法。同样只需要动Action。如下:
public abstract class BaseAction extends Action {
private PetStoreFacade petStore;
public void setServlet(ActionServlet actionServlet) {
super.setServlet(actionServlet);
if (actionServlet != null) {
ServletContext servletContext = actionServlet.getServletContext();
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
this.petStore = (PetStoreFacade) wac.getBean("petStore");
}
}
protected PetStoreFacade getPetStore() {
return petStore;
}
}
ok,这三种方法都可以让Spring与Struts整合,相比较第一种(因为配置很多这里没写,见refernce 16.3.1),
第二第三种方法比较简洁,侵入不多。
分享到:
相关推荐
SSH笔记-Spring整合Struts2,作用是使用 IOC 容器来管理 Struts2 的 Action
SSH笔记-整合Spring2、Struts2、Hibernate4的完整步骤
整合Spring,换句话说,也就是让spring的IOC功能为我们的struts action注入逻辑组件....
Struts+Spring+Hibernate整合笔记
轻量级J2EE企业应用实战--Struts+Spring+Hibernate整合开发笔记
本文档是在整合struts2,spring4,hiernate4后所做的笔记以及怎么整合技巧
struts+spring+hibernate配置笔记
Struts 2 + Spring 2.0 + Hibernate 3.0整合笔记.doc
SSH开发笔记,特别适合入门者学习
SSH整合学习笔记之struts2+spring+hibernate+c3p0源码
二、 第二步: 首先整合Spring + Hibernate 41 三、 第三步:再来整合Struts2 41 四、 struts的读常量: 43 第十二课:DTO、VO 43 一、 DTO 43 二、 VO 43 第十二课:SSH整合存在的问题 43 一、 Jsp中访问Session时...
spring hibernate struts框架应用及整合
JDK5.0 应用服务器:JBOSS4.0.4GA Spring:2.5 Hibernate:3.1 Struts:1.2 学习心德 作者:姚赪海
java(1) 整合入门(spring,struts,hibernate的整合)资料(1) 中大软件工厂项目前培训资料(1) 完整笔记+源码(1) ssh(1) C#查询参数化例子(1) 学习笔记+完整源码(1) 介绍与深入(1) 学习笔记(不含整合)(1) C#(1) .CHM格式...
struts2.1.8+spring2.5+hibernate3.2+ext3.0框架整合笔记的最终版本,整合过程见于:http://blog.csdn.net/phoenixdsf/article/details/7645655
NULL 博文链接:https://gaojiewyh.iteye.com/blog/988190
整合struts2和spring2.5,整合笔记见于:http://blog.csdn.net/phoenixdsf/article/details/7645655
该资源为SSH框架整合示例(Spring、Struts2、Hibernate),里面包含源代码、jar包等,是直接从MyEclipse中导出的可正常运行的完整项目。其中另含完整开发笔记一份,为此项目搭建的开发过程笔记,可供读者开发练习...
Struts+Spring+Hibernate整合笔记
一、Spring 整合 hibernate 二、Spring 整合 struts 三、spring 事 务 处 理