There is an ArrayList which contains String arrays . Each string array contains values of each row. I want to print this in JSP. I tried printing it directly from servlet,its working but it is very slow since there is a lot of data.How to do this in JSP EL ?
public void printTableData(List<String[]> dataTable, PrintWriter out) {
System.out.println(dataTable);
out.println("<table border='1'>");
out.println("<tr>");
for (int j = 0; j < dataTable.get(0).length; j++) {
out.println("<th>" + dataTable.get(0)[j] + "</th>");
}
out.println("</tr>");
if (dataTable.size() > 1)
for (int i = 1; i < dataTable.size(); i++) {
out.println("<tr>");
for (int j = 1; j < dataTable.get(i).length; j++) {
out.println("<td>" + dataTable.get(i)[j] + "</td>");
}
out.println("</tr>");
}
out.println("</table>");
}
}
Maybe if you use a StringBuilder
to concatenate the Strings will improve your performance.
public void printTableData(List<String[]> dataTable, PrintWriter out) {
System.out.println(dataTable);
StringBuilder sb = new StringBuilder();
sb.append("<table border='1'>\n");
sb.append("<tr>\n");
for (int j = 0; j < dataTable.get(0).length; j++) {
sb.append("<th>");
sb.append(dataTable.get(0)[j]);
sb.append("</th>\n");
}
sb.append("</tr>\n");
if (dataTable.size() > 1)
for (int i = 1; i < dataTable.size(); i++) {
sb.append("<tr>\n");
for (int j = 1; j < dataTable.get(i).length; j++) {
sb.append("<td>");
sb.append(dataTable.get(i)[j]);
sb.append("</td>\n");
}
sb.append("</tr>\n");
}
sb.append("</table>\n");
out.println(sb.toString());
}
}
First pass the data to your request. Create a table header after:
<table class="your_class">
<thead>
<tr>
<th>Title</th>
<th>More</th>
<th>Other</th>
</tr>
</thead>
then iterate over elements with <for:each>
:
<tbody>
<c:forEach items="${list}" var="data">
<tr class="your_class">
<td>${data.attribute1}</td>
<td>${data.attribute2}</td>
<td>${data.attribute3}</td>
</tr>
</c:forEach>
</tbody>
</table>
The question was how to iterate in JSP...So...
First you need to add list into request attribute:
request.setAttribute("list", dataTable)
Then you can iterate in jsp using c:forEach
tag
<c:forEach items="${list}" var="entry"> <!-- iterate here using ${entry} --> </c:forEach>
You can also use varStatus="loop"
, if you're using JSP 2.0
I go with Bazz answer. The below code should work fine. You need to add the listName model in request attribute.
<c:forEach var="listVar" items="${listName}">
<option value ="10"><c:out value="${listVar.attribute}"/></option>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.