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.

4
  • Here is another question that may help you stackoverflow.com/questions/5935892/if-else-within-jsp-or-jstl Commented Apr 15, 2021 at 14:51
  • Well, you're already using JSTL (e.g. <c:choose> etc.) so a <c:if> should also be available. Commented Apr 15, 2021 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). Commented Apr 15, 2021 at 14:54
  • Thank you all appreciate the help Commented Apr 16, 2021 at 11:42

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you so much for this it means a lot , it definitely helped.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.