JSTL: How do I set an array variable?

D

Derek Mahar

How do I use the JSTL <c:set> to set an array variable that I may
subsequently iterate over with <c:forEach>?

Thanks,

Derek
 
S

Scott Yanoff

Derek said:
How do I use the JSTL <c:set> to set an array variable that I may
subsequently iterate over with <c:forEach>?

A weak solution is to set a delimited string to simulate and array:
<c:set var="brewers">Richie Sexson, Eric Young, Ben Sheets, Geoff
Jenkins, Brady Clark, Scott Podsednik, Keith Ginter</c:set>

Then, use forTokens to iterate through it:
<c:forTokens items="${brewers}" delims="," var="brewer">
<li><c:eek:ut value="${brewer}">
</c:forTokens>

Good luck,
 
Joined
Jun 7, 2011
Messages
1
Reaction score
0
JSTL Setting Arrays

THIS:
<%
String[] yankees = {"Jeter", "Posada", "Rodriguez", "Kanoe", "Rivera", "Swisher"};
pageContext.setAttribute("yankees",yankees, PageContext.REQUEST_SCOPE);
%>

IS THE SAME AS THIS:
<c:set var="yankees" scope='request'>"Jeter", "Posada", "Rodriguez", "Kanoe", "Rivera", "Swisher"</c:set>

AND BOTH CAN BE PRINTED LIKE THIS:
<c:forEach var="yankee" items="${yankees}">
Name: <c:eek:ut value="${yankee}" /><br />
</c:forEach>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top