Mihir Chauhan
Mihir Chauhan

Reputation: 141

printing ArrayList using jstl in jsp

Using an ArrayList of type Countries that is a bean class I'm getting only a blank page as output when using the following code:

<%
    ArrayList<Countries> countryList = (ArrayList<Countries>) request.getAttribute("al");
%>

<c:forEach items="${countryList}" var="item">
    <c:out value="${item.code}"></c:out>
    <c:out value="${item.name}"></c:out>
</c:forEach>

Upvotes: 2

Views: 259

Answers (2)

Gautam
Gautam

Reputation: 3384

you need not to set in request attribute again.You can use below code

<c:forEach items="${al}" var="item">
    <c:out value="${item.code}"></c:out>
    <c:out value="${item.name}"></c:out>
</c:forEach>

This way you can get rid of scriptlet also.

Hope it Helps

Upvotes: 2

Cesar Loachamin
Cesar Loachamin

Reputation: 2738

The reason is the EL (Expression language) can't find any variable named countryList in any valid scope. The variable declared in the scriptlet is not visible to the EL so you must add it to a valid scope for example the request so:

<%
    ArrayList<Countries> countryList = (ArrayList<Countries>) request.getAttribute("al");
    request.setAttribute("countryList", countryList);
%>

<c:forEach items="${countryList}" var="item">
    <c:out value="${item.code}"></c:out>
    <c:out value="${item.name}"></c:out>
</c:forEach>

Upvotes: 1

Related Questions