我想使用JSTL forEach循環中的計數,但我的代碼似乎沒有工作。使用JSTL forEach循環的varStatus作爲ID
<c:forEach items="${loopableObject}" var="theObject" varStatus="theCount">
<div id="divIDNo${theCount}">
</div>
</c:forEach>
產生
<div id="[email protected]" >
我想使用JSTL forEach循環中的計數,但我的代碼似乎沒有工作。使用JSTL forEach循環的varStatus作爲ID
<c:forEach items="${loopableObject}" var="theObject" varStatus="theCount">
<div id="divIDNo${theCount}">
</div>
</c:forEach>
產生
<div id="[email protected]" >
由varStatus設置的變量是一個LoopTagStatus對象,而不是一個int。用途:
<div id="divIDNo${theCount.index}">
澄清:
${theCount.index}
從0開始${theCount.count}
開始計數在1..或許你需要''div id =「divIDNo $ {theObject}」>' – Gedrox
@Gedrox:我不這麼認爲:) – BalusC
'$ { theCount.count}'** always **從1開始。'$ {theCount.index}'從您設置了'begin'屬性開始。例如'
計數你可以試試這個。類似的結果
<c:forEach items="${loopableObject}" var="theObject" varStatus="theCount">
<div id="divIDNo${theCount.count}"></div>
</c:forEach>
它真的幫我可以動態生成的showDetailItem
IDS爲下面的代碼。
<af:forEach id="fe1" items="#{viewScope.bean.tranTypeList}" var="ttf" varStatus="ttfVs" >
<af:showDetailItem id ="divIDNo${ttfVs.count}" text="#{ttf.trandef}"......>
如果執行這個線<af:outputText value="#{ttfVs}"/>
打印以下:
{指數= 3,計數= 4,最後=假,第一=假,端= 8,步長= 1,開始= 0}
@HenryKeiter這裏有一個真實的答案。糟糕的格式化只是把所有的HTML都隱藏起來,直到我修復它。 –
這提供了3年前已經提供的完全相同的答案(使用'varStatus.count'),只有更少的可讀性。爲什麼要讀別人的答案呢? ;) – rustyx
你使用任何這些:
JSTL C:的forEach varStatus屬性
屬性的getter說明
當前getCurrent()(從集合)當前 輪迭代的項目。
index getIndex()當前一輪 迭代的從零開始的索引。
計數getCount將(),用於指示當前輪 是否是第一次通過迭代
最後isLast會本輪迭代
開始getBegin()中的begin屬性
E的值ND getEnd()結束屬性
步驟getStep的值()的步驟屬性
這裏的值是類的Javadoc:http://download.oracle.com/javaee/ 6/API /的javax/servlet的/ JSP/JSTL /型芯/ LoopTagStatus。看看它提供了什麼getter方法。是的,其中有一個'getIndex()':) – BalusC