JPetStore中Struts和Spring的整合

使用一種更簡單的方式,這種方法不是用Spring集成了Struts,而是Struts去調用Spring所提供的服務,這也是Spring官方自帶的JpetStore例子中所採用的方法。這種方法的主要優點是簡潔,另外StrutsSpring的耦合非常鬆散,如果要改用SpringMVC或其他Web層框架換掉Struts,對其他層幾乎沒有影響,這也是Spring自帶的JpetStore這麼做的一個原因,這裏我們還可以選擇用SpringMVCStruts,只要改一下配置就可以了。但缺點也是很明顯的,就是StrutsAction並不在Spring可管理的範圍之內,也就是說,Action並不是Spring中的一個Bean,這樣,對於StrutsAction,無法使用Spring時的事務處理等服務。不過一般來說,需要事務的部分儘量放在業務層去做,Action中不應包含太複雜的操作和邏輯,所以,這種StrutsSpring綜合應用的方法對一般項目來說還是可行的。如果有特殊需求,可以考慮其他幾種集成SpringStruts的方法,以便SpringStrutsAction進行管理。

先建立一個BascAction,它繼承了Action類,而其他自定義的Action都要繼承這個BaseAction

/**

 *

 */

package com.ascent.struts.action;

 

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

 

import org.apache.struts.action.Action;

import org.apache.struts.action.ActionError;

import org.apache.struts.action.ActionErrors;

 

import com.ascent.bean.Customer;

import com.ascent.business.IBookService;

import com.ascent.business.ICustomerService;

import com.ascent.business.IOrderItemService;

import com.ascent.business.IOrderService;

import com.ascent.util.AppContext;

 

/**

 * @author 

 *

 */

public class BaseAction extends Action {

 

    /**

     *

     */

    public BaseAction() {

        super();

    }

 

    protected IBookService getBookService() {

        return (IBookService) AppContext.getInstance().getAppContext(). getBean(

                "bookService");

    }

   

    protected IOrderService getOrderService() {

        return (IOrderService) AppContext.getInstance().getAppContext(). getBean(

                "orderService");

    }

   

    protected ICustomerService getCustomerService() {

        return (ICustomerService) AppContext.getInstance().getAppContext(). getBean(

                "customerService");

    }

    protected IOrderItemService getOrderItemService() {

        return (IOrderItemService) AppContext.getInstance().getAppContext(). getBean(

                "orderItemService");

    }

    protected String checkUser(HttpServletRequest request,

            HttpServletResponse response){

        Customer user = null;

        user = (Customer) request.getSession().getAttribute("user");

        if(user==null){

   

            System.out.println("you have no loginning!!!!");

            ActionErrors errors = new ActionErrors();

            errors.add(ActionErrors.GLOBAL_MESSAGE,new ActionError("errors. login"));

            this.saveErrors(request,errors);

            return null;

        }else{

            return user.getCustName();

        }

       

       

       

    }

 

}


/**

 *

 */

package com.ascent.util;

 

import org.springframework.context.support.*;

 

/**

 * <p>Title: bookstoressh</p>

 *

 * <p>Description: bookstore System</p>

 *

 * <p>Copyright: Copyright (c) 2005</p>

 *

 * <p>Company: ascenttech</p>

 *

 * @author 

 * @version 1.0

 */

public class AppContext {

 

  private static AppContext instance;

 

  private AbstractApplicationContext appContext;

 

  public synchronized static AppContext getInstance() {

    if (instance == null) {

      instance = new AppContext();

    }

    return instance;

  }

 

  private AppContext() {

    this.appContext = new ClassPathXmlApplicationContext(

        "applicationContext.xml");

  }

 

  public AbstractApplicationContext getAppContext() {

    return appContext;

  }

}

如果在Struts中定義了一個繼承自BaseActionAction,那麼就可以通過getXXXService()的方法來得到某一個service的實例,繼而可以實現對業務方法的調用。這裏實際上使用了一個設計模式—服務定位器(service locator)。

 
發佈了37 篇原創文章 · 獲贊 0 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章