Java 在JSP中避免JSTL中的multi-forEach循环

Java 在JSP中避免JSTL中的multi-forEach循环,java,html,jsp,for-loop,jstl,Java,Html,Jsp,For Loop,Jstl,我希望避免JSTL中的多循环,如下代码所示。我从api响应中获得了属性WRTSC、DTA、DTA_PRZEDST_TR_OSW,它们是随机传递的,所以代码看起来像这样 <c:forEach items="${ctx.model.customerAttributes}" var="customerAttribute"> <tr> <td class="code">${customerAttribute.subGroupName}</td>

我希望避免JSTL中的多循环,如下代码所示。我从api响应中获得了属性WRTSC、DTA、DTA_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>
我需要读取每个属性,如果没有发送属性,我需要创建空块

可以在一个循环而不是三个循环中完成吗?在这种情况下,这个数字表示不同属性的数量


谢谢你的帮助。

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

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


不是运行3个内部循环,而是取3个内部变量作为

var WRTSC='';
var DTA ='';
var DTA_PRZEDST_TR_OSW ='';

并且只运行一个内部循环,在该循环中检查条件与变量,如果条件匹配,则设置变量的值,否则默认值为。

customerAttribute.attributes[0]。。[1] …[2]应该分别具有这些值,您可以直接访问它们。但是,正如我所说,[0]也可以包含WRTSC、DTA或DTA_PRZEDST_tru OSW……您需要在服务器端处理这些值,从服务器端按顺序排列。@Danyalsandelo我对此没有影响。但是你能检查我的答案吗?这是更好的解决方案吗?是的,更好。以前你的外循环至少有9次内循环,现在你把重复次数减少到了一次。这不是我的观点。我需要按以下顺序显示:WRTSC、DTA和DTA_PRZEDST_TR_OSW
var WRTSC='';
var DTA ='';
var DTA_PRZEDST_TR_OSW ='';