JSTL Integration
As part of this tutorial, we are going to integrate the Spring Boot application with JSTL tag library. JSTL also called as Jakarta Standard Tag Library to simplify and rapid product development.
JSTL provides five types of tags:
- Core tags is the widely used tags for used for iteration, condition checking, URL management etc. Tag library URL is http://java.sun.com/jsp/jstl/core with prefix as c.
- Function tags for string manipulation and string length. Tag library URL is http://java.sun.com/jsp/jstl/functions with prefix as fn.
- Formatting tags for message formatting and number formatting. Tag library URL is http://java.sun.com/jsp/jstl/fmt and prefix as fmt.
- XML tags mainly used for flow control, transformation, etc. Tag library URL is http://java.sun.com/jsp/jstl/xml and prefix as x.
- SQL tags for SQL support. Tag library URL is http://java.sun.com/jsp/jstl/sql and prefix as sql.
Below is the Spring Boot project code structure which we are going to build as part of this tutorial. As part of current discussion, we will be making changes to below highlighted packages and files red in color.
As part of this article we will be using Spring tool kit and to download and setup Spring Tool Kit please click on this link.
Follow below steps to create our very first Spring application project.
- Open Spring Tool kit we installed here.
- Click on File in top navigation menu and than click on new. There will be an option Spring Starter Project start appearing once we click on create new option.
Below window will start appearing.
Provide below highlighted value for Group and Artifact for the project and click on Next.
3. In the below screen select open Web option and select Spring Web. Click on Finish will create Spring project.
4. Add below highlighted dependency for JSTL to work in application.
5. Create User.java class bean having id, name and city as a variable with setter getter and parameterized constructor.
6. Create UserController.java class as mentioned in below screenshot t0 handle http://localhost:8080/users request and display list of users.
7. Create below hierarchy to place jsp files inside src/main/resources folder as shown below.
8. Place below content inside user.jsp file.
Include JSTL tag library using below statement:
<%@ taglib prefix=”c” uri=”http://java.sun.com/jsp/jstl/core” %>
<c:forEach> and <c:if> are the JSTL tags. <c:forEach> tag is to iterate through list and <c:if> to check condition.
For more info on JSTL tags please go through this link.
9. Below code inside application.properties file is required to inform spring framework to locate view files and its extension.
Note: Complete path is not required to give as prefix /src/main/resources/META-INF/resources/WEB-INF/jsp/hello.jsp
Spring understands /src/main/resources/META-INF/resources as a default path.
10. Right click on the project and run it as Spring Boot App. Hit http://localhost:8080/users url with specified port number in console once server is up.
Imran Khan, Adobe Community Advisor, AEM certified developer and Java Geek, is an experienced AEM developer with over 11 years of expertise in designing and implementing robust web applications. He leverages Adobe Experience Manager, Analytics, and Target to create dynamic digital experiences. Imran possesses extensive expertise in J2EE, Sightly, Struts 2.0, Spring, Hibernate, JPA, React, HTML, jQuery, and JavaScript.