You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JSTL中仅遍历单个元素?

解决方案

你不需要用<c:forEach>遍历整个好友集合,直接定位到目标单个好友对象,再用<c:if>做条件判断就行,分两种常见情况:

情况1:目标好友是集合里的特定元素(比如第一个)

可以通过EL的下标直接获取集合中的元素,再判断其selectedCount:

<%-- 先获取目标单个好友 --%>
<c:set var="targetFriend" value="${profile.friends[0]}" />
<%-- 判断并显示<th> --%>
<c:if test="${targetFriend.selectedCount != -1}">
    <th id="closeFriends" style="text-align:center;"></th>
</c:if>

情况2:后台已单独传递目标好友对象

如果后台已经把要检查的单个好友存到某个变量(比如${targetFriend}),直接用这个变量判断即可:

<c:if test="${targetFriend.selectedCount != -1}">
    <th id="closeFriends" style="text-align:center;"></th>
</c:if>

核心就是:跳过遍历操作,直接拿到要检查的单个好友对象,用<c:if>做条件校验,满足条件就渲染<th>标签。

内容的提问来源于stack exchange,提问作者Brandon Hu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 22:35:48