title: Where to put default-servlet-handler in Spring MVC configuration
body: <p>In my <code>web.xml</code>, the default servlet mapping, i.e. <code>/</code>, is mapped to Spring dispatcher. In my Spring dispatcher configuration, I have <code>DefaultAnnotationHandlerMapping</code>, <code>ControllerClassNameHandlerMapping</code> and <code>AnnotationMethodHandlerAdapter</code> which allows me to map url to controllers either by its class name or its <code>@Requestmapping</code> annotation. However, there are some static resources under the web root which I also want spring dispatcher to serve using default servlet. According to <a href="http://static.springsource.org/spring/docs/3.0.5.RELEASE/spring-framework-reference/htmlsingle/spring-framework-reference.html#mvc-default-servlet-handler" rel="noreferrer">Spring documentation</a>, this can be done using <code>&lt;mvc:default-servlet-handler/&gt;</code> tag.</p>
  
  <p>In the configuration below, there are 4 candidate locations that I marked which are possible to insert this tag. Inserting the tag in different location causes the dispatcher to behave differently as following :</p>
  
  <p><strong>Case 1</strong> : If I insert it at location 1, the dispatcher will no longer be able to handle mapping by the @RequestMapping and controller class name but it will be serving the static content normally.</p>
  
  <p><strong>Cas 2, 3</strong> : It will be able to handle mapping by the @RequestMapping and controller class name as well as serving the static content if other mapping cannot be done successfully.</p>
  
  <p><del><strong>Case 4</strong> : It will not be able to serve the static contents.</del> <em>Removal Note : This was a bug when implementing a controller which has a method mapped to <code>/**</code> but does not have explicit request mapping on the controller class name.</em></p>
  
  <p>Therefore, <strong>Case 2</strong> and <strong>3</strong> are desirable .According to Spring documentation, this tag configures a handler which precedence order is given to lowest so why the position matters? and Which is the best position to put this tag?</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"&gt;
  
      &lt;context:annotation-config/&gt;
      &lt;context:component-scan base-package="webapp.controller"/&gt;
      &lt;!-- Location 1 --&gt;
  
      &lt;!-- Enable annotation-based controllers using @Controller annotations --&gt;
      &lt;bean id="annotationUrlMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"/&gt;
  
      &lt;!-- Location 2 --&gt;
      &lt;bean id="controllerClassNameHandlerMapping" class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/&gt;
  
      &lt;!-- Location 3 --&gt;
      &lt;bean id="annotationMethodHandlerAdapter" class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"/&gt;
  
      &lt;!-- Location 4 --&gt;
      &lt;mvc:default-servlet-handler/&gt;
  
      &lt;!-- All views are JSPs loaded from /WEB-INF/jsp --&gt;
      &lt;bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/&gt;
          &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt;
          &lt;property name="suffix" value=".jsp"/&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>