Reputation: 967
i want to write common page for viewing data list or single object.
In controller:
ArrayList<Table> tables;
request.setAttribute("tables", tables);
and JSP:
<table >
<c:forEach var="table" items="${tables}">
<tr>
<th><c:out value=" ${table.name}" /></th>
</tr>
</c:forEach>
</table>
Shows good.
When i pass single object -
Table table;
request.setAttribute("table", table);
does not show anything;
Than i try pass
Table tables;
request.setAttribute("tables", tables);
i have error
Don't know how to iterate over supplied "items" in <forEach>
This is a pretty simple example, my application must view a lot of data of this object, and make two similar pages with the same code it silly. How to solve this problem? add this single object to the list or there is another solution?
Upvotes: 0
Views: 1432
Reputation: 13488
It's because jstl forEach
tag is expecting a List, and you are passing a single object.
Try passing a one element list:
Table table;
ArrayList<Table> tables=new ArrayList<Table>();
tables.add(table)
request.setAttribute("tables", tables);
Upvotes: 1
Reputation: 59694
It seems like you want to pass single element instead of list. One way to solve this is to build a list with single element and pass it. Use following to create list with only one element in it.
List<Table> tables = Collections.singletonList(table);
Upvotes: 3