I am trying to develop a new Spring boot application using MVC as a first step to move my existing Spring MVC application to Spring boot.
However, I am facing an is
I am facing The Same issue and I remove this problem by following changes:
@SpringBootApplication
public class MyFirstAppApplication extends SpringBootServletInitializer {
public static void main(String[] args) {
SpringApplication.run(MyFirstAppApplication.class, args);
}
}
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(MyFirstAppApplication.class);
}
No need to make ServletInitializer
new class.
The issue was with the version of jar spring-boot-starter-parent
. For some reason this doesn't work with the version 1.5.3 RELEASE
. It works until version 1.5.2 RELEASE
.
I have updated the pom.xml
's parent tag as below:
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
Why do have two separate java classes(MyFirstAppApplication,ServletInitializer) extending SpringBootServletInitializer class ?
Remove ServletInitializer.java & move configure method from ServletInitializer to MyFirstAppApplication.
There is a dependency to include as Spring boot doesn't know how to translate JSP to Servlet. So,
Check the version of your tomcat-embed-core-.jar.
Go to that corresponding version release on https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper
Copy the dependency, it will look like-
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jasper</artifactId>
<version>9.0.37</version>
</dependency>
here version needs to be updated as per your tomcat-embed-core jar version
Now you are good to go with your project.
thanKs.
I have gone exactly the same path. Upgrading a jsp, xml-based spring application to spring boot 2. There are few things you need to consider when migrating:
First remove @EnableWebMvc.
Second you need @ComponentScan on top of MyFirstAppApplication class.
Try to read this article, it helped me alot https://htr3n.github.io/2018/12/jsp-spring-boot/
Third you also need this dependeny together with embed-jasper:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-core</artifactId>
<version>9.0.22</version>
</dependency>
Last but not least this is a shortcut for creating a view handler in java
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix:.jsp
and as far as I know it only works in spring boot 2. You can have Java implementation instead and debug it to see if it ever hit that.
@Override
public void configureViewResolvers(ViewResolverRegistry registry) {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/jsp/");
resolver.setSuffix(".jsp");
registry.viewResolver(resolver);
}
Because in my case for example I didn't even need a specific or default resolver. I had different resolver for each Controller that I had to define in xml/bean and inject in each class.
I have created a demo project which is rendering jsp
Git URL : https://github.com/rksharma1401/spring-boot-war
take checkout then mvn package java -jar target\simple-web-app-tomcat-0.0.1-SNAPSHOT.war URL : http://localhost:8081/w