我正在使用 JSTL 迭代 JSP 中的 Spring 模型值。 我使用 JSTL 在 JSP 中显示这两个日期。
下面是我的Java代码:
model.put("oldest",timesheetService.getOldestPendingTimesheet(userDetails));
model.put("pending", timesheetService.getNextOldestPendingTimesheet(userDetails));
JSP代码:
<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>
我将这两个日期显示在 c:choose
之外它打印如下
oldest:Tue Nov 04 00:00:00 IST 2014
Pending:Sun Nov 09 00:00:00 IST 2014
最早的日期打印在 c:when
内,但待定仅在 c:choose
之外打印并且不在 c:when
内打印.
上面的代码有什么问题吗?
最佳答案
那是因为当你有几个<c:when>
时在<c:choose>
,第一个被解释为 if
以及所有其他的 else if
。因此,您的第一个条件已满足,第二个条件 when
被忽略。
如果你想根据非排他条件打印两者,最好使用<c:if>
。它也比<c:choose>
更容易阅读。当你的条件很少时,就可以构建。
<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>
关于java - 正在打印日期,但在 jSTL 中无法评估为非空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27037599/