title: Spring MVC unable to resolve view name
body: <p>I am writing an application which I want to be able to package into a single executable Jar, and use an embedded web server to serve both a REST API and s set of configuration pages which consume the API (allowing the API to be accessed by other clients outside of the server e.g. an Android app).</p>
  
  <p>So far I have a basic REST API working but keep having problems using it with a view, I have tried following the guide <a href="http://www.mkyong.com/spring-mvc/spring-3-rest-hello-world-example/" rel="nofollow">here</a> and have not been able to get the view name to resolve to the JSP page. Here are some log lines which seem to show it being unable to resolve it:</p>
  
  <pre><code>15:37:23.559 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Looking up handler method for path /UserPage
  15:37:23.560 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Did not find handler method for [/UserPage]
  15:37:23.561 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Matching patterns for request [/UserPage] are [/**]
  15:37:23.561 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - URI Template variables for request [/UserPage] are {}
  15:37:23.562 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapping [/UserPage] to HandlerExecutionChain with handler [org.springframework.web.servlet.resource.ResourceHttpRequestHandler@37f9ecb0] and 1 interceptor
  15:37:23.563 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.DispatcherServlet - Last-Modified value for [/UserPage] is: -1
  15:37:23.564 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.resource.ResourceHttpRequestHandler - Trying relative path [UserPage] against base location: ServletContext resource [/]
  15:37:23.564 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.resource.ResourceHttpRequestHandler - Trying relative path [UserPage] against base location: class path resource [META-INF/resources/]
  15:37:23.565 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.resource.ResourceHttpRequestHandler - Trying relative path [UserPage] against base location: class path resource [resources/]
  15:37:23.565 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.resource.ResourceHttpRequestHandler - Trying relative path [UserPage] against base location: class path resource [static/]
  15:37:23.566 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.resource.ResourceHttpRequestHandler - Trying relative path [UserPage] against base location: class path resource [public/]
  15:37:23.566 [http-nio-8080-exec-1] DEBUG org.springframework.web.servlet.resource.ResourceHttpRequestHandler - No matching resource found - returning 404
  </code></pre>
  
  <p>I have a feeling that because I am trying to run this with an embedded Tomcat server (as opposed to deploying it to a Tomcat server) I am missing a step which configures the <code>/webapp/</code> directory as a webapp, but am fairly new to Spring so am unsure what I am missing.</p>
  
  <p>src/main/webapp/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;IeNoTenshi&lt;/display-name&gt;
  &lt;description&gt;IeNoTenshi internal webserver&lt;/description&gt;
  
  &lt;servlet&gt;
      &lt;servlet-name&gt;mvc-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;mvc-dispatcher&lt;/servlet-name&gt;
      &lt;url-pattern&gt;/&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  
  &lt;context-param&gt;
      &lt;param-name&gt;contextConfigLocation&lt;/param-name&gt;
      &lt;param-value&gt;/WEB-INF/mvc-dispatcher-servlet.xml&lt;/param-value&gt;
  &lt;/context-param&gt;
  
  &lt;listener&gt;
      &lt;listener-class&gt;
          org.springframework.web.context.ContextLoaderListener
      &lt;/listener-class&gt;
  &lt;/listener&gt;
  </code></pre>
  
  <p></p>
  
  <p>src/main/webapp/WEB-INF/mvc-dispatcher-servlet.xml</p>
  
  <pre><code>&lt;beans xmlns="http://www.springframework.org/schema/beans"
  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"&gt;
  
  &lt;context:component-scan base-package="com.dan_nixon.ienotenshi.web.service" /&gt; 
  &lt;mvc:annotation-driven/&gt;
  
  &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix" value="/WEB-INF/pages/" /&gt;
      &lt;property name="suffix" value=".jsp" /&gt;
  &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>src/com/dan_nixon/.../web/service/UsersController.java</p>
  
  <pre><code>@Controller
  @RequestMapping("/service")
  public class UsersController {
  private static final Logger log = LoggerFactory.getLogger(UsersController.class);
  public static final int DEFAULT_COUNT = 10;
  
  @RequestMapping(method=RequestMethod.GET)
  public String test(ModelMap model) {
      model.addAttribute("username", "test");
      model.addAttribute("name", "Test User");
      return "UserPage";
  }
  
  @RequestMapping(value="/user/{user_id}", method=RequestMethod.GET)
  public @ResponseBody ModelAndView getUser(@PathVariable long user_id,
          Model model) {
      INTObjectDB db = new INTObjectDB();
      db.open();
      User user = db.getUser(user_id);
      user.getPermissions();
      db.close();
      ModelAndView mav = new ModelAndView();
      mav.setViewName("UserPage");
      mav.addObject("username", user.getUsername());
      mav.addObject("name", user.getName());
      log.error(String.format("%s", mav));
      return mav;
  }
  </code></pre>