Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Servlets SpringMVC如何绕过DispatcherServlet处理*.html文件?_Servlets_Spring Mvc_Web.xml - Fatal编程技术网

Servlets SpringMVC如何绕过DispatcherServlet处理*.html文件?

Servlets SpringMVC如何绕过DispatcherServlet处理*.html文件?,servlets,spring-mvc,web.xml,Servlets,Spring Mvc,Web.xml,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

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>

SpringMVC调度程序Servlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/spring/app-config.xml
1.
SpringMVC调度程序Servlet
/*

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

将其映射到更具体的
url模式上

<servlet-mapping>
    <servlet-name>Spring MVC Dispatcher Servlet</servlet-name>
    <url-pattern>/spring/*</url-pattern>
</servlet-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.
}

尝试将其添加到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>
<mvc:default-servlet-handler/>


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

在SpringMVC3.x中,需要解决这个问题

只需将其添加到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>
<mvc:default-servlet-handler/>

只能捕获
/index
/main
,没有后缀。

拉里的解决方案(上面)很好,对我很有用,但你必须非常小心编写说明的顺序,它必须在文档的开头

在我的例子中是这样的:

<mvc:annotation-driven />
<mvc:default-servlet-handler/>

<context:annotation-config />
<context:component-scan base-package="org.civitana.controller" />


这可能会有所帮助:@AHungerArtist感谢您的链接。我在那里找到了更好的答案。这对我来说几乎很有效。。。使用getRequestURI将返回一个包含上下文路径的路径。假设您的上下文是/ctx,对/ctx/foo的请求将被发送到/ctx/spring/ctx/foo。我认为getServletPath()+getPathInfo()(如果pathInfo!=null)可以更好地解决我的问题。谢谢巴卢斯,你真是太棒了。我记得这个配置,但我不记得确切的代码。谢谢你,你救了我一天。谢谢哇,最简单的。
<mvc:annotation-driven />
<mvc:default-servlet-handler/>

<context:annotation-config />
<context:component-scan base-package="org.civitana.controller" />