【问题标题】:Looping through all attributes of item in EL遍历EL中item的所有属性
【发布时间】:2026-01-12 19:40:01
【问题描述】:

BalusC 可能会为我回答这个问题。

我有一个字符串列表,我将其设置为请求对象中的属性。这些字符串中的每一个都代表一个表格标题,我将在页面上打印出来。

然后,我将为我在别处得到的每个“进程”类型的对象添加一个 jsp。每个流程对象都具有相同的属性,但有些已填充,有些则没有。

这是目标:对于每个进程,循环遍历列名列表中的每个字符串。如果此进程上与字符串匹配的属性有值,那么我想显示该值,否则我会将该单元格留空。

基本上,我要求的是一种在 EL 和 JSTL 中进行递归的方法,以便我可以检查进程中的每个属性。

如果其中任何一个没有意义或者您需要更多的扩展,请询问。

编辑

<c:forEach items="${colNames}" var="cName"><%--colNames is the list of strings --%>
    <c:forEach items="${item.values}" var="value"><%--item is the process whose attributes I want to iterate through --%>
            <c:if test="item has attribute that matches cName">
                <td><c:out value="${value}"/></td><%--if item has an attribute that matches the string in the list, then I want to print out the value of that attribute--%>
            </c:if>
    </c:forEach>
</c:forEach>

【问题讨论】:

  • 或者这不是你的意思吗?
  • 我将添加一个编辑,以便更清晰。
  • 哇!为什么投反对票?

标签: jsp jstl el


【解决方案1】:

试试这个:

<c:forEach items="${colNames}" var="cName">
    <c:forEach items="${item.values}" var="value">
            <c:choose>
               <c:when test="${cName==value}">
                   <td><c:out value="${cName}"/></td>
               </c:when>
               <c:otherwise>
                   <td></td>
               </c:otherwise>
            </c:choose>
    </c:forEach>
</c:forEach>

【讨论】: