javastruts2struts2-interceptors

Interceptor: actionInvocation.invoke() without return, but it also jump to the view


I use struts2 to build a login platform. I use an Interceptor extends MethodFilterInterceptor. In the method doIntercept, I use "actionInvocation.invoke()" instead of "return actionInvocation.invoke()". But it also run.

struts-2.5.18

public class loginInterceptor extends MethodFilterInterceptor {
    @Override
    protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
        if (ServletActionContext.getRequest().getSession().getAttribute("user") != null){
            actionInvocation.invoke();
        }
        return null;
    }
}

I think because the method return null, it should not jump to the view. However, it does.


Solution

  • Please see the "Writing Interceptors" docs.

    The important part:

    [...] invoke will return after the result has been called (eg. after your JSP has been rendered), making it perfect for things like open-session-in-view patterns. If you want to do something before the result gets called, you should implement a PreResultListener.

    I.e., if you call invoke, the interceptors and action will be executed as normal.

    To short-circuit normal processing do not call invoke, instead return a result suitable to your purposes, e.g., a login page's global result.