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 |
|
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 |
|
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 |
|
Configure JSP view
Rename the application.properties file in src/main/resources: application.yml, and add the following:
1 | spring: |
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