java - 正在打印日期,但在 jSTL 中无法评估为非空

标签 java spring hibernate jsp jstl

我正在使用 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/

相关文章:

java - 拦截 Rest Controller 响应

java - Spring单例bean是线程安全的吗?

sql - 连接具有不同连接列名称的两个表

java - 通过java反射从Field中访问整型数组

java - Spring Util :Properties Injection via Annotations into a bean

java - 部分有序比较器到全序比较器

java - Spring Security getPrincipal() 方法返回 anonymousUser

java - "SELECT... WHERE column > ?1"- 那是什么?

java - @OneToMany(EAGER) 与@LazyCollection(FALSE)&OneToMany(LAZY)

java - Hibernate:如何从查询结果中获取属性?