13

web.xml fragment

<!-- 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>

It works fine but I'd like to NOT let the Dispatcher Servlet handle *.html requests. How do I I do that? Thanks.

Bobo
  • 8,777
  • 18
  • 66
  • 85

5 Answers5

24

In Spring MVC 3.x there is the default servlet handler to take care of this problem.

just add this to the Spring XML config:

<mvc:default-servlet-handler/>
Lari Hotari
  • 5,190
  • 1
  • 36
  • 43
5

Map it on a more specific url-pattern.

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

Create a Filter which is mapped on /*.

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

Which does the following in doFilter() method.

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.
}
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This almost works for me... using getRequestURI will return a path including the context-path. Assuming your context is /ctx, a request to /ctx/foo will be dispatched to /ctx/spring/ctx/foo I think getServletPath() + getPathInfo() (if pathInfo != null) works better – VoidPointer Jan 20 '11 at 13:38
  • This solve my problem. Thanks BalusC, You're awesome – Arif Nazar Purwandaru Sep 12 '15 at 04:34
2
<url-pattern>/*</url-pattern> 

Can catch like /index , /*.html , /*.jsp ...,and give to DispatcherServlet.

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

Only Can catch /index,/main ... , without suffix.

Andrei
  • 42,814
  • 35
  • 154
  • 218
zll
  • 21
  • 3
2

Lari's solution (above) is great, and worked for me, but you have to be very careful with the order in which you write the instruction, it has to be in the begining of the document!!!

In my case was something like:

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

<context:annotation-config />
<context:component-scan base-package="org.civitana.controller" />
camposer
  • 5,152
  • 2
  • 17
  • 15
0

Try adding this to your Spring XML config:

<!-- 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>

This will prevent Spring from automatically mapping requests with .html to your controller. E.g. @RequestMapping(value = "/products/widgets", method = RequestMethod.GET) would normally trap a URI of /products/widgets.html as well as /products/widgets. Adding the above XML forces exact matching of URI patterns by Spring (only the latter will match).

nickdos
  • 8,348
  • 5
  • 30
  • 47