http-redirectstruts2interceptorinterceptorstack

Struts 2 interceptor redirect JSP page


Using Struts 2, I have this interceptor:

public class AccessInterceptor extends AbstractInterceptor {
    @Override
    public String intercept(ActionInvocation ai) throws Exception {
        ActionContext actionContext = ai.getInvocationContext();
        Map<String, Object> session = actionContext.getSession();
        Boolean logged = (Boolean) session.get("logged");
        if(logged == null) {
          session.put("logged", false);
          logged = false;
        }
        if (!logged) {
            // If exception is here, it IS thrown, so I am sure "login" is returned on /homepage
            return "login";
        }   
        return ai.invoke();
    }
}

And this struts.xml:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">

<struts>
<constant name="struts.devMode" value="true" />
<package name="loginInterceptor" namespace="/" extends="struts-default">
    <interceptors> 
        <interceptor name="access"
         class="logininterceptor.interceptors.AccessInterceptor"/>
        <interceptor-stack name="appDefault"> 
            <interceptor-ref name="access" />
            <interceptor-ref name="defaultStack" /> 
        </interceptor-stack> 
    </interceptors> 
    <default-interceptor-ref name="appDefault" />       
    <global-results>
        <result name="login">/WEB-INF/content/login.jsp</result>
    </global-results>
</package>
</struts>

I have also /WEB-INF/content/homepage.jsp JSP and /WEB-INF/content/login.jsp and mypackage.LoginAction.java class. There is not an acton class for homepage

The problem is, that when I go to the /homepage page, it is NOT redirected to login.jsp and I see the homepage.jsp content. What is wrong?

PS: I use struts2-convention-plugin


Solution

  • This is happening because the conventions plugin uses the "convention-default" package which does not know about your interceptor or interceptor stack.

    If you could create a package which extends conventions-default doing just what your interceptor stack does (defines the interceptor, interceptor stack and defines the default result type) and then make the conventions plugin apply that stack as the default for all actions wouldn't that be great? You can.

    After the <struts> tag define a constant like so:

    <constant name="struts.convention.default.parent.package" value="loginInterceptor"/>
    

    The other suggestion, was to use struts.xml to override the conventions, which is fine but a little verbose.

    Another option is to use the parent package annotation, which is good when you need to override the default behavior but as you pointed out you want this to be the default behavior so you should reserve annotations for overriding this, not to implement it.

    It may be worth browsing the following list of struts2 conventions plugin constants for the future: http://struts.apache.org/2.1.8/docs/convention-plugin.html#ConventionPlugin-Overwritingpluginclasses

    PS: Just naming preference but I would call your package login-package rather than loginInterceptor... not a big deal.