日期正在打印,但未能在jstl中评估为空 [英] Date is printing but failed to evaluate as not empty in jstl
问题描述
我使用JSTL在我的JSP中迭代Spring模型值。
我使用JSTL在JSP中显示两个日期。
以下是我的Java代码:
model.put(oldest,timesheetService.getOldestPendingTimesheet(userDetails));
model.put(pending,timesheetService.getNextOldestPendingTimesheet(userDetails));
JSP代码:
< c:选择>
< c:when test =$ {not empty model.oldest}>
< c:out value =$ {model.oldest}/>
< / c:当>
< c:out value =$ {model.pending}/>
< / c:当>
我在之外显示两个日期c:choose
它打印如下
最早:Tue Nov 04 00:00:00 IST 2014
待定:太阳Nov 09 00:00:00 IST 2014
最旧日期打印在 c:当
时,但挂起仅在 c:选择
之外打印,并且不打印在 c中:
。
上面的代码有什么问题?
这是因为当< c:choose>
中有多个< c:when>
时,第一个被解释为,如果
,其他所有其他的被解释为 else if
。所以,你的第一个条件是满足的,当被忽略时,第二个
如果你想要打印它们两个取决于非排他性条件,最好使用< c:if>
。
<$ p $,比< c:choose>
构造更容易阅读。
< c:if test =$ {not empty model.oldest}>
< c:out value =$ {model.oldest}/>
< / c:if>
< c:out value =$ {model.pending}/>
< / c:if>
I am using JSTL to iterate Spring 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 the c:choose
it 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
, but pending is printing only outside the c:choose
and not printing inside the c:when
.
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>
这篇关于日期正在打印,但未能在jstl中评估为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!