简体   繁体   中英

struts2 interceptor redirecting twice to action

i have a logininterceptor that checks session member object and if it is null redirect it to login page,else continue to action .

Every action blocks has this interceptor. The problem is When you call action it hits the interceptor, if it is true continue to action method, then return "success" or "input" then again hits the interceptor to redirect that selected result name.

How can i prevent to call interceptor twice?

Interceptor code :

public String intercept(ActionInvocation actionInvocation) throws Exception {
        HttpServletRequest request = (HttpServletRequest) actionInvocation.getInvocationContext().get(StrutsStatics.HTTP_REQUEST);
        HttpSession session = request.getSession();

    if (session.getAttribute("member")==null) {

        return Action.LOGIN;
    } else {
        return actionInvocation.invoke();
    }
}

struts.xml action view :

    <action name="actionName" class="actionClass" method="init">
        <interceptor-ref name="loginStack"/>
        <result name="input">show.jsp</result>
    </action>

If you redirect to a result that has the same interceptor in its refs of course it will be called twice.

If you forward to a JSP as you show here, the interceptor will not be called again.


There's no need to declare this for every action--set the default interceptor stack. For actions that don't need this stack (like login) define an interceptor ref for that action that doesn't include this interceptor.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM