【问题标题】:Spring 3.0 unable to forward request from HandlerInterceptorAdapterSpring 3.0 无法转发来自 HandlerInterceptorAdapter 的请求
【发布时间】:2023-04-04 10:19:01
【问题描述】:

如果会话无效,我想重定向到主页。 我的 spring-servlet.xml 是

<mvc:interceptors>
    <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
    <mvc:interceptor>
        <mvc:mapping path="/**" />
        <bean class="com.xxx.MyInterceptor" />
    </mvc:interceptor>
</mvc:interceptors>

拦截器:

public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        if ((null == request.getSession(false))
                || (null == request.getSession(false).getAttribute(
                        "user"))) {
            System.out.println("user logged out...");
            RequestDispatcher rd = request.getRequestDispatcher("loginForm.htm");
            rd.forward(request, response);
            return false;
        }
        return super.preHandle(request, response, handler);
    }

但它不起作用... 每当应用程序启动时,消息会被打印多次,最后它会导致堆栈溢出..

谢谢。

【问题讨论】:

    标签: spring interceptor spring-3


    【解决方案1】:

    问题似乎出在您的映射路径中。由于它与/** 映射,您的 loginForm.htm 也被拦截。您有两种解决方案可以解决此问题。

    要么定义&lt;mvc:resources location="/resources/" mapping="/resources/**" /&gt;,这样*.htm 请求就不会被拦截。根据 *.htm 文件所在的路径替换位置和映射值。

    另一种选择是使用/*.do 或其他内容更改拦截器中的映射。

    希望这对您有所帮助。干杯。

    【讨论】:

    • 感谢回复,但要求是,我拦截每个请求以检查会话是否有效。如果不是,则重定向到主页...所有 url 模式都以 . 结尾。 html
    • 这就是我试图解释的,因为你所有的请求映射都以 *.htm 结尾,你进入了一个无限循环。必须排除这种模式,否则您的问题将持续存在。 :-)
    • 感谢您的指导...通过将 LoginForm.htm 更改为 LoginForm.do 并将拦截器映射从“/**”到“/*.htm”解决了问题..
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 2019-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多