WebWork的核心是Xwork,而Xwork的核心可能就是Interceptor了。上周我在学习Action的时候已经接触到了Interceptor的概念,Interceptor的作用就是在执行Action前后进行拦截,使用户有机会执行其他操作。

在Xwork中,AOP是通过Action、ActionInvocation和Interceptor这三个接口完成的。WebWork通过Action代理类ActionProxy来执行Action的execute方法,而该方法则调用ActionInvocation的invokeAction方法具体执行Action中的方法。在DefaultActionInvocation的invoke方法中可以看到拦截器的执行逻辑,代码如下:

public String invoke() throws Exception {
    ......
    if (interceptors.hasNext()) {
        InterceptorMapping interceptor = (InterceptorMapping) interceptors.next();
        resultCode = interceptor.getInterceptor().intercept(this);
    } else {
        resultCode = invokeActionOnly();
    }
    ......
}

看看上周的那个DefaultWorkflowInterceptor是如何工作的:
    protected String doIntercept(ActionInvocation invocation) throws Exception {
       
       // 取得要拦截的Action对象
        Object action = invocation.getAction();
       
       
        if (action instanceof Validateable) {
            /*
             * 如果action是Validateable接口的一个实例,则执行接口的validate方法,这个方法可能是检查用户
             * 输入的合法性,如果有错误,可能往errors里增加错误信息。
            Validateable validateable = (Validateable) action;
            if (_log.isDebugEnabled()) {
                _log.debug("Invoking validate() on action "+validateable);
            }
           
            try {
                PrefixMethodInvocationUtil.invokePrefixMethod(
                        invocation,
                        new String[] { VALIDATE_PREFIX, ALT_VALIDATE_PREFIX });
            }
            catch(Exception e) {
                e.printStackTrace();
                // If any exception occurred while doing reflection, we want
                // validate() to be executed
                _log.warn("an exception occured while executing the prefix method", e);
            }
           
           
            if (alwaysInvokeValidate) {
                validateable.validate();
            }
        }
       
        /*
          * 如果action是ValidationAware的实例,则检查action中是否包含错误信息,如果有,则返回
          * INPUT的Result代码,并且整个Action就终止。
          */
        if (action instanceof ValidationAware) {
            ValidationAware validationAwareAction = (ValidationAware) action;

            if (validationAwareAction.hasErrors()) {
                if (_log.isDebugEnabled()) {
                    _log.debug("Errors on action "+validationAwareAction+", returning result name 'input'");
                }
                return Action.INPUT;
            }
        }

         /*
          * 输入合法,执行action的功能。
          */
        return invocation.invoke();
    }
评论
Randlee 2007-03-05
学有所获
justcode 2007-02-24
嗯,对的!
javafever
  • 浏览: 15288 次
  • 性别: Icon_minigender_1
  • 来自: Sichuan Chengdu
  • 详细资料
搜索本博客
最近加入圈子
存档
最新评论
  • Mule入门
    简单来说,mule有两个主要功能: 1:作为一系列协议或者技术的实现引擎,如mu ...
    -- by boyingking
  • Mule入门
    关于中文乱码的问题,到现在我也没有找到好的办法,有一个Encoder的属性,但是 ...
    -- by welcomyou
  • Mule入门
    我也是输入中文出现乱码,楼上的那位兄弟解决了这个问题了么?
    -- by changyanping
  • Mule入门
    兄弟可不可以给个录个视频呀?我老是搞不对!郁闷,要是能得到您的指点的话不慎感激!
    -- by changyanping
  • Mule入门
    谢谢哦 调试通过了 不过输入中文有乱码 能不能说下怎么解决中文乱码的问题
    -- by ainrio
评论排行榜