微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

如何解决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请求。我该怎么做?谢谢。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。