jstl为每个循环内部访问另一个带索引的jstl变量

时间:2011-12-03 05:24:49

标签: jstl

在JSTL中如何在此循环中访问另一个带索引的var

 <c:set var="cmbIndex" value="${TXTTITLE}"> </c:set>  this variable have 1,6,3....
<c:forEach var="item" items="${HDNNAMEANDID}" varStatus="row">
    <option value='<c:out value="${cmbIndex[row.index]}"/>'>${item}</option>
</c:forEach>

它给出了错误

1 个答案:

答案 0 :(得分:1)

你应该能够在没有c:out的情况下访问它。 TXTTITLE也是什么类型的收藏品?如果一个数组应该工作:

<c:set var="cmbIndex" value="${TXTTITLE}"> </c:set>  
<c:forEach var="item" items="${HDNNAMEANDID}" varStatus="row">
    <option value='${cmbIndex[row.index]}'> ${item}</option>
</c:forEach>

如果是列表类型集合:

<c:set var="cmbIndex" value="${TXTTITLE}"> </c:set>  
<c:forEach var="item" items="${HDNNAMEANDID}" varStatus="row">
    <option value='${cmbIndex.get(row.index)}'> ${item}</option>
</c:forEach>

您也可以直接访问:

    <option value='${TXTTITLE[row.index]}'> ${item}</option>
    // OR
    <option value='${TXTTITLE.get(row.index)}'> ${item}</option>

鉴于TXTTITLE是逗号分隔值,如果您尝试通过逗号分隔列表中的数字位置来访问它,我将使用split()并将其转换为String数组:

<c:set var="cmbIndex" value="${TXTTITLE.split(',')}"> </c:set>  
...
<option value='${cmbIndex[row.index]}'> ${item}</option>
相关问题