How to calculate the total of a sum in JSTL

31,063

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

<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 elements of List and Map using JSTL <c:forEach> tag.

Share:
31,063
Alex
Author by

Alex

Updated on July 09, 2022

Comments

  • Alex
    Alex almost 2 years

    How can I realise this with JSP and JSTL?

    int total = 0;
    for (Article article : list) {
        total += article.price;
    }