Reputation: 749
I am having a hard time iterating over a List with jsp. I did this several times with velocity, but I somehow can't get it to work with jsp.
This is an easy example I am trying to get to work:
@RequestMapping("/bye")
public ModelAndView byeWorld() {
String message = "Goodbye World, Spring 4.1.2!";
List<Map<String, Object>> data = dataProvider.getVorgaengeGesamtByArkNr();
model.put("table", data);
model.put("columnNames", utils.getColumnNames(data));
return new ModelAndView("test", model);
}
utils.getColoumnNames
returns an List.
This is the lopp in the .jsp:
<c:forEach var="entry" items="${columnNames}">
<tr>
entry
</tr>
</c:forEach>
Response looks like this:
<c:forEach var="entry" items="[ARKNR_ABTEILUNG, LIEFERANT_ID, LIEFERANT_NAME, RECHNUNGS_NR, RECHNUNGS_DATUM, RECHNUNGS_EINGANG, STATUS_ID, STATUS_NAME_DE, RECHNUNGS_BETRAG_BRUTTO, RECHNUNGS_BETRAG_WAEHRUNG, SKONTO, WEITERBERECHNUNG, ARCHIV_ID, PROZESS_ID, AKTUELLER_BENUTZER, AKTUELLER_BENUTZER_ID, RECHNUNG_ID, KV_ID, ARKNR, DEPARTMENT_ID, FIBU_NAME, DBRD_ID]">
<tr>
entry
</tr>
</c:forEach>
So the list is just replacing ${columnNames}
, not looping through it at all.
@SuppressWarnings("unchecked")
public static List<String> getColumnNames(List list) {
if (list.size() > 0) {
Map map = (Map)list.get(0);
List<String> columnNames = new ArrayList<String>();
for (Object entry : map.keySet())
{
logger.debugT(entry.toString());
columnNames.add(entry.toString());
}
return columnNames;
} else {
return null;
}
}
Upvotes: 1
Views: 7747
Reputation: 3466
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<body>
<h2>Example</h2>
<c:if test="${not empty columnNames}">
<h2>Columns</h2>
<ul>
<c:forEach var="col" items="${columnNames}">
<li>${col}</li>
</c:forEach>
</ul>
</c:if>
</body>
</html>
Upvotes: 1
Reputation:
You need to use ${}
, also use td
to make it visible within table.
<tr>
<td>${entry}</td>
</tr>
Edited:
As Jackk already pointed out, you need to import that taglib in order to use forEach
on the top of the jsp.
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
Upvotes: 0