title: Spring MVC resource Mapping
body: <p>I have been testing some request mapping in Spring MVC, and I came across a strange situation in my application. I decided to create a simple cenario so that you can understand my problem. I will first show you the details of my project (the source), and then I'll get to my question.</p>
  
  <p>I have the following directory structure in my project:</p>
  
  <pre><code>+webapp
    +WEB-INF
      +recursos
        +estilos
          test.css
      +spring
        fronte-beans.xml
      +views
        +testes
          page1.jsp
          page2.jsp
    web.xml
  </code></pre>
  
  <p>My Tomcat deployment descriptor:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:web="http://xmlns.jcp.org/xml/ns/javaee"
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd http://xmlns.jcp.org/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
      id="WebApp_ID" version="2.4"&gt;
  
      &lt;display-name&gt;Archetype Created Web Application&lt;/display-name&gt;
  
      &lt;servlet&gt;
          &lt;servlet-name&gt;fronte&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;WEB-INF/spring/fronte-beans.xml&lt;/param-value&gt;
          &lt;/init-param&gt;
          &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
      &lt;/servlet&gt;
  
      &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;fronte&lt;/servlet-name&gt;
          &lt;url-pattern&gt;/&lt;/url-pattern&gt;
      &lt;/servlet-mapping&gt;
  
  &lt;/web-app&gt;
  </code></pre>
  
  <p>My application context for DispatcherServlet:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
  
  &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.xsd
              http://www.springframework.org/schema/mvc
              http://www.springframework.org/schema/mvc/spring-mvc.xsd
              http://www.springframework.org/schema/context
              http://www.springframework.org/schema/context/spring-context.xsd "&gt;
  
      &lt;mvc:annotation-driven /&gt;
  
      &lt;mvc:resources mapping="/recursos/**" location="/WEB-INF/recursos/" /&gt;
  
      &lt;context:component-scan base-package="com.regra7.minhaapp.contro" /&gt;
  
      &lt;bean id="handlerMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt;
      &lt;/bean&gt;
  
      &lt;bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
          &lt;property name="prefix" value="/WEB-INF/views/" /&gt;
          &lt;property name="suffix" value=".jsp" /&gt;
      &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>My controller class for page1.jsp:</p>
  
  <pre><code>import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  
  @Controller
  @RequestMapping(value="/path")
  public class TestController 
  {
      @RequestMapping(value="/to/something")
      public String getPage()
      {
          return "testes/page2";
      }
  }
  </code></pre>
  
  <p>My page1.jsp:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  
  &lt;%@ page 
      language="java" 
      contentType="text/html; charset=ISO-8859-1"
      pageEncoding="ISO-8859-1"%&gt;
  
  &lt;%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
  &lt;%@taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %&gt;
  
  &lt;html lang="pt-BR"&gt;
  
      &lt;!-- ############################################### --&gt;
      &lt;!--                                          HEADER --&gt;
      &lt;!-- ############################################### --&gt;
  
      &lt;head&gt;
          &lt;title&gt;Test Page&lt;/title&gt;
          &lt;meta name="author"                 content="RDS"       /&gt;
          &lt;meta name="description"            content="? ? ?" /&gt;
  
          &lt;meta charset="UTF-8" /&gt;
  
          &lt;link rel="stylesheet" type="text/css" href="recursos/estilos/test.css" media="all" /&gt;
      &lt;/head&gt;
  
      &lt;!-- ############################################### --&gt;
      &lt;!--                                            BODY --&gt;
      &lt;!-- ############################################### --&gt;
  
      &lt;body&gt;
  
          &lt;h1&gt;PAGE 1&lt;/h1&gt;
          &lt;p&gt;This is a test, p1.&lt;/p&gt;
          &lt;p&gt;This is a test, p2.&lt;/p&gt;
          &lt;p&gt;This is a test, p3.&lt;/p&gt;
  
          &lt;a href="${pageContext.request.contextPath}/path/to/something"&gt;CLICK TO PAGE 2&lt;/a&gt;
  
      &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>I can access page1.jsp and page2.jsp smoothly, but the CSS file of page2.jsp ends up not being found. The following text is printed on my console:</p>
  
  <pre><code>dez 29, 2014 8:16:22 PM org.springframework.web.servlet.PageNotFound noHandlerFound
  WARNING: No mapping found for HTTP request with URI [/testeSpringMvc/path/to/recursos/estilos/test.css] in DispatcherServlet with name 'fronte'
  </code></pre>
  
  <p>For what reason "/path/to" is being included in the resulting path? If I try to add different combinations of mapping, both class or method-level, the same thing happens. However, if I map the link to a query string (URL) as follows, the file is found without problems...</p>
  
  <p>page1.jsp:</p>
  
  <pre><code>&lt;a href="${pageContext.request.contextPath}/?cd=page2"&gt;CLICK TO PAGE 2&lt;/a&gt;
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>@Controller
  @RequestMapping(value="/")
  public class TestController
  ...
  @RequestMapping(params="cd=page2")
  public String getPage()
  </code></pre>
  
  <p>What's happening? How can I set a path I want so that my pages use and find the necessary resources? I'm trying to separate pages in different paths so that I can apply the security features from Tomcat (security constraints).</p>
  
  <p><strong>NOTE</strong>: I've tried using contextPath to help me in setting the CSS file path, but nothing worked. In fact, the situation worsened because Page1.jsp also turned out not having stylization:</p>
  
  <pre><code>&lt;link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}recursos/estilos/test.css" media="all" /&gt;
  </code></pre>
  
  <p>As always, thank you for your attention and time.</p>