在JSP使用EL和JSTL判断指定元素是否存在于指定集合中

来源:互联网 发布:linux下运行nodejs 编辑:程序博客网 时间:2024/05/29 15:25

判断某一元素是否在一个集合中

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


JSTL标签
<c:set>用于保存数据

<c:if>

与我们在一般程序中用的if一样

<c:forEach>基础迭代标签,接受多种集合类型

用到项目中,select标签,显示合作单位信息已被选择,其他单位信息显示不出现重复

<!-- 显示当前草稿合作单位信息 -->
<c:if test="${not empty cooDepts }">

<select id="sel" name="cooDeparts"  multiple="multiple" style="width:100px">

<c:forEach var="department" items="${departments}">

<c:set var="iscontain" value="false"></c:set>

<c:forEach var="cooDept" items="${cooDepts}">

<c:if test="${department.deptId ==  cooDept.deptId}">

<option value="${department.deptId}" selected="selected">${department.deptName}</option>

<c:set var="iscontain" value="true"></c:set>

</c:if>

</c:forEach>

<c:if test="${!iscontain }"><option value="${department.deptId}">${department.deptName}</option></c:if>

</c:forEach>

</select>

</c:if>

参考地址

http://www.cnblogs.com/zfc-java/p/6862224.html

http://www.cnblogs.com/tingbogiu/p/5829010.html

阅读全文
0 0
原创粉丝点击