Spring读书笔记(四)与Struts整合

《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),
第二第三种方法比较简洁,侵入不多。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值