code

list.contains JSTL의 문자열 평가

codestyles 2020. 9. 18. 08:13
반응형

list.contains JSTL의 문자열 평가


JSP에 특정 값이있는 경우 요소를 숨겨야합니다.

값은 목록에 저장되므로 시도했습니다.

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

그러나 작동하지 않습니다.

목록에 JSTL의 값이 포함되어 있는지, 목록과 값이 문자열인지 어떻게 평가할 수 있습니까?


슬프게도 JSTL은 이것을 알아 내기 위해 모든 요소를 ​​반복하는 것 외에는 아무것도 지원하지 않는다고 생각합니다. 과거에는 핵심 태그 라이브러리에서 forEach 메서드를 사용했습니다.

<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>

이 실행 후 myList에 myValue가 포함 된 경우 $ {contains}는 "true"와 같습니다.


이를 확인할 내장 기능이 없습니다. 목록과 항목을 가져 와서 목록의 contains () 메서드를 호출하는 자체 tld 함수를 작성하면됩니다. 예 :

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

그런 다음 Yourclass라는 클래스를 만들고 위의 서명을 사용하여 contains라는 정적 메서드를 추가합니다. 나는 그 방법의 구현이 꽤 자명하다고 확신합니다.

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);
   }
}

그런 다음 jsp에서 사용할 수 있습니다.

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

태그는 사이트의 모든 JSP에서 사용할 수 있습니다.

편집 : tld 파일에 관한 추가 정보 - 여기에 추가 정보


이를 수행하는 또 다른 방법은 Map (HashMap)객체를 나타내는 with Key, Value 쌍을 사용하는 것입니다.

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

JSTL에서

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

쌍이 맵에 있으면 true를 반환해야합니다.


fn:contains()또는 fn:containsIgnoreCase()함수 를 사용해야합니다 .

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

...

 <c:if test="${not fn:containsIgnoreCase(mylist, 'apple')}">
        <p>Doesn't contain 'apple'</p>
    </c:if>

또는

<c:if test="${not fn:contains(mylist, 'Apple')}">
            <p>Contains 'Apple'</p>
        </c:if>

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}


${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>

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>

<c:if test="${fn:contains(task.subscribers, customer)}">

This works fine for me.

참고URL : https://stackoverflow.com/questions/1490139/evaluate-list-contains-string-in-jstl

반응형