Spring Boot 2 with JSP View

In this tutorial, I am going to show you how easy to create web application with Spring Boot 2, along with using embedded Tomcat + JSP template, and JSP views.

What you'll need

  • JDK 8+ or OpenJDK 8+
  • Maven 3+

1. Project Structure

You can get the blueprint from Spring Initializer page which leads us to have structure like this:

project-strucutreFigure: Project Structure

2. Project Dependencies

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
   <modelVersion>4.0.0</modelVersion>
   <parent>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-parent</artifactId>
      <version>2.1.6.RELEASE</version>
      <relativePath/> <!-- lookup parent from repository -->
   </parent>
   <groupId>com.eprogrammerz.examples.spring</groupId>
   <artifactId>spring-boot-jsp</artifactId>
   <version>0.0.1-SNAPSHOT</version>
   <name>spring-boot-jsp</name>
   <description>Example Spring Boot with JSP view</description>

   <properties>
      <java.version>1.8</java.version>
   </properties>

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

   <build>
      <plugins>
         <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
         </plugin>
      </plugins>
   </build>

</project>

3. Configuration

3.1 Application Configurations

This SpringBootServletInitializer run a SpringBootJspApplication from a traditional WAR deployment

SpringBootJspApplication.java

package com.eprogrammerz.examples.spring.springbootjsp;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class SpringBootJspApplication extends SpringBootServletInitializer {
   @Override
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
      return application.sources(SpringBootJspApplication.class);
   }

   public static void main(String[] args) {
      SpringApplication.run(SpringBootJspApplication.class, args);
   }

}

3.2 Resources

application.properties

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

4. Controller and View Template

4.1 Create a controller with simple method handling base request

HelloController.java

package com.eprogrammerz.examples.spring.springbootjsp.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
public class HelloController {
    @GetMapping({"/", "/hello"})
    public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
        model.addAttribute("name", name);
        return "hello";
    }
}

4.2 Add JSP view as template

For JSP files, put in src/main/webapp/WEB-INF/jsp/

hello.jsp

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Hello ${name}!</title>
</head>
<body>
    <h2 class="hello-title">Hello ${name}!</h2>
</body>
</html>

5. Run with Maven

You can run your application with bash command at the project root directory as:

mvn clean spring-boot:run

and visit to localhost:8080 to test out your application.