【问题标题】:Avoid multi forEach loop in JSTL in JSP在 JSP 中避免 JSTL 中的多个 forEach 循环
【发布时间】:2017-05-23 11:41:44
【问题描述】:

我想避免 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>

我需要读取每个属性(如果没有发送属性我需要创建空的&lt;td&gt;&lt;/td&gt; 块。

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

感谢您的帮助。

【问题讨论】:

  • customerAttribute.attributes[0] .. [1]...[2]应该有各自的值,你可以直接访问它们。
  • 但是,正如我所说,[0] 也可以包含WRTSCDTADTA_PRZEDST_TR_OSW...
  • 你需要在服务器端处理,从服务器端按顺序放置。
  • @DanyalSandeelo 我对此没有影响。但是你能检查我的答案吗?这是更好的解决方案吗?
  • 是的,这样更好。以前,您的外部循环至少有 9 次内部迭代,现在您将重复次数减少到了一次。

标签: java html jsp for-loop jstl


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    你可以使用这样的东西

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

    【讨论】:

    • 这不是我的意思。我需要按以下顺序显示WRTSCDTADTA_PRZEDST_TR_OSW
    【解决方案3】:

    而不是运行 3 个内部循环,取 3 个内部变量用于

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-22
      • 2014-03-24
      • 2021-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-14
      相关资源
      最近更新 更多