Bobo
Bobo

Reputation: 9163

spring mvc how to bypass DispatcherServlet for *.html files?

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.

Upvotes: 13

Views: 22398

Answers (5)

camposer
camposer

Reputation: 5612

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" />

Upvotes: 2

zll
zll

Reputation: 21

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

Upvotes: 2

Lari Hotari
Lari Hotari

Reputation: 5310

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

Upvotes: 24

nickdos
nickdos

Reputation: 8414

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).

Upvotes: 0

BalusC
BalusC

Reputation: 1108722

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.
}

Upvotes: 5

Related Questions