You are currently viewing Spring Boot Masterclass – Display the list of expense in JSP 08

Spring Boot Masterclass – Display the list of expense in JSP 08

Hello, in this post we will display the list of expenses in the JSP view template. Let’s begin. First, we need to add the dependency for the JSTL tag library in pom.xml

pom.xml

<dependency>
	<groupId>javax.servlet</groupId>
	<artifactId>jstl</artifactId>
</dependency>




Next, add a list of expenses to the model and view object in order to access it in JSP view template

MasterController.java

@RequestMapping("/")
public ModelAndView home() {
	ModelAndView mav = new ModelAndView("home");
	mav.addObject("message", "List of expenses");
	List<Expense> expenses = expenseService.findAll();
	mav.addObject("expenses", expenses);
	return mav;
}

Next, add a reference to the JSTL tag library inside the view template and loop through the list of expenses using the JSTL forEach tag.

home.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	
	<h1>Expense Tracker</h1>
	<p>${message}</p>
	
	<c:forEach var="expense" items="${expenses}">
		
		<div>
			<h3>${expense.expensename}</h3>
			<p>&#8377;${expense.amount}</p>
		</div>
		
	</c:forEach>
	
</body>
</html>

That’s it for this post. This ends the feature 1 for Expense Tracker. I will see you in the next feature. Let me know in the comment section if you have any queries anything on this.




 

Thanks
Bushan SC

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply