I have the following code:
String[] columnHeaders = {"Banana", "Apple", "Carrot", "Orange", "Lychee", "Permisson"};
<c:forEach var="header" items="<%= columnHeaders%>">
<td>
<c:out value="${header}" />
</td>
</c:forEach>
When the JSP is executed, the following values get printed:
org.apache.commons.el.ImplicitObjects$7@6ac86ac8
org.apache.commons.el.ImplicitObjects$7@6ac86ac8
...
It appears to me that the memory value is being printed and not the value contained in each string.
What am I missing here?
You’re referencing the
itemsvalue incorrectly. Taglibs/EL and scriptlets does not share the same variable scope. You’re basically printingcolumnHeaders.toString()in theitemsattribute and tellingc:forEachto iterate over it. Instead, you need to put it in the request scope (preferably by a servlet) and use EL${}the normal way:Also,
${header}is an reserved EL variable referring to the request header map (see implicit objects in EL), you’d need to rename it to something else such as${columnHeader}in the above example.See also:
Unrelated to the concrete problem, table headers needs to be represented in HTML by
<th>, not<td>.