Spring Boot Web Application Example with JSP(Update to Spring Boot 2.6 and Java 17)

This article uses a simple example to demonstrate: How to build a spring boot web applicaiton with JSP.

Why use the JSP in Spring Boot Web Application

As a relatively obsolete technology,By default, the JSP is not used as a template for web page rendering in Spring Boot 2, However, in the face of some old system upgrades written in JSP, you may also want to be able to configure support for JSP.

Next, let’s build a Spring Boot Web Application from scratch and then add support for JSPs.

For the detail of operation, we also provide a voide tutorial in youtube, you can watch it.

Generate a Spring Boot Web Application Example

First, in Spring Boot Starter, fill in the basic configuration information for the project.

Depending on the objectives of the experiment, we chose to use maven as the build tool, Java as the programming language, Select the current stable version for Spring Boot, and Spring Web Starter as the support package.

Add JSP Dependency

To support JSP, We need to add the JSP parser in project. In this example, we choose tomcat-embed-jasper library.

Add tomcat-embed-jasper library in pom.xml file, The final pom file is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?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 https://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.6.2.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>

<groupId>info.mls-tech</groupId>
<artifactId>spring-boot-web-jsp-with-jsp</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-web-with-jsp</name>
<description>Demo project for Spring Boot Web Application with JSP</description>

<properties>
<java.version>17</java.version>
</properties>

<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>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>

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

</project>

Create a Spring MVC Controller

In Spring Boot Web Application, each HTTP request can be configured to respond with a corresponding method in the Controller. In this case, we built a Controller to respond to the root and ‘hello’ path.

Create a new package named controller and create a new class in the package named HelloController, as follows:

1
2
3
4
5
6
7
8
9
10

@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";
}
}

About the more information about spring mvc controller, you can refer the Offical Documentation

Create a JSP page

Create a new webapp directory in the src/main directory, and then create a WEB-INF/jsp directory in the webapp directory. Finally, create a file named hello.jsp in the jsp directory, which content as follows:

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

Configure JSP view

Rename the application.properties file in src/main/resources: application.yml, and add the following:

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

Run Web Application

Run the Web Application

If you run it in console, you can execute:

1
mvn spring-boot:run

If you use IDE tools, you can call maven in the IDE menu to run the application.

After the application is running, you can access the following URL via your browse.

1
http://localhost:8080/hello?name=tom

You can see something like this in your browser

1
Hello tom!

Download the Code

You can download the completed code from GitHub

本文标题:Spring Boot Web Application Example with JSP(Update to Spring Boot 2.6 and Java 17)

文章作者:Morning Star

发布时间:2022年01月19日 - 19:01

最后更新:2022年01月25日 - 19:01

原始链接:https://www.mls-tech.info/java/spring-boot-web-application-with-jsp/

许可协议: 署名-非商业性使用-禁止演绎 4.0 国际 转载请保留原文链接及作者。