参见英文答案 Calculate total sum of all numbers in c:forEach loop7个 如何通过JSP和JSTL实现这一点? int total = 0;for (Article article : list) { total += article.price;} 使用 c:set要初始化总变量,请使用 c:for
如何通过JSP和JSTL实现这一点?
int total = 0;
for (Article article : list) {
total += article.price;
}
使用< c:set>要初始化总变量,请使用< c:forEach>迭代列表并使用另一个< c:set>将迭代值添加到总计中.
<c:set var="total" value="${0}"/>
<c:forEach var="article" items="${list}">
<c:set var="total" value="${total + article.price}" />
</c:forEach>
另见Iterate over elements of List and Map using JSTL <c:forEach> tag.
