list JSTL 集合和列表 - 检查集合中是否存在项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1076679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 01:21:52  来源:igfitidea点击:

JSTL Sets and Lists - checking if item exists in a Set

listjstlset

提问by McDowell

I have a Java Set in my session and a variable also in the session. I need to be able to tell if that variable exists in the set.

我的会话中有一个 Java 集,会话中有一个变量。我需要能够判断该变量是否存在于集合中。

I want to use the contains ( Object ) method that Java has for Lists and Sets to test whether that object exists in the set.

我想使用 Java 为 Lists 和 Sets 提供的 contains ( Object ) 方法来测试该对象是否存在于集合中。

Is that possible to do in JSTL? If so, how? :)

在 JSTL 中可以这样做吗?如果是这样,如何?:)

Thanks, Alex

谢谢,亚历克斯

回答by McDowell

You could do this using JSTL tags, but the result is not optimal:

您可以使用 JSTL 标签执行此操作,但结果不是最佳的:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>

<jsp:useBean id="numbers" class="java.util.HashSet" scope="request">
    <%
        numbers.add("one");
        numbers.add("two");
        numbers.add("three");
    %>
</jsp:useBean>

<c:forEach items="${numbers}" var="value">
    <c:if test="${value == 'two'}">
        <c:set var="found" value="true" scope="request" />
    </c:if>
</c:forEach>
${found}

</body>
</html>


A better way would be to use a custom function:

更好的方法是使用自定义函数:

package my.package;
public class Util {

  public static boolean contains(Collection<?> coll, Object o) {
    if (coll == null) return false;
    return coll.contains(o);
  }

}

This is defined in a TLD file ROOT/WEB-INF/tag/custom.tld:

这是在 TLD 文件ROOT/WEB-INF/tag/custom.tld 中定义的:

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
  version="2.1">
  <tlib-version>1.0</tlib-version>
    <short-name>myfn</short-name>
    <uri>http://samplefn</uri>
    <function>
      <name>contains</name>
      <function-class>my.package.Util</function-class>
      <function-signature>boolean contains(java.util.Collection,
          java.lang.Object)</function-signature>
  </function>
</taglib>

The function can then be imported into your JSPs:

然后可以将该函数导入到您的 JSP 中:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="myfn" uri="http://samplefn"%>
<html>
<body>

<jsp:useBean id="numbers" class="java.util.HashSet" scope="request">
    <%
        numbers.add("one");
        numbers.add("two");
        numbers.add("three");
    %>
</jsp:useBean>

${myfn:contains(numbers, 'one')}
${myfn:contains(numbers, 'zero')}

</body>
</html>


The next version of EL (due in JEE6) should allow the more direct form:

EL 的下一个版本(在 JEE6 中到期)应该允许更直接的形式:

${numbers.contains('two')}

回答by xxg

If you are using Spring Framework, you can use Spring TagLib and SpEL:

如果您使用的是 Spring Framework,则可以使用 Spring TagLib 和 SpEL:

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
---
<spring:eval var="containsValue" expression="yourList.contains(yourValue)" />

Contains (true or false): ${containsValue}