在JSP使用EL中判斷指定元素是否存在於指定集合中
1、問題描述
在JSP頁面中使用EL表達式判斷一個指定元素是否存在於指定集合中?
2、問題解決
eg:指定集合:collection:{1,2,3,4,45,6,7,},指定元素:4
<!-- set集合,存儲測試值存在於集合否? -->
<c:set var="iscontain" value="false" />
<!-- 原始集合:items,集合元素 :var-->
<c:forEach items="${collection}" var="element">
<!-- 判斷測試值是否存在於集合中,存在將iscontain置為true -->
<c:if test="${element eq 4}">
<c:set var="iscontain" value="true" />
</c:if>
</c:forEach>
<!-- 測試值在集合中 -->
<c:if test="${iscontain}">如果指定元素在指定集合中執行的代碼</c:if>
<!-- 測試值不在集合中 -->
<c:if test="${!iscontain}">如果指定元素不在指定集合中執行的代碼</c:if>