[英]jsp (jstl 1.2) - set a variable from a java list
(我正在使用jstl 1.2版和Java 6)
我正在使用一些在其jsp頁面中具有某些邏輯的舊代碼,除了這些,我現在還需要遍歷數據列表,匹配某些內容,然后將變量設置為列表值(在這種情況下為ArrayList)。 我需要這樣做的原因是,然后遍歷以后在jsp文件中找到的列表。
這是到目前為止的代碼片段,但是不起作用:
<c:set var="listOfChildData" value="${[]}" scope="page"/>
<c:forEach items="${otherListOfData}" var="data">
<c:if test="${data.id == dataToMatchOn.id}">
<c:catch var="exception">${data.children}</c:catch>
<c:if test="${empty exception}">
<c:set var="listOfChildData" value="${data.children.toArray()}" scope="page"/>
</c:if>
</c:if>
</c:forEach>
我需要手動瀏覽列表中的每個項目並將其添加到listOfChildData
嗎?
通讀一遍,我發現的所有示例都是從頭開始而不是從另一個變量創建數組變量。
如果歸結為使用scriptlet,我可以在forEach
循環中執行此操作嗎?
更新以包括data.children
為null,empty等時的異常處理。
您無需復制列表。 您需要設置的只是selectedItem,然后就可以使用它:
<c:forEach items="${otherListOfData}" var="data">
<c:if test="${data.id == dataToMatchOn.id}">
<c:set var="selectedItem" value="${data}/>
</c:if>
</c:forEach>
<p>From here on I can work with the selected item:</p>
${selectedItem.children}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.