国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JSP > 正文

Spring學習基礎---與Struts整合

2024-09-05 00:17:17
字體:
來源:轉載
供稿:網友
《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: 

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), 第二第三種方法比較簡潔,侵入不多。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 石柱| 南部县| 治多县| 顺昌县| 临沧市| 河北区| 兰考县| 桃园市| 贺兰县| 和静县| 平乡县| 台南市| 南陵县| 邢台市| 沿河| 吉林省| 大渡口区| 浦县| 汾阳市| 临沂市| 铜川市| 准格尔旗| 泾川县| 龙胜| 那曲县| 吴旗县| 济阳县| 宁南县| 德保县| 鄂伦春自治旗| 邢台市| 资溪县| 科技| 潮州市| 霍林郭勒市| 石屏县| 保山市| 咸阳市| 义乌市| 霍林郭勒市| 吐鲁番市|