title: Spring MVC with Java based config - 404 not found for static resources
body: <p>I'm trying out Spring MVC with java config and I'm getting 404 not found for static resources, the folder of which I can confirm is definitively getting published to the server. It seems like the resources are not being resolved at all. So even when calling localhost:8080/SpringMVC/resources/js/jquery-1.4.2.min.js I get 404 not found. I'm using Spring 4.0.1</p>
  
  <p>Deployed structure</p>
  
  <pre><code>SpringMVC/
     resources/
        css/
        js/
           jquery-1.4.2.min.js  
     WEB-INF/
        classes/
        lib/
        views/ 
           services.jsp
        web.xml
  </code></pre>
  
  <p>My Code:</p>
  
  <p>com/me/config/WebApplicationConfig.java</p>
  
  <pre><code>@Configuration
  @EnableWebMvc
  @ComponentScan(basePackages = {"com.me.services"})
  public class WebApplicationConfig extends WebMvcConfigurerAdapter {
  
      private static final String VIEW_RESOLVER_SUFFIX = ".jsp";
      private static final String VIEW_RESOLVER_PREFIX = "/WEB-INF/views/";
  
      @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
      }
  
      @Bean
      public ViewResolver setupViewResolver() {
          InternalResourceViewResolver resolver = new InternalResourceViewResolver();
  
          resolver.setPrefix(VIEW_RESOLVER_PREFIX);
          resolver.setSuffix(VIEW_RESOLVER_SUFFIX );
          resolver.setViewClass(JstlView.class);
  
          return resolver;
      }
  }
  </code></pre>
  
  <p>com/me/config/Initializer.java</p>
  
  <pre><code>public class Initializer implements WebApplicationInitializer {
  
      private static final String DISPATCHER_SERVLET_MAPPING = "/";
      private static final String DISPATCHER_SERVLET_NAME = "dispatcher";
  
      public void onStartup(ServletContext servletContext) throws ServletException {
  
          AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
          rootContext.register(WebApplicationConfig.class);
          rootContext.setServletContext(servletContext);
  
          Dynamic servlet = servletContext.addServlet(DISPATCHER_SERVLET_NAME, new DispatcherServlet(rootContext));
          servlet.addMapping(DISPATCHER_SERVLET_MAPPING );
          servlet.setLoadOnStartup(1);
      }
  }
  </code></pre>
  
  <p>com/me/controller/ServicesController.java</p>
  
  <pre><code>@Controller
  public class ServicesController {
  
      @RequestMapping(value = "/", method = RequestMethod.GET)
      public String services(ModelMap model) {
          return "services";
      }
  }
  </code></pre>
  
  <p>WEB-INF/views/services.jsp</p>
  
  <pre><code>&lt;%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt;
  &lt;html&gt;
  &lt;head&gt;    
  &lt;script src="&lt;c:url value="/resources/js/jquery-1.4.2.min.js" /&gt;"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h2&gt;Services&lt;/h2&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>GET Request Response when calling localhost:8080/SpringMVC/:</p>
  
  <pre><code>&lt;html&gt;
  &lt;head&gt;
  &lt;script src="/SpringMVC/resources/js/jquery-1.4.2.min.js"&gt;&lt;/script&gt;
  &lt;/head&gt;
  &lt;body&gt;
      &lt;h2&gt;Services&lt;/h2&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And then after that 404 Not Found on:</p>
  
  <pre><code>localhost:8080/SpringMVC/resources/js/jquery-1.4.2.min.js
  </code></pre>