Missing membership checking functionality in a collection

Why did the designers of the JSTL EL decided to not include such an important functionality. There are many cases where I want to do something different if a specific element is present in a collection. Simply being able to iterate over is not enough.

Folks in this forum sugested to refactor the code to use a Map instead of a List/Set, then check for map["myElement"]. This works, but only if your values in the collection happen to be simple strings. Most of the time values in my map are objects.

Any thoughts?

[535 byte] By [vijaychhipaa] at [2007-10-2 0:51:28]
# 1

Iam interested as I have had similar problems. Basically I set out to use jstl to check if a certain attribute exists in session and this is what I came up with,

<%

java.util.Enumeration en = session.getAttributeNames();

pageContext.setAttribute("en",en);%>

<c:forEach var="attr" items="${en}">

<c:if test = "${attr eq value}">

yes..process

</c:if>

</c:forEach>

Forgive my stupidity if there is an easy way around.

I have another complaint here ::: I have read that jstl looks for a 'get' method. It doesnt access the variable directly. However it looks like it requires both the variable and the associated get() method for it to work, because

<c:forEach var="attr" items="${session.attributeNames}">

in the above doesnt work. Iam forced to use a scriptlet. I guess this is because there is no Enumeration variable named 'attributeNames' in the HttpSession class. However my thinking was that as long as it is an instance method, why should jstl be bothered if the value is stored in an instance variable.

Last but not the least, how does conversion of a List/Set to Map help ?

ram.

Madathil_Prasada at 2007-7-15 18:11:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 2

>Why did the designers of the JSTL EL decided to not include such an important functionality. There are many cases where I want to do something different if a specific element is present in a collection.

Maybe because its not clean programming to include different types of Objects in one collection ?

pgeuensa at 2007-7-15 18:11:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...
# 3

> >Why did the designers of the JSTL EL decided to not

> include such an important functionality. There are

> many cases where I want to do something different if

> a specific element is present in a collection.

>

> Maybe because its not clean programming to include

> different types of Objects in one collection ?

I forgot to mention in my previous post. Jstl relies on reflection to dynamically access data in which case its reasonable to expect it to work only for getXXX() kind of methods.

The functionality you are seeking here is contains(Object obj) which I dont think can be invoked via jstl. It shouldnt be too hard to write a function that does this work though.

ram.

Madathil_Prasada at 2007-7-15 18:11:00 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...