struts2:Infinite recursion detected

这个问题是struts2的拦截器循环调用自身的 intercept(ActionInvocation invocation)方法所导致的。

struts.xml文件中部分配置如下:

<!-- 定义自己的拦截器 -->
        <interceptors>
            <interceptor name="managerVerification" class="interceptor.ManagerVerification"/>
            <interceptor-stack name="managerInterceptor">
                <!-- 先引用struts的默认拦截器 -->
                <interceptor-ref name="defaultStack"/>
                <!-- 再引用自定义拦截器 -->
                <interceptor-ref name="managerVerification"/>
            </interceptor-stack>
        </interceptors>
        <!-- 将自已定义的拦截器栈配置为整个包的默认拦截器 -->
        <default-interceptor-ref name="managerInterceptor"/>

        <!-- 配置全局结果 -->
        <global-results>
            <result name="loginForm" type="chain">loginForm1</result>
        </global-results>

        <!-- 登录表单 -->
        <action name="loginForm1">
            <result>/WEB-INF/jsp/manager/login.jsp</result>
        </action>

自定义拦截器实现类如下:

public class ManagerVerification implements Interceptor{

    private static int count = 0;

    public void destroy() {
    }

    public void init() {

    }

    public String intercept(ActionInvocation invocation) throws Exception {
        //模拟处理过程
        System.out.println(count++);
        //返回一个全局结果,该全局结果对应于处理登录表单的action
        return "loginForm";
    }
}

访问一个被自定义拦截器拦截的action后,控制台输出为
0
1
说明该拦截器的intercept(ActionInvocation invocation)方法执行后返回名为”loginForm”的逻辑结果,该结果为一个链式action,映射到另一个名为loginForm1的action,而该action又被拦截器拦截,于是又执行拦截器的intercept(ActionInvocation invocation)方法……因此造成了循环递归(Infinite recursion)。

猜你喜欢

转载自blog.csdn.net/qq_37720278/article/details/77779130
今日推荐