Evaluate list.contains string in JSTL

后端 未结 10 1168
青春惊慌失措
青春惊慌失措 2020-12-07 14:08

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

The values are stored in a List so I tried:



        
相关标签:
10条回答
  • 2020-12-07 14:31

    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">
    <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 called contains with the above signature. I m sure the implementation of that method is pretty self explanatory:

    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 in the site.

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

    0 讨论(0)
  • 2020-12-07 14:36

    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 讨论(0)
  • 2020-12-07 14:42
    <c:if test="${fn:contains(task.subscribers, customer)}">
    

    This works fine for me.

    0 讨论(0)
  • 2020-12-07 14:47

    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" items="${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.

    0 讨论(0)
  • 2020-12-07 14:49
    ${fn:contains({1,2,4,8}, 2)}
    

    OR

      <c:if test = "${fn:contains(theString, 'test')}">
         <p>Found test string<p>
      </c:if>
    
      <c:if test = "${fn:contains(theString, 'TEST')}">
         <p>Found TEST string<p>
      </c:if>
    
    0 讨论(0)
  • 2020-12-07 14:50

    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

    0 讨论(0)
提交回复
热议问题