title: Not able to map static resources i.e. JS, CSS, images in spring MVC
body: <p>I'm not sure how these static resources were loading earlier but after I added one new controller mapping I see no mapping found for static resources warning message in logs.</p>
  <p><strong>No mapping for GET /testproject/org/css/style.css</strong></p>
  <p>Let me explain my project configuration first so that it will be easy to understand the problem.</p>
  <p><strong>All JSP location (view have different folders to separate JSP for different modules)</strong></p>
  <pre><code>src/main/java/webapp/views/org/*.jsp  
  src/main/java/webapp/views/*.jsp
  </code></pre>
  <p><strong>Static resource location</strong></p>
  <pre><code>src/main/java/resources/static/css/*.css  
  src/main/java/resources/static/js/*.js  
  src/main/java/resources/static/images/*.jpg  
  src/main/java/resources/static/fonts/*.ttf
  </code></pre>
  <p><strong>Application.properties</strong></p>
  <pre><code>spring.mvc.view.prefix=/WEB-INF/views/
  spring.mvc.view.suffix=.jsp
  server.servlet.contextPath=/testproject
  </code></pre>
  <p>After adding last config in <strong>application.properties</strong> now my application URL for any view page will be like</p>
  <p><strong>http://localhost:8080/testproject/dashboard</strong></p>
  <p>Controller mapping for the above URL is:</p>
  <pre><code>@RequestMapping(value = &quot;/dashboard&quot;, method = RequestMethod.GET)
      public String dashboard(ModelMap map) {return &quot;org/dashboard&quot;;}
  </code></pre>
  <p>With the above configuration, the static resource path is getting formed as below</p>
  <p><strong>http://localhost:8080/testproject/css/style.css</strong></p>
  <p>With the above config, I was able to load CSS files using the below code</p>
  <pre><code>&lt;%@ taglib prefix=&quot;core&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot;%&gt;
  &lt;link href=&quot;&lt;core:url value=&quot;css/style.css&quot;/&gt;&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;all&quot;&gt;
  </code></pre>
  <p>Now as I have dashboard page defined under separate folder <strong>org</strong> so I thought to change my controller mapping to match exactly as folder structure &amp; also it will help me to assign different roles/Access_ROLE for diff modules in spring security.</p>
  <p>Spring security config for defining USER_ROLE who can access <strong>org</strong> module pages</p>
  <p><em>.antMatchers(&quot;/org/**&quot;).access(&quot;hasRole('ROLE_ORG')&quot;).anyRequest()</em></p>
  <p>After changing my controller mapping (from <strong>/dashboard</strong> to <strong>/org/dashboard</strong>)</p>
  <pre><code>@RequestMapping(value = &quot;/org/dashboard&quot;, method = RequestMethod.GET)
      public String dashboard(ModelMap map) {return &quot;org/dashboard&quot;;}
  </code></pre>
  <p>Now spring security works fine. It is not allowing direct access of dashboard without login or if logged-in user is having different ROLE.</p>
  <p>But now my static resource URL is changes now</p>
  <p><strong>http://localhost:8080/testproject/org/css/style.css</strong></p>
  <p>Earlier this URL was</p>
  <p><strong>http://localhost:8080/testproject/css/style.css</strong> (working)</p>
  <p>Things I tried to fix this</p>
  <p><strong>WebMvcConfig.java</strong></p>
  <pre><code>@Configuration
  public class WebMvcConfig implements WebMvcConfigurer {
  @Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          // TODO Auto-generated method stub
          registry.addResourceHandler(&quot;/res/**&quot;).addResourceLocations(&quot;/resources/static/&quot;);
          WebMvcConfigurer.super.addResourceHandlers(registry);
      }
  }
  </code></pre>
  <p>Added this property in <strong>application.properties</strong></p>
  <pre><code>spring.mvc.static-path-pattern=/resources/**
  </code></pre>
  <p>Now in JSP, I'm trying to access CSS with below code, none of them worked</p>
  <pre><code>&lt;%@ taglib prefix=&quot;core&quot; uri=&quot;http://java.sun.com/jsp/jstl/core&quot;%&gt;
  &lt;link href=&quot;&lt;core:url value=&quot;css/style.css&quot;/&gt;&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;all&quot;&gt;
  &lt;link href=&quot;&lt;core:url value=&quot;resources/css/style.css&quot;/&gt;&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;all&quot;&gt;
  &lt;link href=&quot;&lt;core:url value=&quot;res/css/style.css&quot;/&gt;&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;all&quot;&gt;
  </code></pre>
  <p>So I checked in developer tool to see the path created for all above code PFB</p>
  <pre><code>&lt;link href=&quot;&lt;core:url value=&quot;css/style.css&quot;/&gt;&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;all&quot;&gt;
  </code></pre>
  <p>http://localhost:8080/testproject/org/css/style.css</p>
  <pre><code>&lt;link href=&quot;&lt;core:url value=&quot;resources/css/style.css&quot;/&gt;&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;all&quot;&gt;
  </code></pre>
  <p>http://localhost:8080/testproject/org/resources/css/style.css</p>
  <pre><code>&lt;link href=&quot;&lt;core:url value=&quot;res/css/style.css&quot;/&gt;&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;all&quot;&gt;
  </code></pre>
  <p>http://localhost:8080/testproject/org/res/css/style.css</p>
  <p>Let me know what exactly I'm doing wrong here. I know above code will work but some configuration I'm missing or configured wrong.</p>
  <p>I've tried to capture all possible details which may cause this issue. Let me know if need more details.</p>
  <h1><strong>Update 2</strong></h1>
  <p>I created simple spring MVC project which can be downloaded form below link</p>
  <p><a href="https://drive.google.com/file/d/1-WqLnuEH2BffvcwBil7QcKcxqPay214Y/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/1-WqLnuEH2BffvcwBil7QcKcxqPay214Y/view?usp=sharing</a></p>
  <p>few doubts</p>
  <p><strong>static resource loads normally with below code, with or without resource handler mapping in MvcWebConfig.java</strong> just uncomment line 4 &amp; comment line 5 in welcome.jsp css will load.</p>
  <p>the only change i made is &quot;/&quot; in value parm before <strong>css/test.css</strong>.</p>
  <pre><code>&lt;link href=&quot;&lt;core:url value=&quot;/css/test.css&quot;/&gt;&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;all&quot;&gt;
  </code></pre>
  <p><strong>but after adding resource handler , i see 404 error, help me to resolve this issue</strong></p>
  <pre><code>@Override
      public void addResourceHandlers(ResourceHandlerRegistry registry) {
          // TODO Auto-generated method stub
          WebMvcConfigurer.super.addResourceHandlers(registry);
          
          registry.addResourceHandler(&quot;/resources/**&quot;).addResourceLocations(&quot;classpath:/resources/static/css&quot;);
      }
  </code></pre>
  <p>JSP CHANGES</p>
  <pre><code>&lt;link href=&quot;&lt;core:url value=&quot;/resources/test.css&quot;/&gt;&quot; rel=&quot;stylesheet&quot; type=&quot;text/css&quot; media=&quot;all&quot;&gt;
  </code></pre>
  <p>can any one please explain what is the difference between all these mapping or let me know if any of them are wrong.</p>
  <p>assuming <strong>src/main/java/resources/static/css/*.css</strong>  is static folder structure.</p>
  <pre><code>registry.addResourceHandler(&quot;/resources/**&quot;).addResourceLocations(&quot;classpath:/resources/static/css&quot;);
  
  registry.addResourceHandler(&quot;/resources/**&quot;).addResourceLocations(&quot;classpath:/resources/&quot;);
  
  registry.addResourceHandler(&quot;/resources/**&quot;).addResourceLocations(&quot;/resources/&quot;);
  </code></pre>
  <p>below will work as i've changed handler mapping (<strong>myresources</strong>) other than folder structure</p>
  <pre><code>registry.addResourceHandler(&quot;/myresources/**&quot;).addResourceLocations(&quot;classpath:/resources/static/css&quot;);
  </code></pre>
  <p>when i should use classpath in locations ?</p>
  <pre><code>registry.addResourceHandler(&quot;/resources/**&quot;).addResourceLocations(&quot;classpath:/resources/static/css&quot;);
  </code></pre>