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

10

34

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!

Stickup answered 19/3, 2016 at 9:51 Comment(5)
Have you read this? docs.spring.io/spring-boot/docs/current/reference/htmlsingle/…Rough
I don't see any .jsp files under your /WEB-INF/ directory. I see only home.html file, is that a jsp file and a typo error in question?Chock
@JBNizet Thanks. Did not know about the JSP limitations.Stickup
@SanjayRawat Yes that was a typo. Thanks for pointing it out.Stickup
Old, but this might still be relevant: Isn't the WEB_INF directory supposed to only hold hidden views?Buy
S
27

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>
Stickup answered 20/3, 2016 at 9:35 Comment(9)
The ThymeLeaf part is the second part of this answer. The first paragraph answers the original question. Unless you can prove that somehow the WEB-INF configuration was working for you? I did a simple rename of the view directory and the configuration somehow worked. All the "solutions" you proposed did not work and you are being sore about it by marking my answer down when it is the only answer that has resolved the issue. You are being a very bad sport in this community.Stickup
um...... the my works for me i use spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp in application proeprtiesJeremy
In my opinion the best way today for useing spring in a web enviroment is use ThymeLeaf but if you askhelps for jsp I answare for jspJeremy
I have just tried that and it does not work. I am using a completely empty structure so there should be no other factors. This is the error: Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback. Sun Mar 20 18:05:56 SGT 2016 There was an unexpected error (type=Not Found, status=404). No message availableStickup
? it is impossible. repeate the step: empty project. do the my advice. create of course a controller for reatcive the url that you test and deploy it in a tomcat, i try with tomcat 8Jeremy
if you want i post on my github the my poceven if is very similar to the offical guide: github.com/spring-projects/spring-boot/tree/master/…Jeremy
@tys The answer "Valerio Vaudi" posted works fine, I just created a spring-boot-jsp-demo project with all the config you mentioned in the the question and it is working fine.Chock
I now suspect this has something to do with IntelliJ. Assume both of you are using eclipse/STS?Stickup
i found out my error was the way i was executing the app instead executing it on a server tomcat i was executing it as a spring boot appBrewage
C
8

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
Chalybeate answered 1/7, 2018 at 8:57 Comment(0)
E
2

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!

Ellis answered 17/10, 2021 at 15:37 Comment(0)
R
1

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> -->
Reliquiae answered 23/10, 2019 at 6:59 Comment(0)
H
1

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
Heed answered 21/1, 2020 at 6:48 Comment(1)
your answer isn't providing any additional insight into this issue. this question already has an accepted answer from 5 years+ ago and has had multiple other answers added so i'm not sure why you are adding another duplicate. if you are trying to clarify a point, please use comments instead.Stickup
J
0

The first think that you have do, is insert the dependency in pour pom like below

<dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>

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

With this dependency you say at spring boot that the embedded tomcat dependency are provided and this have the effect that your spring app don't have the tomcat dependency inside the jar.

The second think that you have do, is change the pakaging proeprties in your pon from jar to war like below

<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">
...
    <packaging>war</packaging>
    ....
</project>

The third and last think that you heve do is refactor the standard Application Boot startrer like below

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }
}

for your configuration in application properties may be fine but I suggest you to put the your jsp under a specific folder and then refactor the your config for point to the new path, and not just under WEB-INF but this is just an advice.

I hope that this can help you.

Jeremy answered 19/3, 2016 at 13:29 Comment(5)
All 3 steps did not helpStickup
it is very strange can you do more details about the your errors?Jeremy
the error was a vanilla 404 not found. i've found the root cause and posted my own answer. feel free to test and critique!Stickup
Ok I readed your answare and this is true, spring boot expect the your view by deffault in templates. but in your question appear that you want conserv the your legacy structure whit jsp and an maven project style for webapp running on a standalone werver. If you are ready to migrate your legacy app written in jsp to a new web app with embedded web server ( for this reason you don't need to change from jar to war), don't use jsp in a standalone web server (for this reason you don't need th my other changing)Jeremy
ok the my answare is not correct but in really the your question are not correct. If you say please help me to running this kind of structure I try to help you but if you can change the your application structure the case is totally different. Anyway I'm very happy for you that you found the problemJeremy
L
0

Set prefix suffix in spring thymeleaf view, this tutorial works:

@Bean
public ClassLoaderTemplateResolver secondaryTemplateResolver() {
    ClassLoaderTemplateResolver secondaryTemplateResolver = new ClassLoaderTemplateResolver();
    secondaryTemplateResolver.setPrefix("templates-2/");
    secondaryTemplateResolver.setSuffix(".html");
    secondaryTemplateResolver.setTemplateMode(TemplateMode.HTML);
    secondaryTemplateResolver.setCharacterEncoding("UTF-8");
    secondaryTemplateResolver.setOrder(1);
    secondaryTemplateResolver.setCheckExistence(true);
        
    return secondaryTemplateResolver;
}
Libelous answered 26/5 at 13:5 Comment(0)
V
-1

tys, pls try to add dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

Reference : https://github.com/lenicliu/examples/tree/master/examples-spring-boot/examples-spring-boot-jsp

spring.mvc.view.prefix is a relative path of webapp folder, and u can put jsp files into it.

Volotta answered 19/3, 2016 at 13:17 Comment(1)
This was not required.Stickup
R
-1

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

Redwing answered 17/1, 2018 at 1:56 Comment(0)
S
-2
<!-- 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

Sacker answered 19/2, 2020 at 9:46 Comment(1)
this is irrelevant to the question asked, which already has many established answers here.Stickup

© 2022 - 2024 — McMap. All rights reserved.