Rate list.contains string in JSTL

I need to hide an element if some values โ€‹โ€‹are present in JSP

The values โ€‹โ€‹are stored in a list, so I tried:

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

But that will not work.

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

+72
java jsp jstl
Sep 29 '09 at 1:28
source share
7 answers

Unfortunately, I think JSTL does not support anything but iterate through all the elements to figure this out. I used to use the forEach method in the main tag library:

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

After that, $ {contains} will be "true" if myList contains myValue.

+56
Sep 29 '09 at 1:41
source share

There is no built-in function to verify that: you must write your own tld function, which takes a list and an element, and calls the list contains () method. eg.

 //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"> <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> </taglib> 

Then create a class called Yourclass and add a static method that contains the above signature. I am sure that the implementation of this method is pretty clear:

 package com; // just to illustrate how to represent the package in the tld 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> 

The tag can be used from any JSP on the site.

edit: more information about the tld file - more info here

+90
Sep 29 '09 at 1:45
source share

Another way to do this is to use 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 exists on the map.

+26
Sep 07 2018-10-09T00:
source share

Below is a more workaround than an answer to your question, but that might be what you are looking for. If you can put your values โ€‹โ€‹on a map instead of a list, this will solve your problem. Just match your values โ€‹โ€‹with a non-zero value and do it <c:if test="${mymap.myValue ne null}">style='display:none;'</c:if> , or you can even go to style='display:none; and just print ${mymap.myValue}

+3
Oct 08 '09 at 18:27
source share

If you are using Spring Framework, you can use Spring TagLib and SpEL:

 <%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> --- <spring:eval var="containsValue" expression="mylist.contains(myValue)" /> <c:if test="${containsValue}">style='display:none;'</c:if> 
0
Jul 20 '17 at 15:07
source share

You need to use fn:contains() or fn:containsIgnoreCase() instead.

 <%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%> 

...

 <c:forEach items="${datas}" var="data"> <c:if test="${not fn:containsIgnoreCase(data, 'apple')}"> <p>Doesn't contain 'apple'</p> </c:if> </c:forEach> 
0
Nov 30 '17 at 3:59
source share
 <c:if test="${fn:contains(task.subscribers, customer)}"> 

This works great for me.

-2
Oct 26 '13 at 18:16
source share



All Articles