在 JSP 中避免 JSTL 中的多个 forEach 循环

Avoid multi forEach loop in JSTL in JSP

我想避免 JSTL 中的多循环,如下面的代码所示。我从 api 响应中获得了属性 WRTSCDTADTA_PRZEDST_TR_OSW,它们随机传递,所以代码看起来像这样。

<c:forEach items="${ctx.model.customerAttributes}" var="customerAttribute">
<tr>
    <td class="code">${customerAttribute.subGroupName}</td>
    <td class="value">
        <c:forEach items="${customerAttribute.attributes}" var="attribute">
            ${attribute.attrName == 'WRTSC' ? attribute.attrValue : ''}
        </c:forEach>
    </td>
    <td class="value">
        <c:forEach items="${customerAttribute.attributes}" var="attribute">
            ${attribute.attrName == 'DTA' ? attribute.attrValue : ''}
        </c:forEach>
    </td>
    <td class="value">
        <c:forEach items="${customerAttribute.attributes}" var="attribute">
            ${attribute.attrName == 'DTA_PRZEDST_TR_OSW' ? attribute.attrValue : ''}
        </c:forEach>
    </td>
</tr>
</c:forEach>

我需要读取每个属性(如果没有发送属性我需要创建空 <td></td> 块。

是否可以在一个循环而不是三个循环中完成(在这种情况下,这个数字代表不同属性的数量)。

感谢您的帮助。

我现在有这样的东西。小伙伴们,你们觉得这样更好吗?

<c:forEach items="${ctx.model.customerAttributes}" var="customerAttribute">
<tr>
    <c:set var="WRTSC" value="" />
    <c:set var="DTA" value="" />
    <c:set var="DTA_PRZEDST_TR_OSW" value="" />

    <c:forEach items="${customerAttribute.attributes}" var="attribute">
        <c:if test="${WRTSC eq ''}">
            <c:set var="WRTSC" value="${attribute.attrName == 'WRTSC' ? attribute.attrValue : ''}" />
        </c:if>
        <c:if test="${DTA eq ''}">
            <c:set var="DTA" value="${attribute.attrName == 'DTA' ? attribute.attrValue : ''}" />
        </c:if>
        <c:if test="${DTA_PRZEDST_TR_OSW eq ''}">
            <c:set var="DTA_PRZEDST_TR_OSW" value="${attribute.attrName == 'DTA_PRZEDST_TR_OSW' ? attribute.attrValue : ''}" />
        </c:if>
    </c:forEach>

    <td class="code">${customerAttribute.subGroupName}</td>
    <td class="value">${WRTSC}</td>
    <td class="value">${DTA}</td>
    <td class="value">${DTA_PRZEDST_TR_OSW}</td>
</tr>
</c:forEach>

你可以使用这样的东西

<c:forEach items="${ctx.model.customerAttributes}" var="customerAttribute">
    <tr>
        <td class="code">${customerAttribute.subGroupName}</td>
        <td class="value">
            <c:forEach items="${customerAttribute.attributes}" var="attribute">
             ${(attribute.attrName == 'WRTSC') || (attribute.attrName == 'DTA') || (attribute.attrName == 'DTA_PRZEDST_TR_OSW')? attribute.attrValue : ''}
            </c:forEach>
        </td>

    </tr>
</c:forEach>

<c:forEach items="${ctx.model.customerAttributes}" var="customerAttribute">
    <tr>
        <td class="code">${customerAttribute.subGroupName}</td>
        <td class="value">
            <c:forEach items="${customerAttribute.attributes}" var="attribute">
                <c:if test="${(attribute.attrName == 'WRTSC') || (attribute.attrName == 'DTA') || (attribute.attrName == 'DTA_PRZEDST_TR_OSW')}">
                 ${attribute.attrValue}
                </c:if>
            </c:forEach>
        </td>
    </tr>
</c:forEach>

而不是 运行ning 3 内部循环为

取 3 个内部变量
var WRTSC='';
var DTA ='';
var DTA_PRZEDST_TR_OSW ='';

和运行只有一个内部循环,其中检查变量的条件,如果条件匹配则设置变量的值,否则默认值为''。