简体   繁体   中英

Interceptor not being called but executes anyway?

I have two Interceptors defined in separate packages.

One is "affirmative" (checks if session contains currentId ) and the other "non-affirmative" (checks if session does not contain currentId ).

struts.xml :

<!-- actions available to guests -->
<package name="guest" extends="struts-default">
    <interceptors>
        <interceptor name="containskey" class="com.mypackage.interceptor.ContainsKeyInterceptor" />
    </interceptors>

    <action name="index" class="com.mypackage.action.IndexAction">
        <interceptor-ref name="defaultStack" />
        <interceptor-ref name="containskey" />
        <result type="redirect">/index.jsp</result>
        <result name="index" type="redirect">/index.jsp</result>
    </action>

    <action name="login" class="com.mypackage.action.LoginAction">
        <interceptor-ref name="defaultStack" />
        <interceptor-ref name="containskey" />
        <result type="redirect">/index.jsp</result>
        <result name="input">/login.jsp</result>
        <result name="index" type="redirect">/index.jsp</result>
    </action>
</package>

<!-- actions available to members -->
<package name="member" extends="struts-default">
    <interceptors>
        <interceptor name="notcontainskey" class="com.mypackage.interceptor.NotContainsKeyInterceptor" />
    </interceptors>

    <action name="changepassword" class="com.mypackage.action.ChangePasswordAction">
        <interceptor-ref name="defaultStack" />
        <interceptor-ref name="notcontainskey" />
        <result type="redirect">/usercp.jsp</result>
        <result name="input">/usercp-change-password.jsp</result>
        <result name="index" type="redirect">/index.jsp</result>
    </action>

    <action name="logout" class="com.mypackage.action.LogoutAction">
        <result type="redirectAction">
            <param name="actionName">index</param>
        </result>
    </action>
</package>

NotContainsKeyInterceptor :

public class NotContainsKeyInterceptor extends AbstractInterceptor {
    private static final long serialVersionUID = 1L;

    @Override
    public String intercept(ActionInvocation actionInvocation) throws Exception {
        System.out.println("NotContainsKeyInterceptor");

        final ActionContext actionContext = actionInvocation.getInvocationContext();
        Map<String, Object> session = actionContext.getSession();

        if(!session.containsKey("currentId")) {
            return "index";
        }

        String result = actionInvocation.invoke();

        return result;
    }
}

http://localhost:8080/mysite/changepassword

  1. When I call the changepassword action without logging in (ie session does not contain currentId ), the println() of the NotContainsKeyInterceptor is not executing (which makes me assume it is not being called), yet, it does the expected behavior of redirecting to index.jsp .
  2. The validate() method of ChangePasswordAction class is also being called.

Why is this so?

(Please do tell me if you need to see the ChangePasswordAction class, I will add it to the post.)

Actually its not being called.. Can you change your configuration this way and try

    <interceptor-ref name="notcontainskey" /> 
    <interceptor-ref name="defaultStack" /> 

I mean specify your interceptor first later defaultstack

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