Reputation: 1337
I am having issue to display jsp page in Spring-MVC. This is a basic hello world Spring-MVC with Gradle and IntelliJ CE:
I get the following error page:
Here is my build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
}
}
plugins {
id 'java'
}
group 'com.helloct'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-devtools")
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework:spring-jdbc")
compile("com.h2database:h2")
compile("com.fasterxml.jackson.core:jackson-databind")
compile('javax.servlet:jstl')
compile('org.apache.tomcat.embed:tomcat-embed-jasper')
compile 'javax.servlet.jsp:javax.servlet.jsp-api'
testCompile("junit:junit")
}
the The view resolver file:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "hello")
public class WebConfig implements WebMvcConfigurer {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
The controller page:
@Controller
public class JSPController {
@GetMapping("/jspPage")
public String home(){
return "jspPage";
}
}
The jsp page location:
Content of the application.properties file:
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
Using the default template engine, the page displays correctly but using jsp, it doesn't work
Log error:
https://hastebin.com/lijekesoti.apache
NOTE: I know Thymleleaf is the recommanded template for Spring but I want work with JSP for some reason
UPDATE
After reading this post with the help of paulsm4 answer, removing the following line:
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
and removing the view resolver file solved my issue.
Upvotes: 2
Views: 1548
Reputation: 121669
It turns out that it's non-trivial to get JSPs to work with Spring Boot. It also turns out that there are significant changes between Spring Boot 1.x (which most of the tutorials for Spring Boot/JSP were written to) and Spring Boot 2.x.
I found these resources helpful:
I got JSP working with both Spring Boot 1.x and 2.x, with both Maven and Gradle. My project is here on GitHub:
These are the highlights of what I needed to do:
I created my starter project with Eclipse STS.
It was important to specify "War" packaging (vs. the default "Jar")
I added the following dependencies in my build.gradle
:
dependencies {
compile('org.springframework.boot:spring-boot-starter-web')
compile('javax.servlet:jstl')
compile('javax.servlet:javax.servlet-api')
compile('org.apache.tomcat.embed:tomcat-embed-jasper')
compile('org.webjars:bootstrap:4.1.0')
testImplementation('org.springframework.boot:spring-boot-starter-test')
It turns out that 'tomcat-embedded' need NOT be specified (it's included in spring-boot-starter-web by default).
But it also turns out that Embedded Tomcat won't process JSPs unless you explicitly include tomcat-embed-jasper
.
Don't specify a "thymeleaf" dependency - it will conflict with "jasper".
As per other tutorials, I added these lines in my application.properties
:
spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp
I added also added these lines to my root class:
@SpringBootApplication
public class Test7Application extends SpringBootServletInitializer {
...
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Test7Application.class);
}
public static void main(String[] args) {
SpringApplication.run(Test7Application.class, args);
}
Unfortunately, "other tutorials" frequently say to create folder src/main/webapp/WEB-INF/jsp/
. This will NOT work.
Instead, I put my test.jsp
file in folder src/main/resources/META-INF/resources/WEB-INF/jsp
.
These links explain why:
Upvotes: 2