现在无论我是否设置记录属性,都会显示删除按钮:

<c:if test='${not empty "${records}"}'>
           <input type="submit" name="delete" value="delete selected"/>
    </c:if>

在正常情况下,记录属性我传递ArrayList,然后使用foreach,但有时ArrayList 是空的,所以在这些情况下我不想显示删除按钮,我认为实现这一点的最简单方法是使用这个空运算符。我哪里出错了?

我什至尝试手动将此属性设置为 null:

if (ar.size() != 0)
    request.setAttribute("records", ar);
else
    request.setAttribute("records",null);

编辑: @Qwe:是的,你是对的,它之前对我有用,因为我以我的方式测试了属性是否为空,它总是正确的,因为我使用了错误的构造,但它有效,因为我只想显示一个字符串,如果有的话没有任何字符串,所以我认为一切正常。

I am having a problem with JSTL and empty operator. I already made few simple pages and everything worked fine, but now I have:

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    <html>
    <body>

     <form action="/Projekt/myaccount" method="post">
    <table border="1">
        <tr>
                 <td>Artist</td>
                 <td>Record Name</td>
                 <td>Delete</td>
         </tr>
        <c:forEach var="item" items="${records}">
         <tr>
                 <td>${item.artist}</td>
                 <td>${item.recordName}</td>
                 <td>
                    <input type="checkbox" name='${item.recordName}|${item.recordName}'/>
                 </td>
         </tr>
        </c:forEach>
    </table>
    <hr/>
        <input type="submit" name="back" value="back"/>
        <c:if test='${not empty "${records}"}'>
               <input type="submit" name="delete" value="delete selected"/>
        </c:if>
     </form>
    </body>
    </html>

now no matter if I set the records attribute or not, the delete button shows up:

<c:if test='${not empty "${records}"}'>
           <input type="submit" name="delete" value="delete selected"/>
    </c:if>

in normal situation to records attribute I pass ArrayList and then use foreach, but sometimes ArrayList is empty, so in those situations I don't want delete button to show up, I fought that easiest way to achieve this would be to use this empty operator. Where am I making a mistake?

I even tried to manually set this attribute to null:

if (ar.size() != 0)
    request.setAttribute("records", ar);
else
    request.setAttribute("records",null);

EDIT:
@Qwe: yes you are right, it worked for me before because I tested if attribute was empty in my way, it was always true, because I used wrong construct, but it worked because I just wanted to show one string, if there was no String nothing showed up so I was thinking that everything worked fine.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伪装你 2025-01-11 06:59:43

以及 (来自您的评论)将始终解析为true,因为您实际上正在测试字符串 ${记录} 是否为空,以及显然不是。

只是为了确定 - String ${records} 我指的是一个 String 值,就像您在 Java 中分配它一样 String foo = "${records}";

下一行代码将测试 records 变量(从页面、请求、会话或应用程序范围查找)是否为空:

<c:if test="${not empty records}">

该行代码 100% 保证可以工作:)

另外, request.setAttribute("records",null) 是删除属性的不好方法,因为 empty 不仅测试请求范围,还测试页面、会话等。使用 而是。

<c:if test='${not empty "${records}"}'> as well as <c:if test="${!empty '${showWarning}'}"> (from your comment) will always resolve to true because you're are actually testing if a String ${records} is empty or not, and obviously it is not.

Just to be sure - by String ${records} I mean a String value, just as if you were assigning it in Java like String foo = "${records}";.

The next line of code will test if records variable (that is looked up from page, request, session or application scope) is empty or not:

<c:if test="${not empty records}">

The line of code is 100% guaranteed to work :)

Also, request.setAttribute("records",null) is a bad way to remove attributes because empty tests not just request scope, but page, session etc. Use <c:remove var='records'/> instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文