我有list一些包含一些对象.对象有一个hours字段.
在<c:foreach>我迭代列表并获取对象.
现在我想总结hours一个totalHours变量中所有迭代对象的字段.
我的代码:
<c:forEach var="attendance" items="${list }" varStatus="rowCounter1">
<tr>
<td><c:out value="${rowCounter1.count}"></c:out></td>
<td><c:out value="${attendance.date }"></c:out></td>
<td><c:out value="${attendance.inTime }"></c:out></td>
<td><c:out value="${attendance.outTime }"></c:out></td>
<td><c:out value="${attendance.interval }"></c:out></td>
<c:set var="totalHours" value="${attendance.Hours += attendance.Hours }"
target="${attendance}"</c:set>
</tr>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)
我试过这个,但它给了我以下错误:
javax.el.ELException: Failed to parse the expression [${attendance.Hours += attendance.Hours }
Run Code Online (Sandbox Code Playgroud)
JB *_*zet 24
在Java中,它看起来像这样:
// before the loop:
int totalHours = 0;
for (Attendance attendance : list) {
totalHours = totalHours + attendance.getHours();
}
Run Code Online (Sandbox Code Playgroud)
在JSTL中也这样做:
<c:set var="totalHours" value="${0}"/>
<c:forEach var="attendance" items="${list }" varStatus="rowCounter1">
...
<c:set var="totalHours" value="${totalHours + attendance.hours}"/>
</c:forEach>
Run Code Online (Sandbox Code Playgroud)