javastruts2struts2-interceptorsinterceptorstackstruts2-rest-plugin

The page is not redirecting properly by interceptor in Struts 2


That's it, I code an interceptor login, the result is

<result name="login" type="redirectAction">access</result>

All works ok, but I think that is recalling it, the result go to access and again execute the interceptor and go access etc. I believe that because my browser shows the message:

the page is not redirecting properly.

I'm using Struts 2 and Rest plugin.

This is my interceptor:

@Override
public String intercept(ActionInvocation invocation) throws Exception {

    HttpSession session = ServletActionContext.getRequest().getSession(false);
    Object loginObject = session.getAttribute("login");
    boolean login = false;

    if (loginObject != null) {
        login = (Boolean) loginObject;
        if (!login) {
            return Action.LOGIN;
        } else {
            return invocation.invoke();
        }
    } else {
        return Action.LOGIN;
    }
   

Solution

  • When you return Action.LOGIN from within the Interceptor, the result is executed, and since the result is of type redirectAction, another request is created and filtered through the Interceptor Stack. Then the execution will trigger again your Interceptor, that is the unwanted result.

    You need to exclude the execution of your Login Interceptor for the access action, using the default stack, for example.

    <default-interceptor-ref name="myCustomLoginStack"/>
    
    <action name="access" class="foo.bar.actions.Access">
        <result>access.jsp</result>
        <interceptor-ref name="defaultStack" />
    </action>
    
    <action name="foobar" class="foo.bar.actions.Foobar">
        <result>foo.jsp</result>
        <result name="login" type="redirectAction">access</result>
    </action>