paramsPrepareParamsStack(源码详解)



paramsPrepareParamsStack

我觉得在讲解标题内容之前有必要在了解一下我们所谓的Action到底有哪些功能,

1:通过实现xxx Aware 可以使Action内部获得属性

2:XXXCapable可以给struts环境提供属性

3:实现特定的接口以供拦截器调用

4:自身的action方法

5:Javabean

该拦截器栈的执行大致过程为:params---prepare---modeldriven---params

作用:可以先给栈顶对象的属性赋值,在根据栈顶对象的属性决定用modeldirven把那个对象压入栈顶,而prepare拦截器提供getModel方法的model,再对其进行赋值。

 

public StringdoIntercept(ActionInvocation invocation) throws Exception {

        Object action = invocation.getAction();

//判断action是否继承了Preparable

        if (action instanceof Preparable) {

            try {

                String[] prefixes;

firstCallPrepareDo默认为0,故执行else,其数组为:preparepreparedo

                if (firstCallPrepareDo) {

                    prefixes = new String[] {ALT_PREPARE_PREFIX, PREPARE_PREFIX};

                } else {

                    prefixes = new String[] {PREPARE_PREFIX, ALT_PREPARE_PREFIX};

                }

///调用函数

                PrefixMethodInvocationUtil.invokePrefixMethod(invocation,prefixes);

            }

            catch (InvocationTargetException e) {

                Throwable cause = e.getCause();

                if (cause instanceof Exception) {

                    throw (Exception) cause;

                } else if(cause instanceof Error) {

                    throw (Error) cause;

                } else {

                    throw e;

                }

            }

/ alwaysInvokePrepare默认为1,执行action实现的prepare方法;

            if (alwaysInvokePrepare) {

                ((Preparable)action).prepare();

            }

        }

        return invocation.invoke();

    }

}

 

 

 

 

public static void invokePrefixMethod(ActionInvocationactionInvocation, String[] prefixes) throws InvocationTargetException,IllegalAccessException {

        Object action =actionInvocation.getAction();

       

        String methodName =actionInvocation.getProxy().getMethod();

       

        if (methodName == null) {

                        methodName = DEFAULT_INVOCATION_METHODNAME;

        }

        ///执行getPrefixedMethod来获得Method;

        Method method = getPrefixedMethod(prefixes,methodName, action);

        if (method != null) {

            method.invoke(action, new Object[0]);

        }

 

 

 

 

 

public static Method getPrefixedMethod(String[] prefixes, StringmethodName, Object action) {

        assert(prefixes != null);

        String capitalizedMethodName = capitalizeMethodName(methodName);

        for (String prefixe : prefixes) {

 

获得prepare[mothedname]方法;

            String prefixedMethodName = prefixe+ capitalizedMethodName;

            try {

                returnaction.getClass().getMethod(prefixedMethodName, EMPTY_CLASS_ARRAY);

            }

            catch (NoSuchMethodException e) {

                // hmm -- OK, try next prefix

                if (LOG.isDebugEnabled()) {

                    LOG.debug("cannot find method [#0] in action[#1]",prefixedMethodName, action.toString());

                }

            }

        }

        return null;

    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值