我如何在 java web 应用程序的 jsp 中实现 if 语句?

问题描述

我正在做的是实现一个表格来显示即将到来的约会,其中我有一个 if 语句,我希望在其中实施以防止显示旧的而不是当前时间范围内的数据。例如约会前的昨天或几周不应该显示在 jsp 中?

我教了一个 if 语句,现在的代码是:

 <form method="POST" action="<%=request.getcontextpath()%>/Appointment/delete">
                    <c:choose>
                        <c:when test="${empty list}">
                            <h1>No appointments available</h1>
                        </c:when>
                        <c:otherwise>

                            <table class="styled-table" style="margin-bottom: 15px; margin-top:15px;">
                                <thead>
                                    <tr>
                                        <th>Patient Name</th>  
                                        <th>Appointment Date</th>
                                        <th>Delete</th>
                                    </tr>
                                </thead>
                                <c:forEach items="${list}" var="record">
                                    <c:choose>
                                       
                                        <c:when test="${record.date lt} <% LocalDateTime Now = LocalDateTime.Now(); %>">
                                    <tbody>
                                        <tr class="active-row">
                                            <td>${record.name}</td>
                                            <td>${record.date}</td>
                                            <td id="id"><button  class="delete"  value="${record.appointmentId}" ><i class="fas fa-trash-alt"></i></button></a></td>
                                        </tr>
                                    </tbody> 
                                        </c:when>
                                        <c:otherwise>
                                         
                                        </c:otherwise>
                                    </c:choose>
                                </c:forEach>
                            </table >
                        </form>

好吧,我对学习 jsp 和 java web 开发的东西还很陌生,非常感谢帮助完成这项工作。提前致谢。

解决方法

尽管您可以实现这种过滤,但 JSP 并不真正适合在渲染期间进行大量客户端处理。我建议您在 servlet 代码中确定应该显示哪些约会,然后返回这些约会。因此,列表将填充正确的约会以开始

如果您仍想在 JSP 中执行此操作,这里有一些建议。要在 JSP 循环中添加条件,您可以执行以下操作:

<c:forEach items="${list}" var="record">
    <!-- Only show records younger than 7 days -->
    <c:if test="${record.ageInDays lt 7}">
        <tbody>
            <tr class="active-row">
                <td>${record.name}</td>
                <td>${record.date}</td>
                <td id="id"><button  class="delete"  value="${record.appointmentId}" ><i class="fas fa-trash-alt"></i></button></a></td>
            </tr>
        </tbody> 
    </c:if>
</c:forEach>

为此,您应该在 Java 记录类中添加一个方法

public class Appointment {

    private static final long ONE_DAY_IN_MILLIS = 24 * 60 * 60 * 1000;

    //... existing code

    public int getAgeInDays() {
        return (System.currentTimeMillis() - date.getTime()) / ONE_DAY_IN_MILLIS;
    }
}

此方法避免了在 JSP 中进行日期计算。