JPetStore中Struts和Spring的整合

使用一种更简单的方式,这种方法不是用 Spring 集成了 Struts ,而是 Struts 去调用 Spring 所提供的服务,这也是 Spring 官方自带的 JpetStore 例子中所采用的方法。这种方法的主要优点是简洁,另外 Struts Spring 的耦合非常松散,如果要改用 SpringMVC 或其他 Web 层框架换掉 Struts ,对其他层几乎没有影响,这也是 Spring 自带的 JpetStore 这么做的一个原因,这里我们还可以选择用 SpringMVC Struts ,只要改一下配置就可以了。但缺点也是很明显的,就是 Struts Action 并不在 Spring 可管理的范围之内,也就是说, Action 并不是 Spring 中的一个 Bean ,这样,对于 Struts Action ,无法使用 Spring 时的事务处理等服务。不过一般来说,需要事务的部分尽量放在业务层去做, Action 中不应包含太复杂的操作和逻辑,所以,这种 Struts Spring 综合应用的方法对一般项目来说还是可行的。如果有特殊需求,可以考虑其他几种集成 Spring Struts 的方法,以便 Spring Struts Action 进行管理。

先建立一个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)。

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值