Spring读书笔记(四)与Struts整合
来源:互联网 发布:淘宝天猫规则大全 编辑:程序博客网 时间:2024/06/05 09:56
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:
ActionSupport,
DispatchActionSupport,
LookupDispatchActionSupport and
MappingDispatchActionSupport.
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),
第二第三种方法比较简洁,侵入不多。
- Spring读书笔记(四)与Struts整合
- Spring读书笔记(四)与Struts整合
- 整合Struts 与Spring
- 整合Struts 与Spring
- 整合Struts 与Spring
- struts与spring整合
- spring与struts整合
- Spring与Struts整合
- Spring与Struts整合
- Spring与struts整合
- Spring与Struts整合
- spring 与struts 整合
- struts与spring整合
- Spring与Struts整合
- spring与struts整合
- spring+struts四种整合
- 【Spring】与Struts框架整合(八)
- Spring与Struts如何整合
- 人生值得珍藏的10句话
- cnet转载:专访Java之父:Java会愈老愈俏吗?
- 伟大的创新
- eclipse+lomboz+mssql开发电子商务心得
- 发布jtotem2 Demo
- Spring读书笔记(四)与Struts整合
- 严蔚敏的数据结构c语言版 和 William Ford/William Topp的数据结构C++语言描述
- 北京购买火车票实用指南(转)
- 将 Web 项目从 Visual Studio .Net 2002/2003 转换到 Visual Studio 2005 的分步指南
- 23种设计模式之有趣见解
- [转载]如何在PHP中使用数组
- SQL查询问题请教(难度稍高)
- Knight Path Plan<update20060115>
- iis 配置怎么看aspx网页 谢谢