[英]What is the most effective way of printing Arraylist of string array in Jsp?
There is an ArrayList which contains String arrays .有一个包含字符串数组的 ArrayList 。 Each string array contains values of each row.每个字符串数组包含每一行的值。 I want to print this in JSP.我想在 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 ?我尝试直接从 servlet 打印它,它可以工作,但是由于数据很多,所以速度很慢。如何在 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.也许如果您使用StringBuilder
连接字符串会提高您的性能。
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>
:然后使用<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...问题是如何在 JSP 中迭代......所以......
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
标签在 jsp 中迭代
<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如果您使用的是 JSP 2.0,您也可以使用varStatus="loop"
I go with Bazz answer.我和 Bazz 一起回答。 The below code should work fine.下面的代码应该可以正常工作。 You need to add the listName model in request attribute.您需要在请求属性中添加 listName 模型。
<c:forEach var="listVar" items="${listName}">
<option value ="10"><c:out value="${listVar.attribute}"/></option>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.