【发布时间】:2017-05-23 11:41:44
【问题描述】:
我想避免 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>
我需要读取每个属性(如果没有发送属性我需要创建空的<td></td> 块。
是否可以在一个循环中而不是三个循环中完成(在这种情况下,这个数字表示不同属性的数量)。
感谢您的帮助。
【问题讨论】:
-
customerAttribute.attributes[0] .. [1]...[2]应该有各自的值,你可以直接访问它们。
-
但是,正如我所说,[0] 也可以包含
WRTSC、DTA或DTA_PRZEDST_TR_OSW... -
你需要在服务器端处理,从服务器端按顺序放置。
-
@DanyalSandeelo 我对此没有影响。但是你能检查我的答案吗?这是更好的解决方案吗?
-
是的,这样更好。以前,您的外部循环至少有 9 次内部迭代,现在您将重复次数减少到了一次。
标签: java html jsp for-loop jstl