This tutorial will demonstrate how to run JSP pages in Spring Boot applications.

So you are still using JSP pages in your applications and you want to add them in your Spring Boot applications? This tutorial tells you exactly how to do that.

Let's start from the dependencies needed for this application:

  <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>
  <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
  </dependency>

As you can see we have included also the JSTL dependency if you want to use the JSTL libraries in your JSP pages.

Now, in order to configure JSP usage in your Spring Boot application, add the following configuration class in your web application:

package com.example.demo;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

@Configuration
@EnableWebMvc
@ComponentScan
public class Config implements WebMvcConfigurer {

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setViewClass(JstlView.class);
        viewResolver.setPrefix("/WEB-INF/pages/");
        viewResolver.setSuffix(".jsp");
        return viewResolver;
    }

}

Within this configuration class, we have configured:

  • The org.springframework.web.servlet.view.JstlView Class as standard View class
  • The project path where JSP pages are searched in the webapp/WEB-INF/pages folder
  • The suffix for our views to be ".jsp"

With that in place, just add JSP pages in the right project path, as in this example:

src/
├── main
│   ├── java
│   │   └── com
│   │       └── example
│   │           └── cruddemo
│   │               ├── Config.java
│   │               ├── controllers
│   │               │   └── CustomerController.java
│   │               ├── DemoApplication.java
│   │               ├── entities
│   │               │   └── Customer.java
│   │               └── repositories
│   │                   └── CustomerRepository.java
│   ├── resources
│   │   ├── application.properties
│   └── webapp
│       └── WEB-INF
│           └── pages
│               ├── hello.jsp
│               └── index.jsp

Now you can call your JSP pages either directly from the root Web context (ex. http://localhost:8080/index.jsp), or you can also reference them from a Controller:

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView listAction() {

        List<String> data = new ArrayList<>();
        data.add("Hello!");

        ModelAndView mv = new ModelAndView();

        mv.setViewName("index");
        mv.addObject("data", data);

        return mv;

    }

In the latter example, the JSP page will be dispatched from the REST Endpoint, passing an Iterable List into it.

That's all. We have just demonstrated how to use JSP pages in a Spring Boot application.

FREE WildFly Application Server - JBoss - Quarkus - Drools Tutorials