tags:

views:

4577

answers:

5

I need to hide an element if certain values are present in the JSP

The values are stored in a List so I tried:

<c:if test="${  mylist.contains( myValue ) }">style='display:none;'</c:if>

But, it doesn't work.

How can I evaluate if a list contains a value in JSTL, the list and the values are strings.

+3  A: 

Sadly, I think that JSTL doesn't support anything but an iteration through all elements to figure this out. In the past, I've used the forEach method in the core tag library:

<c:set var="contains" value="false" />
<c:forEach var="item" values="${myList}">
  <c:if test="${item eq myValue}">
    <c:set var="contains" value="true" />
  </c:if>
</c:forEach>

After this runs, ${contains} will be equal to "true" if myList contained myValue.

Kaleb Brasee
works nicely if the list is small. Just realize there is a performance cost to doing it this way.
Chii
Yeah, there would be if you get high enough. I've used it for collections of 10-20 things and have not experienced any performance issues. The thing I think is worse is the number of lines of JSTL. Still, I think this is the only way without setting up your own TLD (which isn't too difficult and may very well be worth it).
Kaleb Brasee
+4  A: 

there is no built-in feature to check that - what you would do is write your own tld function which takes a list and an item, and calls the list's contains() method. e.g.

//in your own WEB-INF/custom-functions.tld file add this
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
        PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"&gt;
<taglib
        xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
        version="2.0"
        >
    <tlib-version>1.0</tlib-version>
<function>
        <name>contains</name>
        <function-class>com.Yourclass</function-class>
        <function-signature>boolean contains(java.util.List,java.lang.Object)</function-signature>
</function>

Then create a class called Yourclass, and add a static method called contains with the above signature. I m sure the implementation of that method is pretty self explanatory:

public class Yourclass {
   public static boolean contains(List list, Object o) {
      return list.contains(o);
   }
}

Then you can use it in your jsp:

<%@ taglib uri="WEB-INF/custom-functions.tld" prefix="fn" %>
<c:if test="${  fn:contains( mylist, myValue ) }">style='display:none;'</c:if>

edit: more info regarding the tld file - more info here

Chii
: - S Where is that code placed? Looks menacing
OscarRyz
ive edited the answer with a bit more info.
Chii
+2  A: 

The following is more of a workaround than an answer to your question but it may be what you are looking for. If you can put your values in a map instead of a list, that would solve your problem. Just map your values to a non null value and do this <c:if test="${mymap.myValue ne null}">style='display:none;'</c:if> or you can even map to style='display:none; and simply output ${mymap.myValue}

svachon
I guess the syntax should be <c:if test="${mymap[myValue] ne null}">style='display:none;'</c:if>Otherwise the variable "myValue" is not evaluated.
Andreas
A: 

You can find this into JSTL functions:

<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%&gt;
<c:if test="${fn:contains(myList, myValue)}">style='display:none;'</c:if>
greut
Oh right, my bad…
greut
fn:contains doesn't work. fn:contains wants 2 string, if you pass a list it will just do a tostring.For example, my list of longs has 102, 109, 108. Do a fn:contains(list, 1) and it will return true, also on 9 or 8.It doesn't throw an error but it can cause a nicely hidden bug
AlfaTeK
A: 

Another way of doing this is using a Map (HashMap) with Key, Value pairs representing your object.

Map<Long, Object> map = new HashMap<Long, Object>();
map.put(new Long(1), "one");
map.put(new Long(2), "two");

In JSTL

<c:if test="${not empty map[1]}">

This should return true if the pair exist in the map

Tamer Salama