Creating and adding elements to a list in JSTL

There are plenty of places in WebSphere Commerce out-of-the-box code where we can see code to iterate over a list in JSTL. But I feel that code to create and add items to list in JSTL are rare and hence lot of developers (atleast beginners) do not know how to do that.

I recently came across and used this code to create and add items to an arraylist in JSP using JSTL. And I thought I need to note it down somewhere so I can refer in future.

Sample code below:

 <wcf:useBean var="sampleList" classname="java.util.ArrayList"/>  
 ...  
 <c:set var="sampleVar" value="val"/>  
 <wcf:set target="${sampleList}" value="${sampleVar}"/>  

Once you created the list, you can iterate through and process it as required.

 <c:forEach items="${sampleList}" var="eachVal">  
      <c:out value="${eachVal}" />  
 </c:forEach>  

Hope this help someone. Feel free to share this page with your friends and colleagues.

Thanks!

Comments

Popular posts from this blog

Applied promotions of an order - IBM WebSphere Commerce

Fetching cookie value in controller command - IBM WebSphere Commerce