title: Spring mvc static resource configuration issues
body: <p>I am working with spring mvc 3 on tomcat 7.  I have been having issues getting my static resources to load. </p>
  
  <p>My directory structure is as follows:</p>
  
  <pre><code>-static
   -common
    - paths to static
  - WEB-INF
  </code></pre>
  
  <p>I have already made sure that the statics were not getting caught up in a request mapping I have set up.  However, whenever I go to retrieve them I just get a 404 as follows:</p>
  
  <pre><code>GET http://localhost:8080/YouthMinistryHibernate/static/common/css/compiled.css 404 (Not Found)
  GET http://localhost:8080/YouthMinistryHibernate/static/common/js/compiled.js 404 (Not Found) 
  </code></pre>
  
  <p>Here is my web.xml:</p>
  
  <pre><code>&lt;web-app id="WebApp_ID" version="2.4"
      xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
          http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"&gt;
  
      &lt;display-name&gt;YouthMinistryHibernate&lt;/display-name&gt;
  
      &lt;!-- Spring MVC --&gt; 
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;/WEB-INF/classes/spring/root-context.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;spring.profiles.default&lt;/param-name&gt;
          &lt;param-value&gt;simple&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;deepDishCMSServlet&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;init-param&gt;
              &lt;param-name&gt;spring.profiles.default&lt;/param-name&gt;
              &lt;param-value&gt;simple&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;deepDishCMSServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&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;filter&gt;
          &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
          &lt;filter-class&gt;org.springframework.web.filter.DelegatingFilterProxy&lt;/filter-class&gt;
      &lt;/filter&gt;
      &lt;filter-mapping&gt;
          &lt;filter-name&gt;springSecurityFilterChain&lt;/filter-name&gt;
          &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
      &lt;/filter-mapping&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>and my spring-mvc config file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
          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"&gt;
  
      &lt;mvc:resources mapping="/static/**" location="/static/" /&gt;
  
      &lt;mvc:annotation-driven/&gt;
  
      &lt;bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /&gt;
  
      &lt;bean id="messageSource"
          class="org.springframework.context.support.ResourceBundleMessageSource"&gt;
          &lt;property name="basename" value="message" /&gt;
      &lt;/bean&gt;
  
      &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/jsp/&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="suffix"&gt;
              &lt;value&gt;.jsp&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="order"&gt;
              &lt;value&gt;1&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;mvc:annotation-driven&gt;
          &lt;mvc:message-converters&gt;
              &lt;bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"&gt;
                  &lt;property name="supportedMediaTypes"&gt;
                      &lt;list&gt;
                          &lt;value&gt;image/jpeg&lt;/value&gt;
                          &lt;value&gt;image/png&lt;/value&gt;
                      &lt;/list&gt;
                  &lt;/property&gt;
              &lt;/bean&gt;
          &lt;/mvc:message-converters&gt;
      &lt;/mvc:annotation-driven&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>I have tried retrieving the resources using two methods:</p>
  
  <pre><code>&lt;link rel="stylesheet" type="text/css" href="${ pageContext.request.contextPath }/static/common/css/compiled.css" /&gt;
  </code></pre>
  
  <p>and </p>
  
  <pre><code>&lt;script type="text/javascript" src="&lt;c:url value="/static/common/js/compiled.js" /&gt;"&gt;&lt;/script&gt;
  </code></pre>
  
  <p>Here is the link to the project on github as well just in case:
  <a href="http://github.com/dmcquillan314/YouthMinistryHibernate" rel="nofollow">http://github.com/dmcquillan314/YouthMinistryHibernate</a></p>
  
  <p>Any help is greatly appreciated.</p>
  
  <p>Below is the relevant configuration in my pom.xml:</p>
  
  <pre><code>    &lt;plugin&gt;
          &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
          &lt;artifactId&gt;exec-maven-plugin&lt;/artifactId&gt;
          &lt;version&gt;1.1&lt;/version&gt;
          &lt;executions&gt;
              &lt;execution&gt;
                  &lt;phase&gt;prepare-package&lt;/phase&gt;
                  &lt;goals&gt;
                      &lt;goal&gt;exec&lt;/goal&gt;
                  &lt;/goals&gt;
              &lt;/execution&gt;
          &lt;/executions&gt;
          &lt;configuration&gt;
              &lt;executable&gt;${project.basedir}/src/main/resources/build.sh&lt;/executable&gt;
              &lt;arguments&gt;
                  &lt;argument&gt;${env}&lt;/argument&gt;
              &lt;/arguments&gt;
          &lt;/configuration&gt;
      &lt;/plugin&gt;
      &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.1&lt;/version&gt;
          &lt;executions&gt;
              &lt;execution&gt;
                  &lt;phase&gt;package&lt;/phase&gt;
              &lt;/execution&gt;
          &lt;/executions&gt;
          &lt;configuration&gt;
              &lt;warSourceExcludes&gt;static/common/js/**&lt;/warSourceExcludes&gt;
          &lt;/configuration&gt;
      &lt;/plugin&gt;
      &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.3.2&lt;/version&gt;
          &lt;configuration&gt;
              &lt;source&gt;${java-version}&lt;/source&gt;
              &lt;target&gt;${java-version}&lt;/target&gt;
          &lt;/configuration&gt;
      &lt;/plugin&gt;
      &lt;plugin&gt;
          &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;
          &lt;artifactId&gt;maven-war-plugin&lt;/artifactId&gt;
          &lt;version&gt;2.1.1&lt;/version&gt;
          &lt;configuration&gt;
              &lt;warName&gt;YouthMinistryHibernate&lt;/warName&gt;
          &lt;/configuration&gt;
      &lt;/plugin&gt;
      &lt;plugin&gt;
          &lt;groupId&gt;org.codehaus.mojo&lt;/groupId&gt;
          &lt;artifactId&gt;tomcat-maven-plugin&lt;/artifactId&gt;
          &lt;configuration&gt;
              &lt;url&gt;&lt;/url&gt;
              &lt;server&gt;TomcatServer&lt;/server&gt;
              &lt;path&gt;/YouthMinistryHibernate&lt;/path&gt;
          &lt;/configuration&gt;
          &lt;version&gt;1.1&lt;/version&gt;
      &lt;/plugin&gt;
  </code></pre>
  
  <p>Debug logs when I try to access this resource:</p>
  
  <pre><code>DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/static/common/js/compiled.js]
  DEBUG: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Matching patterns for request [/static/common/js/compiled.js] are [/static/**]
  DEBUG: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - URI Template variables for request [/static/common/js/compiled.js] are {}
  DEBUG: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/static/common/js/compiled.js] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@569939c1] and 1 interceptor
  DEBUG: org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/YouthMinistryHibernate/static/common/js/compiled.js] is: -1
  DEBUG: org.springframework.web.servlet.resource.ResourceHttpRequestHandler - Trying relative path [common/js/compiled.js] against base location: ServletContext resource [/static/]
  DEBUG: org.springframework.web.servlet.resource.ResourceHttpRequestHandler - No matching resource found - returning 404
  DEBUG: org.springframework.web.servlet.DispatcherServlet - Null ModelAndView returned to DispatcherServlet with name 'deepDishCMSServlet': assuming HandlerAdapter completed request handling
  DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request
  </code></pre>
  
  <p><strong>UPDATE</strong></p>
  
  <p>Still have not fixed this issue however, I've had the chance to look through all of my codebase and found nothing helpful so far.  At the moment I'm looking around to see where my code may be getting stuck. </p>
  
  <p>I'm thinking the issue may lie somewhere in the spring mvc config as it doesn't seem to be using the mvc:resources when loading in the static file request mappings.  My mvc config is below:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns:context="http://www.springframework.org/schema/context"
         xmlns:tx="http://www.springframework.org/schema/tx"
         xmlns:mvc="http://www.springframework.org/schema/mvc"
  
         xsi:schemaLocation="http://www.springframework.org/schema/beans
             http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
             http://www.springframework.org/schema/context
             http://www.springframework.org/schema/context/spring-context-3.1.xsd
             http://www.springframework.org/schema/tx
             http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
             http://www.springframework.org/schema/mvc
             http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd"&gt;
  
      &lt;!-- Configure the multipart resolver --&gt;
      &lt;bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"&gt;
          &lt;!-- one of the properties available; the maximum file size in bytes --&gt;
          &lt;property name="maxUploadSize" value="100000"/&gt;
      &lt;/bean&gt;
  
      &lt;mvc:resources mapping="/static/**" location="/static/" /&gt;
  
      &lt;!-- Add controller package to be scanned for this path --&gt;
      &lt;context:component-scan base-package="com.youthministry.controller"/&gt;
  
      &lt;mvc:annotation-driven/&gt;
  
      &lt;bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" /&gt;
  
      &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/jsp/&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="suffix"&gt;
              &lt;value&gt;.jsp&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="order"&gt;
              &lt;value&gt;1&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  
      &lt;mvc:annotation-driven&gt;
          &lt;mvc:message-converters&gt;
              &lt;bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter"&gt;
                  &lt;property name="supportedMediaTypes"&gt;
                      &lt;list&gt;
                          &lt;value&gt;image/jpeg&lt;/value&gt;
                          &lt;value&gt;image/png&lt;/value&gt;
                      &lt;/list&gt;
                  &lt;/property&gt;
              &lt;/bean&gt;
          &lt;/mvc:message-converters&gt;
      &lt;/mvc:annotation-driven&gt;
  
  &lt;/beans&gt;
  </code></pre>