title: Accessing resources in JSP page of Spring MVC app
body: <p>I am fairly new to spring and I am having issues with accessing my resources in my Spring mvc app. I have tried Google and using stack overflow to find an answer (none of these helped me <a href="https://stackoverflow.com/questions/11146212/jsp-access-spring-mvc-resources">possible solution 1</a> , or <a href="https://stackoverflow.com/questions/12847247/not-displaying-images-in-spring-mvc">possible solution</a>, <a href="https://stackoverflow.com/questions/4169266/where-to-place-images-css-in-spring-mvc-app">possible solution 3</a>, <a href="http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html" rel="nofollow noreferrer">spring framework</a> ) but I have found none that have either solved my problem or improved my understanding of the problem (it may be simply because I didn't quite understand the mentioned solutions). </p>
  
  <p>I have managed to embedded my css file into a webpage but using the code:</p>
  
  <pre><code>&lt;%@include file="css/style.css" %&gt;
  </code></pre>
  
  <p>but I cannot access it using a URL this way for obvious reasons. I have also tried using the following in my web.xml file but it has no effect, I admit I don't really understand the mapping in this case so that may be an issue</p>
  
  <pre><code>&lt;mvc:annotation-driven /&gt;
  &lt;mvc:resources mapping="/images/**" location="/images/" /&gt;
  &lt;mvc:resources mapping="/css/**" location="/css/" /&gt;
  </code></pre>
  
  <p>I have also tried using each of the following separately:</p>
  
  <pre><code>&lt;img src="&lt;%=request.getContextPath()%&gt;/images/logo.jpg"/&gt;
  
  &lt;img src="&lt;%=request.getContextPath()%&gt;/src/main/resources/images/logo.jpg"/&gt;
  </code></pre>
  
  <p>This is my project layout:</p>
  
  <p><img src="https://i.stack.imgur.com/5KYwI.png" alt="My Project structure"></p>
  
  <p>This 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;MyProject Application&lt;/display-name&gt;
    &lt;servlet&gt;
      &lt;servlet-name&gt;myServlet&lt;/servlet-name&gt;
          &lt;servlet-class&gt;
                    org.springframework.web.servlet.DispatcherServlet
          &lt;/servlet-class&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
    &lt;servlet-mapping&gt;
      &lt;servlet-name&gt;myServlet&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
    &lt;/servlet-mapping&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>This is my myServlet-servlet.xml:</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
      xmlns:mvc="http://www.springframework.org/schema/mvc"
      xmlns:context="http://www.springframework.org/schema/context"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          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;!-- --&gt;
       &lt;mvc:annotation-driven /&gt;
      &lt;mvc:resources mapping="/images/**" location="/images/" /&gt;
      &lt;mvc:resources mapping="/css/**" location="/css/" /&gt;
      &lt;bean name="/index.html" 
      class="com.myproject.controller.AdminController" /&gt;
      &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver" &gt;
          &lt;property name="prefix"&gt;
              &lt;value&gt;/WEB-INF/pages/&lt;/value&gt;
          &lt;/property&gt;
          &lt;property name="suffix"&gt;
              &lt;value&gt;.jsp&lt;/value&gt;
          &lt;/property&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>and just in case here is my pom.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;groupId&gt;com.myproject&lt;/groupId&gt;
      &lt;artifactId&gt;myProject&lt;/artifactId&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;name&gt;myProject Maven Webapp&lt;/name&gt;
      &lt;url&gt;http://maven.apache.org&lt;/url&gt;
      &lt;properties&gt;
          &lt;springVersion&gt;3.0.4.RELEASE&lt;/springVersion&gt;
      &lt;/properties&gt;
      &lt;dependencies&gt;
  
          &lt;!-- JUnit --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;junit&lt;/groupId&gt;
              &lt;artifactId&gt;junit&lt;/artifactId&gt;
              &lt;version&gt;3.8.1&lt;/version&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
  
          &lt;!-- ORACLE JDBC driver, need install yourself --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;com.oracle&lt;/groupId&gt;
              &lt;artifactId&gt;ojdbc6&lt;/artifactId&gt;
              &lt;version&gt;11.2.0&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.hibernate&lt;/groupId&gt;
              &lt;artifactId&gt;hibernate-core&lt;/artifactId&gt;
              &lt;version&gt;3.6.3.Final&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- Spring framework --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-core&lt;/artifactId&gt;
              &lt;version&gt;${springVersion}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-web&lt;/artifactId&gt;
              &lt;version&gt;${springVersion}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-webmvc&lt;/artifactId&gt;
              &lt;version&gt;${springVersion}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-context&lt;/artifactId&gt;
              &lt;version&gt;${springVersion}&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework&lt;/groupId&gt;
              &lt;artifactId&gt;spring-orm&lt;/artifactId&gt;
              &lt;version&gt;${springVersion}&lt;/version&gt;
          &lt;/dependency&gt;
  
          &lt;!-- for compile only, your container should have this --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;servlet-api&lt;/artifactId&gt;
              &lt;version&gt;2.5&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;version&gt;1.2&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;!-- Commons-logging &amp; log4j --&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;log4j&lt;/groupId&gt;
              &lt;artifactId&gt;log4j&lt;/artifactId&gt;
              &lt;version&gt;1.2.12&lt;/version&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;commons-logging&lt;/groupId&gt;
              &lt;artifactId&gt;commons-logging&lt;/artifactId&gt;
              &lt;version&gt;1.1.1&lt;/version&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;finalName&gt;admin_UI&lt;/finalName&gt;
          &lt;plugins&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.3&lt;/version&gt;
                  &lt;configuration&gt;
                      &lt;webappDirectory&gt;/sample/servlet/container/deploy/directory&lt;/webappDirectory&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  &lt;/project&gt;
  </code></pre>
  
  <p>I would be happy if I can get access the images and css folders and the contents those folders from within the webapp/WEB-INF/pages the same way I can access my jsp pages (ie via URL), but Ideally I would like to understand how I should do this correctly i.e. what folder I should be mapping (src\main\resources or src\main\webapp\WEB-INF\some_resource_folder) and how I should map it.</p>
  
  <p>If any more information is require I would be happy to supply. </p>