简体   繁体   English

在 Jsp 中打印字符串数组的 Arraylist 最有效的方法是什么?

[英]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.

相关问题 在IntelliJ IDEA中创建“新ArrayList”的最快/最有效方法是什么 - What is the fastest/most effective way to create a “new ArrayList” in IntelliJ IDEA 将字符串数组从xml加载到ArrayList的最有效方法是什么? - What is the most effiecient way of loading a string array from xml into an ArrayList? 将数组A []转换为ArrayList的有效和最短方法 - Effective and shortest way to convert array A[] to ArrayList<B> 坚持HashMap的最有效方法是什么? - what is the most effective way to persist HashMap? 用Java输入数组最有效的方法是什么? - What is the most effective method to input an array in Java? 将String数组存储在ArrayList中的正确方法是什么? - What is the proper way to store a String array in an ArrayList? 在用户输入的相关部分和数组之间找到匹配项的最有效方法是什么? - What is the most effective way to find a match between the relevant parts of user input and an array? 加入ArrayList的有效方法 - Effective way to Join ArrayList 将字符串添加到此字符串数组的最有效方法是什么? - What is the most efficient way to add a string to this string array? 从int值创建BigInteger实例的最有效方法是什么? - What is the most effective way to create BigInteger instance from int value?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM