简体   繁体   English

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

[英]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 ). 一个是“肯定的”(检查session包含currentId ),另一个是“非肯定的”(检查session是否不包含currentId )。

struts.xml : 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 : 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 . 当我在changepassword情况下调用changepassword操作时(即, session不包含currentId ), NotContainsKeyInterceptorprintln()未执行(这使我假设它没有被调用),但是,它确实实现了重定向到index.jsp
  2. The validate() method of ChangePasswordAction class is also being called. 还调用ChangePasswordAction类的validate()方法。

Why is this so? 为什么会这样呢?

(Please do tell me if you need to see the ChangePasswordAction class, I will add it to the post.) (如果您需要查看ChangePasswordAction类,请告诉我,我将其添加到帖子中。)

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 我的意思是先指定您的拦截器,然后再默认堆栈

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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