【问题标题】:spring mvc how to bypass DispatcherServlet for *.html files?spring mvc 如何绕过 *.html 文件的 DispatcherServlet?
【发布时间】:2011-05-24 21:13:47
【问题描述】:

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

【问题讨论】:

标签: servlets spring-mvc web.xml


【解决方案1】:

将其映射到更具体的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.
}

【讨论】:

  • 这几乎对我有用...使用 getRequestURI 将返回一个包含上下文路径的路径。假设您的上下文是 /ctx,对 /ctx/foo 的请求将被分派到 /ctx/spring/ctx/foo 我认为 getServletPath() + getPathInfo() (如果 pathInfo != null) 效果更好
  • 这解决了我的问题。谢谢 BalusC,你太棒了
【解决方案2】:

尝试将其添加到您的 Spring XML 配置中:

<!-- This will override the default DefaultAnnotationHandlerMapping that is created,
  -  and not map file extensions automagically -->
<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="useDefaultSuffixPattern" value="false"/>
</bean>

这将阻止 Spring 自动将带有 .html 的请求映射到您的控制器。例如。 @RequestMapping(value = "/products/widgets", method = RequestMethod.GET) 通常会捕获/products/widgets.html/products/widgets 的URI。添加上述 XML 会强制 Spring 精确匹配 URI 模式(只有后者会匹配)。

【讨论】:

    【解决方案3】:

    在 Spring MVC 3.x 中有 default servlet handler 来解决这个问题。

    只需将其添加到 Spring XML 配置中:

    <mvc:default-servlet-handler/>
    

    【讨论】:

    • 太棒了。我记得这个配置,但我不记得确切的代码。谢谢。
    • 你拯救了我的一天。谢谢
    【解决方案4】:
    <url-pattern>/*</url-pattern> 
    

    可以像/index , /*.html , /*.jsp ... 一样捕捉,并给DispatcherServlet。

    <url-pattern>/</url-pattern> 
    

    只能捕获/index,/main ... ,不带后缀。

    【讨论】:

    • 哇,最简单的。
    【解决方案5】:

    Lari 的解决方案(上图)很棒,对我有用,但是你必须非常小心你写指令的顺序,它必须在文档的开头!!!

    在我的情况下是这样的:

    <mvc:annotation-driven />
    <mvc:default-servlet-handler/>
    
    <context:annotation-config />
    <context:component-scan base-package="org.civitana.controller" />
    

    【讨论】:

      猜你喜欢
      • 2013-05-18
      • 2018-10-12
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-09
      • 2018-09-07
      相关资源
      最近更新 更多