Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You could do this using JSTL tags, but the result is not optimal:</p> <pre><code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt; &lt;html&gt; &lt;body&gt; &lt;jsp:useBean id="numbers" class="java.util.HashSet" scope="request"&gt; &lt;% numbers.add("one"); numbers.add("two"); numbers.add("three"); %&gt; &lt;/jsp:useBean&gt; &lt;c:forEach items="${numbers}" var="value"&gt; &lt;c:if test="${value == 'two'}"&gt; &lt;c:set var="found" value="true" scope="request" /&gt; &lt;/c:if&gt; &lt;/c:forEach&gt; ${found} &lt;/body&gt; &lt;/html&gt; </code></pre> <hr> <p>A better way would be to use a custom function:</p> <pre><code>package my.package; public class Util { public static boolean contains(Collection&lt;?&gt; coll, Object o) { if (coll == null) return false; return coll.contains(o); } } </code></pre> <p>This is defined in a TLD file <em>ROOT</em>/WEB-INF/tag/custom.tld:</p> <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt; &lt;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"&gt; &lt;tlib-version&gt;1.0&lt;/tlib-version&gt; &lt;short-name&gt;myfn&lt;/short-name&gt; &lt;uri&gt;http://samplefn&lt;/uri&gt; &lt;function&gt; &lt;name&gt;contains&lt;/name&gt; &lt;function-class&gt;my.package.Util&lt;/function-class&gt; &lt;function-signature&gt;boolean contains(java.util.Collection, java.lang.Object)&lt;/function-signature&gt; &lt;/function&gt; &lt;/taglib&gt; </code></pre> <p>The function can then be imported into your JSPs:</p> <pre><code>&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%&gt; &lt;%@ taglib prefix="myfn" uri="http://samplefn"%&gt; &lt;html&gt; &lt;body&gt; &lt;jsp:useBean id="numbers" class="java.util.HashSet" scope="request"&gt; &lt;% numbers.add("one"); numbers.add("two"); numbers.add("three"); %&gt; &lt;/jsp:useBean&gt; ${myfn:contains(numbers, 'one')} ${myfn:contains(numbers, 'zero')} &lt;/body&gt; &lt;/html&gt; </code></pre> <hr> <p>The next version of EL (due in JEE6) should allow the more direct form:</p> <pre><code>${numbers.contains('two')} </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload