当前位置: 代码迷 >> 综合 >> struts2:Infinite recursion detected
  详细解决方案

struts2:Infinite recursion detected

热度:34   发布时间:2023-11-22 19:01:53.0

这个问题是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++);//返回一个全局结果,该全局结果对应于处理登录表单的actionreturn "loginForm";}
}

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

  相关解决方案