By default, where does Spring Boot expect views to be stored?

Viewed 63615

I'm experimenting on re-writing my configuration-heavy, vanilla Spring MVC project using Spring Boot. I started a brand new Spring Boot project in IntelliJ using the Spring Boot Initiaizer and I'm going the route of minimal Java-based configuration. Lots of tutorials point out that the default main class generated is sufficient and that @SpringBootApplication has everything included. I got a sample REST Controller to work and return a serialized object as JSON, but it appears getting a view to show is proving difficult. My structure is as follows, with everything default other than the webapps directory which I created.

src
`-main
   `-java
   `-resources
     `-static
     `-templates
   `-webapp
     `-WEB-INF
        `-home.jsp

The controller is plain simple.

@Controller
public class ViewMaster {
    @RequestMapping("/home")
    public String home() {
        return "home";
    }
}

Without any configuration, I'd like to know where Spring Boot expects the views to be stored and with what extension (html?). I've also tried to include the following in application.properties but I still get a 404 error. Moving the WEB-INF directory or just the html file around in resources didn't help either.

spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp

I've also tried including these dependencies in my pom.xml without any effect.

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

I must be missing something painfully obvious here so appreciate if someone can point that out!

9 Answers

Without any configuration Spring Boot expects the views to be stored inside /webapp, the view page may be of any format depends on application.properties settings(like html or jsp) to set .jsp as view page at /views/ folder

spring.mvc.view.prefix=/views/
spring.mvc.view.suffix=.jsp //for .html change it to .html

and you have to use tomcat jaspher , if you don't include it the page will not be parsed instead it gets downloaded as a file

spring.mvc.view.prefix=/views/
spring.mvc.view.suffix=.jsp

I had the same problem, the JSP pages was well placed and the required configuration was set correctly in application.properties

After a long examination, I finally found that the problem was caused by the dependency "spring-boot-starter-thymeleaf" that should be removed from the pom.xml. Indeed, this dependency supports the XHML/HTML files in web application, whereas we are using JSP pages

Therefore, if we are using JSP pages in Spring MVC, any kind of the following dependency should be removed:

<!--        <dependency> -->
<!--            <groupId>org.springframework.boot</groupId> -->
<!--            <artifactId>spring-boot-starter-thymeleaf</artifactId> -->
<!--        </dependency> -->

If you found this article and you are returning

return "home.jsp";

instead of just "home". Then you must remove

spring.mvc.view.suffix=.jsp

Otherwise remove .jsp from home and keep the suffix!

According to answer given by @Jasbin karki I tried and found placing your html or jsp files in main->webapp->views folder(you should create the webapp & views folders) and adding the following lines in application.properties file does the task. Add the following in application.properties:

spring.mvc.view.prefix=/views/

spring.mvc.view.suffix=.html //or .jsp- if its a jsp file

application.properties

Remove:

spring.mvc.view.prefix=/view/
spring.mvc.view.suffix=.jsp

Add:

spring.thymeleaf.prefix= /WEB-INF/views/
spring.thymeleaf.suffix= .html

pom.xml

Remove jasper dependency:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

Add thymeleaf dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Details: Using thymeleaf instead of jsp

<!-- Tomcat for JSP rendering -->
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

Change Scope to "default" solved my problem

Related