Spring MVC如何为* .html文件绕过DispatcherServlet?

问题描述

将其映射到更具体的位置url-pattern

<servlet-mapping>
    <servlet-name>Spring MVC dispatcher Servlet</servlet-name>
    <url-pattern>/spring/*</url-pattern>
</servlet-mapping>

创建一个Filter映射到的/*

<filter-mapping>
    <filter-name>Your dispatcher Filter</filter-name>
    <url-pattern>/*</url-pattern>
<filter-mapping>

doFilter()方法中执行以下操作。

String uri = ((HttpServletRequest) request).getRequestURI();
if (uri.endsWith(".html")) {
    chain.doFilter(request, response); // Just let it go (assuming that files are in real not placed in a /spring folder!)
} else {
    request.getRequestdispatcher("/spring" + uri).forward(request, response); // Pass to Spring dispatcher servlet.
}

解决方法

web.xml片段

<!-- Handles all requests into the application -->
<servlet>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/app-config.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>

它工作正常,但我不想让Dispatcher Servlet处理* .html请求。我该怎么做?谢谢。