ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

java-内部Interceptor.intercept()中,我如何知道该动作是否已经执行?

2019-10-12 05:01:13  阅读:297  来源: 互联网

标签:java struts2 interceptor


我正在使用拦截器在基于Struts的应用程序中实现一些功能,而对其生命周期的工作方式却感到困惑.根据Struts文档(“Interceptors”“Writing interceptors”“Big picture”),它应该像这样工作:

FirstInterceptor
  NextInterceptor
    LastInterceptor
      Action
      Result
    LastInterceptor
  NextInterceptor
FirstInterceptor

这是有道理的,但是我在如何区分在操作之前执行的拦截器调用与在结果呈现之后执行的拦截器调用之间有所挣扎(我在这里跳过了PreResultListeners).如果我启动了调试器,则会收到两次调用intercept()的调用,并且在所传递的ActionInvocation上找不到任何明显的内容. (更新:这部分是我的主要困惑,一旦知道,我就可以在下面回答我的问题)

“Big picture”页面对被调用的“之前”和“之后”“子句”有些混淆,但是我不知道该怎么做:

[…]

This includes invoking any Interceptors (the before clause) in advance of invoking the Action itself.

[…]

Interceptors are executed again (in reverse order, calling the after clause).

[…]

(更新:这两个句子仍然很糟糕)

解决方法:

拦截器没有两个调用:

public class MyInterceptor implements Interceptor {

    public String intercept(ActionInvocation invocation) {
        /*
        This is before Action.execute(),
        and before all interceptors down the stack
        */

        String code = invocation.invoke();

        /*
        This is after Action.execute(),
        the result rendering and all
        interceptors down the stack,
        but before the interceptors
        higher up in the stack.
        */

        return code;
    }

}

(请注意,我在调试器中看到的“两次拦截调用”是我没有注意到的不太明显的重定向的结果.这使我很困惑.)

标签:java,struts2,interceptor
来源: https://codeday.me/bug/20191012/1898108.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有