How to calculate the total amount in JSTL

How can I implement this with JSP and JSTL?

int total = 0; for (Article article : list) { total += article.price; } 
+5
source share
1 answer

Use <c:set> to initialize the shared variable, use <c:forEach> to iterate over the list, and use another <c:set> to add the iterated value to the final value.

 <c:set var="total" value="${0}"/> <c:forEach var="article" items="${list}"> <c:set var="total" value="${total + article.price}" /> </c:forEach> 

See also Iterate over list and map elements using the JSTL <c: forEach> tag .

+8
source

Source: https://habr.com/ru/post/1208248/


All Articles