The dsp:contains
tag checks whether a certain value is contained within a Collection and saves the Boolean result to the named EL variable. 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, you define a subclass of java.util.Comparator
that identifies the characteristics you want compared.
To see if an EJB exists in a Collection of EJBs, use dsp:containsEJB
.
Attributes
TagVariable:varorid(Required)
You must define either EL variable or scripting variable:
Attribute | Description |
---|---|
| Names an EL variable. When you use |
| Names a scripting variable, which scriptlets and expressions can access at runtime. |
VariableScope:scope
The scope
attribute determines what resources can access the named EL variable. Options include: page
, request
, session
, or application
. Omitting this attribute causes the default, page
, to be used.
ValueCriteria:Boolean,byte,char,double,float,int,long,object,orshort(Required)
This attribute holds the value on which the comparison is based. The dsp:contains
tag determines whether there is a match between the data held in this attribute and an entry in the Container held by the values
attribute. Use the attribute identified here that matches the data type it contains:
boolean
byte
char
double
float
int
long
object
short
ItemtoBeSearched:values(Required)
The values
attribute holds the Collection in which you want to search for a specific value. This attribute permits items of the following data types: primitive array, Object array, Enumeration, Iterator, Collection and Map.
PortionofaMaptoSearch:compareElement
When the values
attribute is set to a Map, the compareElement
determines what is evaluated: the keys (false) or the values (true). The default behavior is to search all keys in a Map (false).
InstanceofComparatorObject:comparator
The comparator
attribute is set to an instance of java.util.Comparator
that you define. You use a comparator to compare specific portions of one element to another. When you do not use a comparator, the entire first item is compared to a portion of the second.
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 a user ID matches one of the enrolled student IDs, the user’s first name is retrieved and a message is displayed that reads, Welcome
User
Name
. You're enrolled in Math 101
!