title: Spring MVC not loading static resources from webapp
body: <p><strong>Issue</strong>
  Unable to get Spring to add a resource handler for static resources.</p>
  
  <p><strong>Background Information</strong>
  I have a Spring MVC webapp running in a standalone Jetty instance. My webapp structure is</p>
  
  <pre><code>webapp root/
      resources/
          css/
          images/
          js/
      WEB-INF/
          layouts/
              tiles.xml
              page.jsp
          lib/
          views/
              home/
                  home.jsp
                  tiles.xml
          web.xml
  </code></pre>
  
  <p>I've extended the <code>WebMvcConfigurerAdaptor</code> to add a resource mapping such that urls like <code>company.com/myservlet/resources/css/main.css</code> will get resolved to the <code>webapp root/resources/css</code> folder.</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  public class WebMvcConfig extends WebMvcConfigurerAdapter {
  
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
      }
  }
  </code></pre>
  
  <p>My little controller is this:</p>
  
  <pre><code>@Controller
  public class SpringAppController {       
  
      public SpringAppController() {
      }
  
      @RequestMapping(value = "/home", method = RequestMethod.GET)
      public ModelAndView handleRequestHome(HttpServletRequest request, HttpServletResponse response)
          throws ServletException, IOException {
  
          return new ModelAndView("home");
      }
  }
  </code></pre>
  
  <p>My web.xml is simply this:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"&gt;
  
      &lt;!-- Java-based Spring container definition --&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextClass&lt;/param-name&gt;
          &lt;param-value&gt;org.springframework.web.context.support.AnnotationConfigWebApplicationContext&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Location of Java @Configuration classes that configure the components that makeup this application --&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;com.company&lt;/param-value&gt;
      &lt;/context-param&gt;
  
      &lt;!-- Creates the Spring Container shared by all Servlets and Filters --&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  
      &lt;!-- Processes application requests --&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;admin&lt;/servlet-name&gt;
          &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
              &lt;param-value&gt;&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;admin&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
      &lt;!-- Ensure UTF-8 encoded pages so that certain characters are displayed and submitted correctly --&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.web.filter.CharacterEncodingFilter&lt;/filter-class&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;encoding&lt;/param-name&gt;
              &lt;param-value&gt;UTF-8&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;init-param&gt;
              &lt;param-name&gt;forceEncoding&lt;/param-name&gt;
              &lt;param-value&gt;true&lt;/param-value&gt;
          &lt;/init-param&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;encodingFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  
      &lt;!-- Enables support for DELETE and PUT request methods with web browser clients --&gt;
      &lt;filter&gt;
          &lt;filter-name&gt;hiddenHttpMethodFilter&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.web.filter.HiddenHttpMethodFilter&lt;/filter-class&gt;
      &lt;/filter&gt;
  
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;hiddenHttpMethodFilter&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>My JavaConfig for the webapp to instantiate some mvc resolvers is this:</p>
  
  <pre><code>@Configuration
  public class MainConfig {
  
      @Bean
      public ViewResolver viewResolver() {
          UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();
          viewResolver.setViewClass(TilesView.class);
          return viewResolver;
      }
  
      @Bean
      public TilesConfigurer tilesConfigurer() {
          TilesConfigurer configurer = new TilesConfigurer();
          configurer.setDefinitions(new String[] {
              "/WEB-INF/layouts/tiles.xml",
              "/WEB-INF/views/**/tiles.xml"
          });
          configurer.setCheckRefresh(true);
          return configurer;
      }
  
      @Bean
      public org.springframework.context.MessageSource messageSource() {
          ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
          messageSource.setBasename("/WEB-INF/messages/messages");
          return messageSource;
      }
  
  }
  </code></pre>
  
  <p>When I hit the url company.com/myservlet/home the tiles stuff works and the home.jsp page is displayed but the css and images are not loaded. The servlet logs this warning:</p>
  
  <pre><code>[DEBUG] [DispatcherServlet] [DispatcherServlet with name 'admin' processing GET request for [/api/v1/tlb/resources/page.css]] 
  [DEBUG] [RequestMappingHandlerMapping] [Looking up handler method for path /resources/form.css] 
  [DEBUG] [RequestMappingHandlerMapping] [Did not find handler method for [/resources/form.css]]
  [WARN ] [PageNotFound] [No mapping found for HTTP request with URI [/api/v1/tlb/resources/form.css] in DispatcherServlet with name 'admin']
  </code></pre>
  
  <p>I've tried debugging the servlet and the <code>WebMvcConfig.addResourceHandlers()</code> method is never called and hence why the static resources cannot be found. What am I missing to get this working?</p>
  
  <p>As a note if I add the following to my web.xml the css resource will be loaded but I would like to know why the <code>WebMvcConfig.addResourceHandlers()</code> method is not called so I don't need this servlet mapping.</p>
  
  <pre><code>&lt;servlet-mapping&gt;
      &lt;servlet-name&gt;default&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/resources/*&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  </code></pre>