2

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:

enter image description here

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:

enter image description here

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.

Asme Just
  • 1,287
  • 5
  • 27
  • 42
  • You forgot to show the most important information, i.e. the files inside the `views` folder. Is there a `jspPage.jsp` file there? – Andreas Dec 04 '18 at 00:14
  • @Andreas Yes, there is a JSP file, just fixed the screenshot and added a link to its content. – Asme Just Dec 04 '18 at 00:28
  • Q: So what happened with this? Did you resolve the problem? Did my response help and/or correspond to your solution? – paulsm4 Dec 13 '18 at 17:10

1 Answers1

3

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:

  1. I created my starter project with Eclipse STS.

    It was important to specify "War" packaging (vs. the default "Jar")

  2. 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".

  3. 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);
       }
    
  4. 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:

  5. At this point, I was successfully able to display both static and .jsp pages with Spring Boot.

I hope that helps!

paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • 1. I Guess we were editing the post at the same time, so I delete your edit probably by accident. Feel free to edit it, I didn't want to include it as the post was already too long. Am not sure what you mean in 3.b and 3.c – Asme Just Dec 04 '18 at 00:40
  • If it's not already clear, then reading through the tutorials might help your better understand 3b and 3c. – paulsm4 Dec 04 '18 at 00:42
  • Like can I put the JSP file in two different folders? "resources > static" and "webapp > jsp" ? – Asme Just Dec 04 '18 at 00:44
  • I think I did that, just used _views_ folder instead of _jsp_ folder. Am I wrong? – Asme Just Dec 04 '18 at 00:48
  • No, your folder is "webapps > WEB-INF > views". I'm suggesting "webapps > views". But more importantly, please scan through the links I cited (if you haven't already). – paulsm4 Dec 04 '18 at 01:12
  • Well I did this because that's what people recommend, Even in your first link it's in WEB-INF. Same for the third link. – Asme Just Dec 04 '18 at 01:18
  • I'm not sure it's necessarily "the problem". But FYI, "WEB-INF" is intended for things like descriptors (e.g. "web.xml"), lib/*.jar and client classes. Static .html, .css, images and other artifacts are generally stored outside of WEB-INF. Also FYI, I happen to use Eclipse (vs. IntelliJ), so I can't help if the problem turns out to be some kind of IDE-specific "gotcha". Please keep us posted what you find! – paulsm4 Dec 04 '18 at 19:07
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/184730/discussion-between-asme-just-and-paulsm4). – Asme Just Dec 05 '18 at 00:38
  • FYI, I bit the bullet ... created a new "Hello world" Spring Boot app in Eclipse ... enabled both Tomcat and Thymeleaf ... and I'm able to reproduce the problem: `org.thymeleaf.exceptions.TemplateInputException: Error resolving template [index.html]`. Let me dig deeper to find a solution. – paulsm4 Dec 06 '18 at 00:42
  • Hi, I finally got *ONE* example of Spring Boot + Embedded Tomcat/JSP working: https://www.mkyong.com/spring-boot/spring-boot-hello-world-example-jsp/. You can download the working project and "readme.txt" [here](https://github.com/paulsm4/HelloSpringBoot), sub-project "spring-boot-web-jsp". I'm still investigating other "variables" (e.g. Maven vs. Gradle, Spring Boot 1.x vs. Spring Boot 2.1.1, Windows vs. Linux, Thymeleaf vs. Tomcat-only, etc.). As I investigate, I'll create Git branches for each "variable". FYI - and I hope that helps you! – paulsm4 Dec 07 '18 at 22:38
  • GOT IT! The one final piece of "extra magic" I needed for Spring Boot 2.x/Gradle was to add this dependency to build.gradle: `compile('org.apache.tomcat.embed:tomcat-embed-jasper')`! I'll update my response - and post the complete project on Github. – paulsm4 Dec 09 '18 at 21:16