[英]How can i implement an if statement in a jsp in java web app?
What I am working is to implement a table to show the upcoming appointments, where i have a if statement in which I am looking to implement to prevent showing data that's old and not in current time frame.我正在做的是实现一个表格来显示即将到来的约会,我有一个 if 语句,我希望在其中实现以防止显示旧的且不在当前时间范围内的数据。 For example yesterdays or weeks before appointments shouldn't be shown in the jsp?
例如约会前的昨天或几周不应该显示在 jsp 中?
I taught of an if statement, well the code currently is:我教过一个 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>
Well i am quite new to learning jsp and java web development stuff would really appreciate help in getting this work.好吧,我对学习 jsp 和 java web 开发人员非常感激,非常感谢您对这项工作的帮助。 Thanks in advance.
提前致谢。
Although you can implement this filtering, JSP's are not really suited to do a lot of client-side processing during rendering.尽管您可以实现此过滤,但 JSP 并不真正适合在呈现期间进行大量客户端处理。 I would suggest you determine in your servlet code which appointments should be shown, and return those.
我建议您在您的 servlet 代码中确定应显示哪些约会,然后返回这些约会。 So the list will be populated with the correct appointments to begin with
因此,列表将填充正确的约会开始
If you still want to do this in the JSP, here are some suggestions.如果您仍想在 JSP 中执行此操作,这里有一些建议。 To add a condition within your JSP loop you could do something like this:
要在 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>
For this to work you should add a method in your Java record class为此,您应该在 Java 记录 class 中添加一个方法
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;
}
}
This method avoids having to do date calculations within your JSP.这种方法避免了在 JSP 中进行日期计算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.