struts2:Infinite recursion detected

本文分析了一个Struts2项目中由于自定义拦截器的不当配置导致的循环递归问题。通过示例代码展示了如何配置拦截器栈以及自定义拦截器,并解释了循环调用的原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这个问题是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)。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值