Struts2拦截器 —— prepare拦截器

实现类

com.opensymphony.xwork2.interceptor.PrepareInterceptor

 

作用

为实现了com.opensymphony.xwork2.Preparable接口的action调用相关方法。

 

两个参数

参数名

值类型

默认值

描述

alwaysInvokePrepare

boolean

true

如果其值为true则调用接口定义的prepare方法。

firstCallPrepareDo

boolean

false

如果值为true,则调用prepareDoXXX方法。

否则调用prepareXXX方法(XXX为action对应的方法)。

 

关键源码

    public String doIntercept(ActionInvocation invocation) throws Exception {
        Object action = invocation.getAction();
        
if (action instanceof Preparable) {
            
try {
                String[] prefixes;
                
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) {
                
/*
                 * The invoked method threw an exception and reflection wrapped it
                 * in an InvocationTargetException.
                 * If possible re-throw the original exception so that normal
                 * exception handling will take place.
                 */
                Throwable cause = e.getCause();
                
if (cause instanceof Exception) {
                    
throw (Exception) cause;
                }
else if(cause instanceof Error) {
                    
throw (Error) cause;
                }
else {
                    
/*
                     * The cause is not an Exception or Error (must be Throwable) so
                     * just re-throw the wrapped exception.
                     */
                    
throw e;
                }
            }
            
if (alwaysInvokePrepare) {
                ((Preparable) action).prepare();
            }
        }
        
return invocation.invoke();
    }

 

执行顺序

struts2,如果Action实现了Prepare接口:

prepare<方法名>()  --> prepare() --> validate<方法名>方法 --> execute方法


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值