Enterprise Java

Spring Boot & JSP

This guide shows how to use Spring Boot to create MVC web application with JSP.

Prerequisites:

  • Eclipse IDE (neon release)
  • Maven 4
  • Java 1.8

1. Create maven web project

Open eclipse then create a new maven web project and name it as SpringBootWebJsp.

The structure of the generated projects look like the following:

2. pom.xml

After creating the web project, the first step is to configure Spring Boot inside pom.xml, so we add the following as a parent dependency:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
</parent>

Spring Boot exposes a starter called spring-boot-starter-web which automatically imports all the required jars needed to setup a typical Spring MVC application and automatically configures the view resolver and the servlet dispatcher of the application so that the developer focuses on the development rather than the configuration, so we add the starter as a dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

Since we’re using JSP as the front end technology, we need to add the following dependency in order to be able to compile JSP and make use of its features:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>

That’s all, just 2 dependencies can make your MVC application up. Below are the auto-imported jars:

This is the whole pom.xml for reference:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.programmer.gate</groupId>
  <artifactId>SpringBootWebJSP</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>SpringBootWebJSP</name>
  
  <properties>
       <maven.compiler.source>1.8</maven.compiler.source>
       <maven.compiler.target>1.8</maven.compiler.target>
    </properties>
    
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.10.RELEASE</version>
    </parent>
      <dependencies>
            
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.apache.tomcat.embed</groupId>
                <artifactId>tomcat-embed-jasper</artifactId>
                <scope>provided</scope>
            </dependency>
      </dependencies>
  
      <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
</project>

P.S: when using JSP as a front end technology, you should make sure to set the “packaging” attribute as war not jar, since Spring Boot team claimed that currently there is limitations for supporting JSP inside jar file ( the view resolver wouldn’t map correctly).

3. Application.java

The second step is to create the Spring Boot initializer class, this is the entry point of our application. When annotating a class with @SpringBootApplication, we’re configuring our application to run over the embedded servlet container provided by Spring Boot (tomcat by default).

Application.java

package com.programmer.gate;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
 
@SpringBootApplication
public class Application{
 
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

4. application.properties

Spring Boot auto configures the view resolver and the dispatcher servlet of the web application and provides us a way to define our own configuration using application.properties.

So we create application.properties under src/main/resources and define the following properties:

spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
server.port=9093
home.message= Programmer Gate

Here we’re configuring the view resolver to map the views to files of type jsp under “WEB-INF/jsp/”. We also change the default port of the embedded tomcat to be 9093, as well as defining other business message properties to be used later on inside our application.

5. home.jsp

In this tutorial, we’re creating a very simple MVC application which displays a welcome message to the end user, so we create a very basic jsp file called home.jsp under WEB-INF/jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
    <h1>Welcome to ${message} </h1> 
</body>
</html>

6. HomeController.java

Now we create our main controller named HomeController under com.programmer.gate and we define it to serve requests coming from the root path as the following:

package com.programmer.gate;
import java.util.Map;
 
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
 
@Controller
public class HomeController {
 
    @Value("${home.message}")
    private String message;
 
    @RequestMapping("/")
    public String welcome(Map<String, Object> model) {
        model.put("message", this.message);
        return "/home";
    }
 
}

Our controller simply reads the welcome message from application.properties and then redirects to home.jsp.

7. Deploy the application

Following are the steps to deploy our application:

  • Right click pom.xml -> run-as -> Maven install
  • Maven generates a war file called SpringBootWebJSP-0.0.1-SNAPSHOT.war inside target folder
  • Open cmd, then run the war using: java -jar SpringBootWebJSP-0.0.1-SNAPSHOT.war

Here we go, our application is up and ready to serve requests at port 9093.

That’s it, i hope you like it. For clarifications please leave your thoughts in the comments section below.

Published on Java Code Geeks with permission by Hussein Terek, partner at our JCG program. See the original article here: Spring Boot + JSP

Opinions expressed by Java Code Geeks contributors are their own.

Hussein Terek

Hussein is a senior software engineer with 5 years of experience in software design, development and integration. He is the author and founder of Programmer Gate (www.programmergate.com) blog.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
tarantsoff
tarantsoff
6 years ago

Hello Hussein. Thank you for interesting material. Just one question – is it possible to run such application unpacked like exploded war? Usually it is very convenient to be able to run it exploded for have fast development cycles. How do you run such projects from your IDE? Can you show sample run configuration? Thank you!

Back to top button