Spring mvc web.xml错误页面位置重定向未通过我的筛选器定义

Spring mvc web.xml错误页面位置重定向未通过我的筛选器定义,spring-mvc,error-handling,web.xml,Spring Mvc,Error Handling,Web.xml,在我的web.xml中,我已经完成了以下错误页面映射,但是当它们被调用时,这些被调用的请求不会通过web.xml文件中指定的过滤器定义 <error-page> <error-code>403</error-code> <location>/error.vm?id=403</location> </error-page> <error-page> <error-code>40

在我的
web.xml
中,我已经完成了以下
错误页面
映射,但是当它们被调用时,这些被调用的请求不会通过
web.xml
文件中指定的过滤器定义

<error-page>
    <error-code>403</error-code>
    <location>/error.vm?id=403</location>
</error-page>

<error-page>
    <error-code>400</error-code>
    <location>/error.vm?id=400</location>
</error-page>

<error-page>
    <error-code>404</error-code>
    <location>/error.vm?id=404</location>
</error-page>

<error-page>
    <exception-type>javax.servlet.ServletException</exception-type>
    <location>/servlet-exception.vm</location>
</error-page>

403
/错误。vm?id=403
400
/错误。vm?id=400
404
/错误。vm?id=404
javax.servlet.ServletException
/servlet-exception.vm
我的应用程序正在使用SpringMVC,我想处理SpringMVC中的
未找到处理程序
情况。我的应用程序是一个多租户应用程序,其中一些过滤器负责设置与架构相关的一些信息

请求在我的
error.vm
控制器中到达,但由于它们正在通过过滤器,我无法确定
主题
安全上下文

如何解决这个问题


谢谢。

您可以使用servlet过滤器,而不是使用web.xml的错误页面。servlet过滤器可用于捕获所有异常,或仅捕获特定异常,如org.springframework.web.portlet.NoHandlerFoundException。(这就是您所说的“未找到处理程序”异常的意思吗?)

过滤器的外观如下所示:

package com.example;

import java.io.IOException;
import javax.servlet.*;
import javax.servlet.http.*;
import org.springframework.web.portlet.NoHandlerFoundException;

public class ErrorHandlingFilter implements Filter {

    public void init(FilterConfig config) throws ServletException { }

    public void destroy() { }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;

        try {
            chain.doFilter(request, response);
        } catch (NoHandlerFoundException e) {
            // Or you could catch Exception, Error, Throwable...

            // You probably want to add exception logging code here.

            // Putting the exception into request scope so it can be used by the error handling page    
            request.setAttribute("exception", e);

            // You probably want to add exception logging code here.
            request.getRequestDispatcher("/WEB-INF/view/servlet-exception.vm").forward(request, response);
        }
    }
}
   try {
        chain.doFilter(request, response);
    } catch (NoHandlerFoundException e) {
            request.getSession().setAttribute("exception", e);
            response.sendRedirect("/servlet-exception.vm");
    }
然后,在Spring的DelegatingFilterProxy的帮助下,在web.xml中进行设置:

<filter>
    <filter-name>errorHandlingFilter</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
    <filter-name>errorHandlingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
这将迫使浏览器以新的http请求的形式请求您的错误处理页面,这样可以更容易地确保它首先通过所有正确的过滤器。如果您需要原始的异常对象,那么您可以将其放在会话中而不是请求中。

<filter-mapping>
    <filter-name>SomeFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>ERROR</dispatcher>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>

某些过滤器
/*
错误
要求
<filter-mapping>
    <filter-name>SomeFilter</filter-name>
    <url-pattern>/*</url-pattern>
    <dispatcher>ERROR</dispatcher>
    <dispatcher>REQUEST</dispatcher>
</filter-mapping>