How to calculate the total of a sum in JSTL

foreach, jsp, jstl, sum

Solution

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.

Problem

How can I realise this with JSP and JSTL? ``` int total = 0; for (Article article : list) { total += article.price; } ```

Original source

Related problems