【问题标题】:Who calls intercept method of interceptor in Struts 2谁调用Struts 2中拦截器的intercept方法
【发布时间】:2015-03-07 21:15:40
【问题描述】:

我正在使用 Struts2 拦截器,容器在咨询配置管理器 (struts.xml) 后为相关动作类创建动作代理。现在是执行拦截器链的时候了,我有一个简单的问题——到底是谁调用了Myinterceptor 类中的拦截方法。

public class MyInterceptor  implements Interceptor{

    public void destroy() {
        // TODO Auto-generated method stub

    }

    public void init() {
        // TODO Auto-generated method stub

    }

    public String intercept(ActionInvocation ai) throws Exception {
        // TODO Auto-generated method stub
        long t1=System.currentTimeMillis();

        ValueStack s= ai.getStack();
        String val=s.findString("name");
        s.set("name", val.toUpperCase()+"_changed");

        return ai.invoke();

    }

}

据我了解,它是由相应动作类的 actionproxy 调用的,与 Spring AOP 中的做法相同,其中 proxyfactorybean 执行类似的操作来调用类的建议(切入点)。

但我没有任何文档来支持这一点。 请让我知道我是否正确。

【问题讨论】:

  • 嘿!如果您发现答案正确,请接受答案。

标签: java struts2 struts2-interceptors


【解决方案1】:

ActionInvocation 类

  1. Framework首先调用ActionInvocation的invoke()方法开始执行Action。

  2. ActionInvocaton 通过执行堆栈中的第一个拦截器来启动调用过程。

【讨论】:

    【解决方案2】:

    您的拦截必须覆盖接口Interceptor 的方法intercept()。只需在您的方法中添加 @Override 注释即可。

    @Override
    public String intercept(ActionInvocation ai) throws Exception {
       //your code
    }
    

    【讨论】:

    【解决方案3】:

    您将它作为方法intercept 的参数获取。您不必远离拦截器实例,因为调用者在拦截时会将自己传递给每个拦截器实例。

    这个方法调用的地方是DefaultActionInvocation

    public class DefaultActionInvocation extends Object implements ActionInvocation
    

    默认的 ActionInvocation 实现

    【讨论】:

    • 是的,但我想知道像“myInteceptor.intercept(ai)”这样的语句究竟是从哪里被调用的。
    猜你喜欢
    • 2011-10-25
    • 1970-01-01
    • 2023-03-31
    • 1970-01-01
    • 2014-05-07
    • 2011-03-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多