title: How to properly use multiple controllers in Spring MVC
body: <p>I am trying to use 2 controllers with one dispatcher servlet in Spring MVC. But I am running into 404 errors when trying to render the views. The dispatcher is pretty straightforward, from web.xml:</p>
  
  <pre><code>&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;
  </code></pre>
  
  <p>and with the following configuration:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
      ...
  &lt;context:component-scan base-package="com.mycompany.azalea" /&gt;
  
  &lt;mvc:annotation-driven /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>The controllers are:</p>
  
  <pre><code>package com.mycompany.azalea;
  
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  
  @Controller
  @RequestMapping(value = "/home")
  public class homeController {
  
      @RequestMapping(value = "/")
      public String home() {
           return "index";
      }
  }
  </code></pre>
  
  <p>and </p>
  
  <pre><code>package com.mycompany.azalea;
  
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  
  @Controller
  @RequestMapping(value = "/data")
  public class dataController {
  
      @RequestMapping(value = "/")
      public String home() {
           return "index";
      }
  }
  </code></pre>
  
  <p>and I am using a pretty standard resolver:</p>
  
  <pre><code>@Configuration
  public class AppConfig {
  
  // Resolve logical view names to .jsp resources in the /WEB-INF/views directory
  @Bean
  ViewResolver viewResolver() {
          InternalResourceViewResolver resolver = new InternalResourceViewResolver();
          resolver.setPrefix("WEB-INF/views/");
          resolver.setSuffix(".jsp");
          return resolver;
      }
  }
  </code></pre>
  
  <p>Views are set up under WEB-INF/views/home/ and WEB-INF/views/data/</p>
  
  <p>However if I try to request a URL like <code>http://localhost:8080/Azalea/home/</code></p>
  
  <p>I get an entry in the GlassFish log:</p>
  
  <p>SEVERE: PWC6117: File ".../build/web/<strong>home</strong>/WEB-INF/views/index.jsp" not found</p>
  
  <p>instead of the expected request for </p>
  
  <p>".../build/web/WEB-INF/views/<strong>home</strong>/index.jsp"</p>
  
  <p>Same pattern for "/data". It essentially looks like the request mapping is inserted into the wrong position in the request.</p>
  
  <p>My current work around is to modify the resolver to </p>
  
  <pre><code>resolver.setPrefix("../WEB-INF/views/"); 
  </code></pre>
  
  <p>and return the following from the controller:</p>
  
  <p>public class homeController {</p>
  
  <pre><code>    @RequestMapping(value = "/")
      public String home() {
           return "home/index";
      }
  }
  </code></pre>
  
  <p>But this seems to be a suboptimal solution. Please let me know if you have any suggestions.</p>