I have list
which contains some objects. The objects have an hours
field.
我有一个包含一些对象的列表。对象有一个小时字段。
In the <c:foreach>
I am iterating the list and fetching the objects.
在
Now I want to sum up the hours
field of all the iterated objects in a totalHours
variable.
现在我要总结一下totalHours变量中所有迭代对象的时域。
My code:
我的代码:
<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>
I was trying this, but it gave me the following error:
我尝试了这个,但是它给了我以下的错误:
javax.el.ELException: Failed to parse the expression [${attendance.Hours += attendance.Hours }
1 个解决方案
#1
20
In Java, it would look like this:
在Java中,它是这样的:
// before the loop:
int totalHours = 0;
for (Attendance attendance : list) {
totalHours = totalHours + attendance.getHours();
}
So do the same in JSTL:
在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>
#1
20
In Java, it would look like this:
在Java中,它是这样的:
// before the loop:
int totalHours = 0;
for (Attendance attendance : list) {
totalHours = totalHours + attendance.getHours();
}
So do the same in JSTL:
在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>