title: SpringMVC and static resources
body: <p>Im new in java and spring. I'm trying to make hello world app and don't get what i'm doing wrong.</p>
  
  <p>here is my directory structure:</p>
  
  <pre><code>test_app
  -pom.xml
  -src
  --main
  ---java
  ----com.example.web
  -----IndexController.java
  ---webapp
  ----static
  -----img
  ------example.jpg
  ----WEB-INF
  -----web.xml
  -----dispatcher-servlet.xml
  -----jsp
  ------index.jsp
  </code></pre>
  
  <p>and sources:
  web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;!DOCTYPE web-app PUBLIC
          "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
          "http://java.sun.com/dtd/web-app_2_3.dtd" &gt;
  
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
           id="WebApp_ID" version="2.5"&gt;
  
      &lt;display-name&gt;Movie Reminder WebApp&lt;/display-name&gt;
      &lt;context-param&gt;
          &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
          &lt;param-value&gt;WEB-INF/dispatcher-servlet.xml&lt;/param-value&gt;
      &lt;/context-param&gt;
      &lt;servlet&gt;
          &lt;servlet-name&gt;dispatcher&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;dispatcher&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.ContextLoaderListener&lt;/listener-class&gt;
      &lt;/listener&gt;
      &lt;listener&gt;
          &lt;listener-class&gt;org.springframework.web.context.request.RequestContextListener&lt;/listener-class&gt;
      &lt;/listener&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>dispatcher-servlet.xml:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
         xmlns:context="http://www.springframework.org/schema/context"
         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.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.xsd"&gt;
  
      &lt;context:annotation-config/&gt;
      &lt;context:component-scan base-package="com.example.web"/&gt;
      &lt;bean id="viewResolver"
            class="org.springframework.web.servlet.view.UrlBasedViewResolver"&gt;
  
          &lt;property name="viewClass"
                    value="org.springframework.web.servlet.view.JstlView"/&gt;
          &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt;
          &lt;property name="suffix" value=".jsp"/&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>IndexController.java</p>
  
  <pre><code>package com.example.web;
  
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.servlet.ModelAndView;
  
  @Controller
  public class IndexController {
      @RequestMapping(value = "/")
      public ModelAndView index() {
          return new ModelAndView("index");
      }
  }
  </code></pre>
  
  <p>index.jsp</p>
  
  <pre><code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
  &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
  &lt;head&gt;
      &lt;meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/&gt;
      &lt;title&gt;Hello Spring!&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;img src="&lt;c:url value="/static/img/example.jpg" /&gt;" /&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>when i deploy my app and request , i get 404 on image request</p>
  
  <pre><code>http://localhost:8081/  --- http 200 ok 
  http://localhost:8081/static/img/example.jpg - http 404 not found
  </code></pre>
  
  <p>when i add mvc:resources to dispatcher-servlet.xml</p>
  
  <pre><code>&lt;context:annotation-config/&gt;
  &lt;context:component-scan base-package="com.example.web"/&gt;
  &lt;mvc:resources mapping="/static/**" location="/static/" /&gt;
  </code></pre>
  
  <p>and recompile im getting 404 to / request</p>
  
  <pre><code>http://localhost:8081/  --- http 404 not foundok 
  http://localhost:8081/static/img/example.jpg - http 200 ok
  </code></pre>
  
  <p>Help me please to figure out what i'm doing wrong</p>