I have an arraylist in servlet which i am using it in jsp.
ArrayList<String> list =new ArrayList<String>();
list = (ArrayList<String>)request.getAttribute("iInfoType");
I am using jstl to parse the array list
<c:forEach items="<%=list %>" var="element">
<c:out value="${element[10].id}"/>
</c:forEach>
But getting error PropertyNotFoundException. what should i do
There is no id
property for String
, so you need to change your jstl as shown below:
<c:forEach items="list" var="element">
<c:out value="${element}"/>
</c:forEach>
Also, when you are using c:forEach
, you are iterating the list, so using ${element}
gives you the elements present in the list
.
One more point is that if the list
is already set in the request scope by the servlet/controller, you don't need to use scriptlets as shown above (i.e., you can directly access the list
like items="list"
).
If you want to access the index
of the list
, you can use varStatus
as shown below:
<c:forEach items="list" var="element" varStatus="myList">
<c:out value="${myList.index}"/>
</c:forEach>
Using scriptlets in your JSP pages is not a best practice, so I suggest not to use them, please look here for more details on JSP best practices. I have added the key point below from the link:
Embedding bits of Java code (or scriptlets) in HTML documents may not be suitable for all HTML content developers, perhaps because they do not know the Java language and don't care to learn its syntax.