简体   繁体   English

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

[英]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.

相关问题 如何使用twitter为我的jsp / java web项目实现登录 - how can i implement login with twitter for my jsp/java web project 如何在Java Web应用程序中创建可以从jsp中的表达式语言调用的函数? - How can I create my own functions that I can call from Expression Language in jsp in a Java web app? Java 8,如何使用流实现switch语句? - Java 8, how can I implement a switch statement using streams? 如何在Java中用Excel实现类似于Web查询的功能? - How can I implement similar to the web query feature in Excel in java? 如何在不重复代码的情况下在Java Web应用程序中包含jsp页面? - How do I include jsp pages in a Java web app without duplicating code? 我如何在Intranet服务器中部署Web应用程序(带有JSP页面) - how can i Deploy my web app (with JSP pages) in a intranet server 如何从 JAVA 中的其他 JSP 重定向到 JSP - how can I redirect to a JSP from other JSP in JAVA 如何使用 JSP 2 避免 JSP 文件中的 Java 代码? - How can I avoid Java code in JSP files, using JSP 2? 如何在Java Web应用程序中从JSP引用JavaScript库? - How can I reference a javascript library from a JSP in a java web application? 如何在Java版本的Google App Engine中动态更新JSP文件的内容? - How can I dynamically update the contents of a JSP file in the Java version of Google App Engine?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM