title: Why don't my .jsp files work with Spring Boot using Spring MVC
body: <p>I've spent the entire day trying to get a simple Spring Boot application to show my .jsp files. I can't switch to another technology (requirements) and I can't get it to work. It's a very simple application. Here's the file structure:</p>
  
  <pre><code>pom.xml
  src/
    main/
      java/
        hello/
          HelloController.java
          MVCApplication.java
      resources/
        application.properties
        static/
          index.html
        templates/
          pages/
            helloworld.jsp
  </code></pre>
  
  <p>Here are my two java files:</p>
  
  <p>HelloController.java:</p>
  
  <pre><code>// @RestController // From when I first posted this question. 
  @Controller
  public class HelloController {
  
    // Should I add a "produces" attribute to this? I don't know what to use for jsp.
    @RequestMapping("/hello")
    public String hello(Model model) {
      model.addAttribute("greeting", "Hello Spring MVC");
      // I see this message in the output, so I know this method is getting called.
      System.err.printf("Setting \"greeting\" to \"Hello Spring MVC\"%n");
      return "helloworld";
    }
  }
  </code></pre>
  
  <p>MVCApplication.java:</p>
  
  <pre><code>@SpringBootApplication
  public class MVCApplication extends SpringApplication {
  
    public static void main(String[] args) {
      SpringApplication.run(MVCApplication.class, args);
    }
  
    // Since I have the properties spring.mvc.view.prefix and spring.mvc.view.suffix
    // defined in applications.properties, I don't think I need this method.
    // I have tried it both with and without this method. It doesn't work either way.
    @Bean
    public ViewResolver getViewResolver() {
      // I see this line in the output, which confirms that this method gets called.
      System.err.printf("Creating custom JSP View Resolver%n"); // NON-NLS
      InternalResourceViewResolver resolver = new InternalResourceViewResolver();
      resolver.setPrefix("/templates/pages/");
      resolver.setSuffix(".jsp");
      resolver.setViewClass(JstlView.class);
      return resolver;
    }
  }
  </code></pre>
  
  <p>My helloworld.jsp file is very simple, but I never see it in the browser:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;meta charset="UTF-8"&gt;
      &lt;title&gt;Spring MVC -HelloWorld&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
    &lt;h1&gt;Greeting: ${greeting}&lt;/h1&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I have defined three properties in applications.properties.</p>
  
  <pre><code>spring.mvc.view.prefix=/templates/pages/
  spring.mvc.view.suffix=.jsp
  
  server.port=7070
  </code></pre>
  
  <p>I know the properties are getting read, because I need to specify port 7070 in my browser to see anything. So all these properties should be working.</p>
  
  <p>My pom file seems to have all the right dependencies. </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/xsd/maven-4.0.0.xsd"&gt;
          &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
  
      &lt;groupId&gt;org.springframework&lt;/groupId&gt;
      &lt;artifactId&gt;gs-spring-boot&lt;/artifactId&gt;
      &lt;version&gt;0.1.0&lt;/version&gt;
  
          &lt;parent&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
              &lt;version&gt;1.5.2.RELEASE&lt;/version&gt;
          &lt;/parent&gt;
  
          &lt;dependencies&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
              &lt;/dependency&gt;
  
              &lt;!--JSP Enabled: --&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
              &lt;/dependency&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;org.apache.tomcat.embed&lt;/groupId&gt;
                  &lt;artifactId&gt;tomcat-embed-jasper&lt;/artifactId&gt;
                  &lt;scope&gt;provided&lt;/scope&gt;
              &lt;/dependency&gt;
              &lt;dependency&gt;
                  &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
                  &lt;artifactId&gt;jstl&lt;/artifactId&gt;
              &lt;/dependency&gt;
          &lt;/dependencies&gt;
  
          &lt;properties&gt;
              &lt;java.version&gt;1.8&lt;/java.version&gt;
          &lt;/properties&gt;
  
          &lt;packaging&gt;jar&lt;/packaging&gt;
  
          &lt;build&gt;
              &lt;plugins&gt;
                  &lt;plugin&gt;
                      &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                      &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
                  &lt;/plugin&gt;
              &lt;/plugins&gt;
          &lt;/build&gt;
  
      &lt;/project&gt;
  </code></pre>
  
  <p>The templates directory used to be called WEB-INF, but I read somewhere that WEB-INF doesn't work well with Spring Boot. But it doesn't work either way.</p>
  
  <p>I go to localhost:7070/hello, and all that I get is an error message:</p>
  
  <p><strong><em>There was an unexpected error (type=Not Found, status=404).
  /templates/pages/helloworld.jsp</em></strong></p>
  
  <p>This tells me that my controller method is getting called, but it's either not finding my .jsp file, or it's not even looking for it.</p>
  
  <p>I've looked at a lot of similar questions on this forum, and so far, nothing has helped. People have made suggestions like switching to thymeleaf, which is probably fine for other people, but it's not an option for me. Can anybody figure out what I'm doing wrong?</p>
  
  <hr>
  
  <p>Addendum:</p>
  
  <p>At Vipul Singh's suggestion, I tried changing the controller method to this:</p>
  
  <pre><code>@RequestMapping("/hello")
  public ModelAndView hello(Model theModel) {
    // theModel.addAttribute("greeting", "Hello Spring MVC");  // this didn't work.
    ModelAndView modelView= new ModelAndView("helloworld", "helloworld", theModel);
    modelView.addObject("greeting", "Hello Spring MVC"); // This didn't work either.
    return modelView;
  }
  </code></pre>
  
  <p>It didn't work. I got the same error message as before.</p>
  
  <p><em>Revised version. Originally I used @RestController, which was wrong. I also reduced the spring.boot.starter.parent version from 1.5.7 to 1.5.2</em></p>