0

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. For example yesterdays or weeks before appointments shouldn't be shown in the jsp ?

I taught of an if statement , well the code currently is :

 <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 . Thanks in advance.

  • Here is another question that may help you https://stackoverflow.com/questions/5935892/if-else-within-jsp-or-jstl – DLynch Apr 15 '21 at 14:51
  • Well, you're already using JSTL (e.g. `` etc.) so a `` should also be available. – Thomas Apr 15 '21 at 14:52
  • Note that `<% LocalDateTime now = LocalDateTime.now(); %>` would not work within an expression. Try something like `${record.date lt java.time.LocalDateTime.now()}` (don't remember the exact Java EL syntax atm). – Thomas Apr 15 '21 at 14:54
  • Thank you all appreciate the help – Ryan anjelo Apr 16 '21 at 11:42

1 Answers1

0

Although you can implement this filtering, JSP's are not really suited to do a lot of client-side processing during rendering. I would suggest you determine in your servlet code which appointments should be shown, and return those. 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. To add a condition within your JSP loop you could do something like this:

<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

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.

Adriaan Koster
  • 15,870
  • 5
  • 45
  • 60