I am using JSTL to iterate Spring's model values in my JSP.
I display the two dates in my JSP using JSTL.
Below is my Java code:
model.put("oldest",timesheetService.getOldestPendingTimesheet(userDetails));
model.put("pending", timesheetService.getNextOldestPendingTimesheet(userDetails));
JSP Code:
<c:choose>
<c:when test="${not empty model.oldest}">
<c:out value="${model.oldest}"/>
</c:when>
<c:when test="${not empty model.pending}">
<c:out value="${model.pending}"/>
</c:when>
</c:choose>
I display the both dates in outside of the <c:choose>
tag. It's printing like below:
oldest:Tue Nov 04 00:00:00 IST 2014
Pending:Sun Nov 09 00:00:00 IST 2014
Oldest Date
is printing inside the <c:when>
tag, but bpending
is printing only outside of the <c:choose>
tag and not printing inside the <c:when>
tag.
What's wrong with the above code?
That's because when you have several <c:when>
in <c:choose>
, the first one is interpreted as if
and all the others as else if
. So, your first condition is satisfied and the second when
is ignored.
If you want to print both of them depending on non-exclusive conditions, better use <c:if>
. It is also easier to read than <c:choose>
construct, when you have few conditions.
<c:if test="${not empty model.oldest}">
<c:out value="${model.oldest}"/>
</c:if>
<c:if test="${not empty model.pending}">
<c:out value="${model.pending}"/>
</c:if>