'Does any one know how to create Empty Array In JSTL (JSP Standard Tag Library)
I am trying to create an empty in my jstl page is it possible to create an empty array and assign some value to that array in the jstl page itself .?
Solution 1:[1]
Your question is quite unusual. You want to do scripting without a scriplet. The only way that I could think of is to use the useBean tag to create an ArrayList. Its add method returns a boolean. That is why I used empty if tags to insert the elements. The forEach tag is not necessary. I used it to display the elements.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<jsp:useBean id="myList" class="java.util.ArrayList" />
<c:if test = '${myList.add("My first element")}'>
</c:if>
<c:if test = '${myList.add("My second element")}'>
</c:if>
<c:forEach var="element" items="${myList}">
${element}
</c:forEach>
Output: My first element My second element
Solution 2:[2]
You can create an array with some values using c:set
:
<c:set var="array" value="${['item a','item b','item c']}" />
<c:forEach var="item" items="${array}">
<c:out value="${item}" /><br />
</c:forEach>
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | rickz |
Solution 2 | obourgain |