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
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> -->
I hope that can help anyone having the same issue
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.
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.
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:
application.properties
: spring.mvc.view.prefix
+ spring.mvc.view.suffix
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>
<!-- 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