Checks whether a Collection contains the specified value.

<dsp:contains var-spec
   values="collection"
   type="search-value"
   [compareElement="{true|false}"]
   [comparator="comparator-obj"]
/>

Attributes

var-spec

An EL variable or scripting variable that captures the Boolean result of the operation, defined with one of these attributes:

Attribute

Description

var

Names an EL variable. When you use var, you can set its scope attribute to page, request, session, or application. The default scope is page. For more information, see EL Variable Scopes in Chapter 2.

id

Names a scripting variable that scriptlets and expressions can access at runtime.

values

Specifies the Collection to search. This attribute can specify items of the following data types: primitive array, Object array, Enumeration, Iterator, Collection, and Map.

type

Identifies the data type of the search value, one of the following:

boolean
byte
char
double
float
int
long
object
short

Set this attribute to the search value.

compareElement

If the values attribute is set to a Map, compareElement specifies how to evaluate the Map contents:

comparator

Set to an instance of java.util.Comparator that you define. Use a comparator to compare specific portions of one element to another. If no comparator is specified, the entire first item is compared to a portion of the second.

Usage Notes

dsp:contains checks whether a given value is contained within the specified Collection . When you use this tag, you set the single value to an attribute named for that value’s data type. You then specify the Collection that you want to search in the values attribute.

Alternatively, you can construct a complex operator that compares some portion of the two items. To do this, define a subclass of java.util.Comparator that identifies the characteristics to compare.

To find an EJB in a Collection of EJBs, use dsp:containsEJB.

Example

<dsp:tomap bean="Math101Students" var="students"/>
<dsp:tomap bean="Profile" var="profile"/>

<dsp:contains var="enrolled" values="${students}" object="${profile.getId}"/>
    <c:choose>
       <c:when test="${enrolled}">
           Welcome, <c:out value="${profile.firstName}"/>.  You're
           enrolled in Math 101!
       </c:when>
    </c:choose>

In this example, if the specified user profile ID matches one of the IDs in students, the user’s first name is retrieved and the specified message displays.

 
loading table of contents...