现在公司要用SSH架构开发,我想弄个BASEACTION,但不知道怎么写比较好,最好是继承DispatchAction 的,希望大家给个例子

解决方案 »

  1.   

    在里面放一些你经常用到的东西...
    比如private final static ClassPathXmlApplicationContext ctx; // private final static ApplicationContext ctx; static {
    try {
    ctx = new ClassPathXmlApplicationContext(
    "classpath:TEST-INF/applicationContext*"); // ctx = new FileSystemXmlApplicationContext(
    // "classpath:MANPOWER-IBOOKING-INF/applicationContext.xml");
    } catch (ExceptionInInitializerError iniex) {
    iniex.printStackTrace();
    throw new RuntimeException(
    "Error initializing applicationContext.xml. Cause: "
    + iniex);
    }
    } /**
     * 公共的getBean方法 用于得到Spring中定义的bean的实例
     * 
     * @param <T>
     * @param clz
     * @return
     */
    @SuppressWarnings("unchecked")
    public static <T> T lookupService(Class<T> clz) {

    return (T) ctx.getBean(clz.getSimpleName());
    }

    /**
     * 公共的getBean方法 用于得到Spring中定义的bean的实例
     * 
     * @param beanId
     * @return
     */
    @SuppressWarnings("unchecked")
    public static Object lookupService(String beanId) {
    return ctx.getBean(beanId);
    }
      

  2.   

    public abstract class BaseAction extends DispatchAction implements
    ApplicationContextAware { private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext)
    throws BeansException {
    this.applicationContext = applicationContext;
    } public Object getBean(String id) {
    return this.applicationContext.getBean(id); } public abstract ActionForward execute(ActionMapping mapping,
    ActionForm form, HttpServletRequest request,
    HttpServletResponse response) throws Exception;
    }
    这样就行了,也可以轻松获得spring上下文..
      

  3.   

    还可以放上一些常用的service接口
      

  4.   

    一般都有可以取当前用户的方法...获取ApplicationContext什么的..
      

  5.   

    找本《spring In Action》之类的基础书籍做做功课吧。速食面吃多了,营养不全面,影响发育。