Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
125
rated 0 times [  129] [ 4]  / answers: 1 / hits: 61476  / 14 Years ago, thu, july 22, 2010, 12:00:00

I want to iterate a HashMap in javascript using jstl. is it possible to do like this?



function checkSelection(group,tvalue){
alert(group);
alert(tvalue);

<c:forEach items=${configuredGroupMap} var=groupMap>
alert(aa<c:out value=${groupMap.key}/>);
<c:if test=${groupMap.key==group}>
alert(t<c:out value=${groupMap.key}/>);
<c:if test=${groupMap.value==tvalue}>
alert(equal);
</c:if>
</c:if>
</c:forEach>
}


it's not going inside after



 <c:if test=${groupMap.key==group}>

More From » java

 Answers
11

to iterate a HashMap in javascript using jstl - Not possible



JSTL is executed in server side by your servlet container for which Javascript is just a text which would be skipped whereas JavaScript is executed in client side where JSTL is unknown. After the server completes processing JSTL, the generated HTML(if any) from the JSTL along with other JavaScript/HTML will be rendered.



For example, if you have this,



<c:forEach var=myItem items=${myCollection}>
alert('<c:out value=${myItem.id}>')
<c:if test=${myItem.id == 0}>
alert(zero);
</c:if>
</c:forEach>


If the ids of the beans in the collection are 0, 1, 2, the server renders the following to the client side by executing the above code,



alert('0')
alert('zero')
alert('1')
alert('2')


Now the browser will give you 4 alerts on loading the page (what if you have 10000 items, you will render 10000 alert statements to the browser). So the point is that you haven't iterated Java collection in JavaScript, you have simply generated a serious of Javascript statements in server iterating the collection using JSTL and you have provided those Javascript statements along with other html contents to browser.


[#96150] Monday, July 19, 2010, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
pierre

Total Points: 716
Total Questions: 128
Total Answers: 102

Location: Djibouti
Member since Sun, Feb 27, 2022
2 Years ago
pierre questions
Fri, Nov 6, 20, 00:00, 4 Years ago
Fri, Sep 4, 20, 00:00, 4 Years ago
Thu, Jul 18, 19, 00:00, 5 Years ago
Sun, Dec 2, 18, 00:00, 6 Years ago
Fri, Oct 26, 18, 00:00, 6 Years ago
;