拦截器:actionInvocation.invoke() 没有return,但它也跳转到视图

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

我用struts2搭建了一个登录平台。我使用拦截器扩展 MethodFilterInterceptor。在方法 doIntercept 中,我使用 "actionInvocation.invoke()" 而不是 "return actionInvocation.invoke()"。但它也 运行.

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;
    }
}

我觉得因为方法return null,所以不应该跳转到视图。然而,确实如此。

请参阅"Writing Interceptors" docs

重要部分:

[...] 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.

即,如果您调用 invoke,拦截器和操作将正常执行。

要缩短正常处理,请不要调用invoke,而是return适合您目的的结果,例如登录页面的全局结果.