In this tutorial, you will learn how to configure the Spring Boot Web MVC application to support JSP(Java Server pages) and JSTL(JSP Standard Tag Library).
Video Tutorial
Maven Dependencies
To configure your Spring Boot application to support Spring MVC, JSP and JSTL add the following maven dependencies to the pom.xml file.
Spring Web
The following dependency will add support to build Spring Web MVC applications as well as RESTful Web Services with Spring Framework.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
JSP
The following Maven dependency will add support for JSP. Notice the provided scope. This scope is required.
<!-- https://mvnrepository.com/artifact/org.apache.tomcat.embed/tomcat-embed-jasper --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
JSTL
The following Maven dependency will add support for JSTL. The provided scope is optional.
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <scope>provided</scope> </dependency>
JSP Folder
Now that we have added the required maven dependencies, your application can support JSP. JSP pages are usually placed in the following directory /src/main/webapp/WEB-INF/jsp. If your Spring Boot application does not have this directory, then you should create it.
Application Properties File
Now that you have created a folder for your JSP pages, we need to tell Spring Framework about its location. We also need to configure suffix that needs to be used for the View pages.
Add the following two properties to the application.properties file of your Spring Boot application.
spring.mvc.view.prefix=/WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
And this is it!
Your application is now configured to support JSP and JSTL.
JSP Page
You can now create a new JSP page and test your application. Create and place the following JSP page into /src/main/webapp/WEB-INF/jsp folder.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Insert title here</title> </head> <body> <%="This is my JSP page"%> </body> </html>
Controller Class
You can use the following Controller class to handle HTTP requests and make it display a JSP page.
import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.GetMapping; @Controller public class HomeController { @RequestMapping("/") public String homePage() { return "home"; } }
The homePage() method will be triggered when we access the root resource of our website http://localhost:8080/. This is because we use the forward-slash / in the RequestMapping() annotation.
When this method completes, it will return the name of the View that needs to be displayed to a user. The name of the view is “home”. When Spring Framework adds Prefix and Suffix to this View, it will be able to locate the home.jsp page that is placed in the /src/main/webapp/WEB-INF/jsp/ folder.
I hope this tutorial was helpful for you. If you are interested in learning more about Spring Web MVC then check other tutorials in the Spring Web MVC category.