DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Curious about the future of data-driven systems? Join our Data Engineering roundtable and learn how to build scalable data platforms.

Data Engineering: The industry has come a long way from organizing unstructured data to adopting today's modern data pipelines. See how.

Threat Detection: Learn core practices for managing security risks and vulnerabilities in your organization — don't regret those threats!

Managing API integrations: Assess your use case and needs — plus learn patterns for the design, build, and maintenance of your integrations.

Related

  • How to Implement Specific Distributed System Patterns Using Spring Boot: Introduction
  • Integrate Spring With Open AI
  • Distributed Task Synchronization: Leveraging ShedLock in Spring
  • Be Punctual! Avoiding Kotlin’s lateinit In Spring Boot Testing

Trending

  • How to Create a Pokémon Breeding Gaming Calculator Using HTML, CSS, and JavaScript
  • How to Identify Bottlenecks and Increase Copy Activity Throughput in Azure Data Factory
  • Build a Multilingual Chatbot With FastAPI and Google Cloud Translation
  • Microservices Design Patterns for Highly Resilient Architecture
  1. DZone
  2. Coding
  3. Frameworks
  4. Spring Boot 2 With JSP View

Spring Boot 2 With JSP View

Creating a Spring Boot 2 web app just got a lot easier.

By 
Yogen Rai user avatar
Yogen Rai
·
Updated Dec. 29, 20 · Tutorial
Likes (21)
Comment
Save
Tweet
Share
112.2K Views

Join the DZone community and get the full member experience.

Join For Free

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

What You'll Need

1. Project Structure

You can get the blueprint from the Spring Initializer page.

2. Project Dependencies

pom.xml

XML
 




x
55


 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3
   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4
   <modelVersion>4.0.0</modelVersion>
5
   <parent>
6
      <groupId>org.springframework.boot</groupId>
7
      <artifactId>spring-boot-starter-parent</artifactId>
8
      <version>2.1.6.RELEASE</version>
9
      <relativePath/> <!-- lookup parent from repository -->
10
   </parent>
11
   <groupId>com.eprogrammerz.examples.spring</groupId>
12
   <artifactId>spring-boot-jsp</artifactId>
13
   <version>0.0.1-SNAPSHOT</version>
14
   <name>spring-boot-jsp</name>
15
   <description>Example Spring Boot with JSP view</description>
16

          
17
   <properties>
18
      <java.version>1.8</java.version>
19
   </properties>
20

          
21
   <dependencies>
22
      <dependency>
23
         <groupId>org.springframework.boot</groupId>
24
         <artifactId>spring-boot-starter-web</artifactId>
25
      </dependency>
26

          
27
      <dependency>
28
         <groupId>org.springframework.boot</groupId>
29
         <artifactId>spring-boot-starter-tomcat</artifactId>
30
         <scope>provided</scope>
31
      </dependency>
32

          
33
      <dependency>
34
         <groupId>org.apache.tomcat.embed</groupId>
35
         <artifactId>tomcat-embed-jasper</artifactId>
36
         <scope>provided</scope>
37
      </dependency>
38

          
39
      <dependency>
40
         <groupId>org.springframework.boot</groupId>
41
         <artifactId>spring-boot-starter-test</artifactId>
42
         <scope>test</scope>
43
      </dependency>
44
   </dependencies>
45

          
46
   <build>
47
      <plugins>
48
         <plugin>
49
            <groupId>org.springframework.boot</groupId>
50
            <artifactId>spring-boot-maven-plugin</artifactId>
51
         </plugin>
52
      </plugins>
53
   </build>
54

          
55
</project>


3. Configuration

3.1 Application Configurations

This SpringBootServletInitializer runs a SpringBootJspApplication from a traditional WAR deployment

SpringBootJspApplication.java

Java
 




xxxxxxxxxx
1
19


 
1
package com.eprogrammerz.examples.spring.springbootjsp;
2

          
3
import org.springframework.boot.SpringApplication;
4
import org.springframework.boot.autoconfigure.SpringBootApplication;
5
import org.springframework.boot.builder.SpringApplicationBuilder;
6
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
7

          
8
@SpringBootApplication
9
public class SpringBootJspApplication extends SpringBootServletInitializer {
10
   @Override
11
   protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
12
      return application.sources(SpringBootJspApplication.class);
13
   }
14

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

          
19
}


3.2 Resources

application.properties

Properties files
 




xxxxxxxxxx
1


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


4. Controller and View Template

4.1 Create a Controller With Simple Method Handling the Base Request

Java
 




xxxxxxxxxx
1
16


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

          
3
import org.springframework.stereotype.Controller;
4
import org.springframework.ui.Model;
5
import org.springframework.web.bind.annotation.GetMapping;
6
import org.springframework.web.bind.annotation.RequestParam;
7

          
8
@Controller
9
public class HelloController {
10
    @GetMapping({"/", "/hello"})
11
    public String hello(Model model, @RequestParam(value="name", required=false, defaultValue="World") String name) {
12
        model.addAttribute("name", name);
13
        return "hello";
14
    }
15
}
16

          


4.2 Add JSP View as a Template

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

HTML
 




xxxxxxxxxx
1
10


 
1
<!DOCTYPE html>
2
<html lang="en">
3
<head>
4
    <meta charset="UTF-8">
5
    <title>Hello ${name}!</title>
6
</head>
7
<body>
8
    <h2 class="hello-title">Hello ${name}!</h2>
9
</body>
10
</html>


5. Run With Maven

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

Shell
 




xxxxxxxxxx
1


 
1
mvn clean spring-boot:run


Then go to localhost:8080 to test out your application.

That's it!

All code is available on GitHub.

Spring Framework Spring Boot

Published at DZone with permission of Yogen Rai, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • How to Implement Specific Distributed System Patterns Using Spring Boot: Introduction
  • Integrate Spring With Open AI
  • Distributed Task Synchronization: Leveraging ShedLock in Spring
  • Be Punctual! Avoiding Kotlin’s lateinit In Spring Boot Testing

Partner Resources


Comments

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: