简体   繁体   English

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

[英]Avoid multi forEach loop in JSTL in JSP

I want to avoid multi loop in JSTL which is shown by the code presented below. 我想避免JSTL中的多循环,如下所示的代码所示。 I got attributes WRTSC , DTA , DTA_PRZEDST_TR_OSW from api response and they are passing randomly so that is why the code looks like this. 我从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>

I need to read every attribute (if there is not attribute sent I need to create empty <td></td> block. 我需要读取每个属性(如果没有发送属性,则需要创建一个空的<td></td>块。

Can it be done in one loop instead of three (in this case this number respresents the number of different attributes). 是否可以在一个循环中而不是三个循环中完成(在这种情况下,该数字表示不同属性的数量)。

Thanks for helping. 感谢您的帮助。

I've got something like this now. 我现在有这样的事情。 Guys, do you think this is better? 伙计们,您认为这样更好吗?

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

You can use something like this 你可以用这样的东西

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

or 要么

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

Rather than running 3 inner loop take 3 inner variable for 与其运行3个内部循环,不如将3个内部变量用于

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

and run only one inner loop in which check the condition with the variables , if condition is match set the value of variable otherwise by default value is '' . 并且仅运行一个内部循环来检查变量的条件,如果条件匹配,则设置变量的值,否则默认值为''。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM