Spring學習筆記系列(四) 與struts整合

          《Spring開發(fā)指南》只寫了一種與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),
          第二第三種方法比較簡潔,侵入不多。

          posted on 2007-06-18 18:00 chenguo 閱讀(197) 評論(0)  編輯  收藏 所屬分類: Spring Dev

          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          導航

          統(tǒng)計

          留言簿

          隨筆分類(1)

          文章分類(52)

          好友 小山的博客

          最新隨筆

          最新評論

          主站蜘蛛池模板: 广汉市| 兴山县| 江都市| 肥西县| 甘南县| 八宿县| 浮山县| 桐柏县| 古丈县| 万宁市| 开江县| 钟山县| 海南省| 商南县| 林周县| 屏边| 理塘县| 钦州市| 抚州市| 鹤岗市| 富平县| 始兴县| 杭锦后旗| 梨树县| 贡觉县| 嵊泗县| 乃东县| 沂源县| 池州市| 长宁县| 通榆县| 汝州市| 西盟| 孝义市| 鹰潭市| 新密市| 吉安市| 新宁县| 修武县| 海阳市| 泰来县|