使用jstl比较jsp中的日期对象

时间:2014-02-21 13:03:34

标签: java jsp spring-mvc jstl

我有两个POJO,其中包含日期类型字段。 我正在使用Spring MVC并通过ModelAndView传递这两个POJO。 我可以使用compareTo()在控制器中比较这些日期。 我想在JSP页面中比较它们以隐藏或显示某些td。

在控制器中,这有效:

for (AttendanceReport attendanceR2 : pastAttendance) {
    System.out.println("For date: "+ attendanceR2.getTrainingDate());

    for (Attendance attendance2 : absentCandidates) {
        if (attendance2.getDateOfAbsentee().compareTo(attendanceR2.getDateOfTraining())==0) {
            System.out.println("\n");
            System.out.println(attendance2.getCandidate().getCandidateId() + "---- absent on--- "+ attendance2.getDateOfAbsentee());
        }
    }
}

我想在JSP中进行这种比较...... 没有任何scriptlet ...... !! 怎么能实现这个...... !!

我从控制器设置对象如下:

modelAndView.addObject("absentCandidates",absentCandidates);
modelAndView.addObject("pastAttendance",pastAttendance);

我试过这是我的JSP页面......但它不起作用......

<c:forEach items="${pastAttendance}" var="pastAttendance">
<c:forEach items="${absentCandidates}" var="absentCandidates">
   <c:if test="${pastAttendance.getDateOfTraining==absentCandidates.getDateOfAbsentee}">
    <td>Some Check Box Unchecked</td>
   </c:if>
</c:forEach>
</c:forEach>

dateOfTraining和dateOfAbsentee,两者都是类型日期的字段......他们只是没有在JSTL中进行比较......

1 个答案:

答案 0 :(得分:0)

通常,您在servlet中执行业务逻辑。在servlet中,您准备一个表示要有条件构建的表的数据模型,然后在JSP中使用JSTL循环生成的数据模型以生成所需的视图。

或者,您可以在POJO bean类的简单getter属性中构建数据模型,这样您就不需要servlet来完成准备工作。

相关问题