Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 962
ystan- Avatar asked Mar 19 '16 09:03

ystan-


People also ask

What is the default view resolver in spring boot?

The InternalResourceViewResolver is also the default view resolver of DispatcherServlet class, which acts as the front controller in Spring MVC framework.

What is the default view resolver in spring MVC?

The default is an automatically-registered InternalResourceViewResolver ( UrlBasedViewResolver is an abstract superclass of this). If you declare your own view resolver(s), then the default InternalResourceViewResolver will not be used. You can, if you, wish, simply redeclare it as an explicit bean.

Which property should be set in spring boot for resolving view name?

The ResourceBundleViewResolver inspects the ResourceBundle identified by the basename, and for each view it is supposed to resolve, it uses the value of the property [viewname].

What is the default configuration file in a spring boot project?

2. Using the Default Location. By convention, Spring Boot looks for an externalized configuration file — application. properties or application.


2 Answers

The Solution

I found the answer via trial-and-error, which turned out rather annoying. I hope someone can correct me if this conclusion is wrong, but it appears that Spring Boot does not like the string WEB-INF. I renamed the WEB-INF directory to view and changed the application.properties to the following and the view loaded successfully.

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

Additional Findings

The objective of this exercise was to create a working example of a minimal, Java-based configuration so I continued minimalising the setup. I then found that lots of advice dished out on multiple SO threads and forums did not help. @JBNizet provided a link in his comment to the Spring Boot docs which lists a very salient point that no one has mentioned: JSPs simply do not play well with Spring Boot as it has limitations depending on the embedded container chosen. With that in mind, I decided to try replacing JSPs with ThymeLeaf templates.

My new working config removes the need for these:

  • No need to add application.properties: spring.mvc.view.prefix + spring.mvc.view.suffix
  • No need to change the packaging type from jar to war
  • No need to modify main class
  • No need to add pom.xml dependencies for
    • org.springframework.boot / spring-boot-starter-tomcat
    • org.springframework.boot / tomcat-embed-jasper
    • javax.servlet / jstl

So just the default Spring Boot template and 2 ThymeLeaf dependencies with the views named as ViewName.html placed in src/main/resources/templates.

<dependency>     <groupId>org.thymeleaf</groupId>     <artifactId>thymeleaf</artifactId> </dependency>  <dependency>     <groupId>org.thymeleaf</groupId>     <artifactId>thymeleaf-spring4</artifactId> </dependency> 
like image 171
ystan- Avatar answered Sep 28 '22 03:09

ystan-


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 
like image 26
Jasbin karki Avatar answered Sep 28 '22 03:09

Jasbin karki