拦截器没有被调用但是仍然执行?

时间:2015-01-02 08:38:33

标签: java jsp struts2 action interceptor

我在单独的包中定义了两个拦截器。

一个是肯定的" (检查session是否包含currentId)和另一个"非肯定" (检查session是否包含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. 当我在没有登录的情况下调用changepassword动作时(即 session不包含currentId),println() NotContainsKeyInterceptor没有执行(这让我假设 它没有被调用),但是,它做了预期的行为 重定向到index.jsp
  2. validate()类的ChangePasswordAction方法也是 被召唤。
  3. 为什么会这样?

    (如果您需要查看ChangePasswordAction课程,请告诉我,我会将其添加到帖子中。)

1 个答案:

答案 0 :(得分:0)

实际上它没有被调用..你可以用这种方式改变你的配置并尝试

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

我的意思是先指定你的拦截器以后的defaultstack