如何在jsp中以表格格式打印二维数组

时间:2015-03-02 11:02:51

标签: java arrays jsp

我从db检索数据,我可以在控制台中打印。

Code : 
List<String[]> table = new ArrayList<>();
ResultSet rs2=p2.executeQuery();
            int nCol = rs2.getMetaData().getColumnCount();
            while(rs2.next()){
                 String[] row = new String[nCol];
                    for( int iCol = 1; iCol <= nCol; iCol++ ){
                        row[iCol-1] = rs2.getObject( iCol ).toString();
                    }
                    table.add( row );
            }

            for( String[] row: table ){
                for( String s: row ){
                    System.out.print( " " + s );

                }
               System.out.println();
            }
return table;

Output in Console:
 3002085 -1073741824 2015-02-27 2015-03-27 Balance Group<Subscription> 61917361 B-Free GPRS Bundle - UNIT Bytes
 3000009 -409.1 2015-02-27 2015-03-27 Balance Group<Subscription> 61917361 B-Anytime Dollar bundles - UNIT Dollars
 3500101 -1073741824 2015-02-27 2015-03-27 Balance Group<Subscription> 61917361 B-Aggregated Data Entitlement Counter - UNIT Bytes
 3000009 -409.1 2015-01-27 2015-02-27 Balance Group<Subscription> 61917361 B-Anytime Dollar bundles - UNIT Dollars
 3500101 -1073741824 2015-01-27 2015-02-27 Balance Group<Subscription> 61917361 B-Aggregated Data Entitlement Counter - UNIT Bytes
 3002085 -1073741824 2015-01-27 2015-02-27 Balance Group<Subscription> 61917361 B-Free GPRS Bundle - UNIT Bytes
 3500101 -1073741824 2014-12-27 2015-01-27 Balance Group<Subscription> 61917361 B-Aggregated Data Entitlement Counter - UNIT Bytes
 3000009 -409.1 2014-12-27 2015-01-27 Balance Group<Subscription> 61917361 B-Anytime Dollar bundles - UNIT Dollars

我已将整个List 返回到调用此方法的servlet。在servlet中,我使用set属性将其传递给jsp。 我不知道如何在jsp中迭代这个列表并将它们打印在Table标签中。

之前我曾尝试使用arraylist,但是对于每个列结果我都必须使用它。传递数组列表时的代码

jsp code :
<%

ArrayList<String> date=(ArrayList<String>)request.getAttribute("date");
ArrayList<String> packageid=(ArrayList<String>)request.getAttribute("packageid");
ArrayList<String> cli=(ArrayList<String>)request.getAttribute("cli");
ArrayList<String> env=(ArrayList<String>)request.getAttribute("env");

if (cli != null && !cli.isEmpty()){%>
    <table class="table table-hover">
    <thead>
      <tr>
        <th>DATE</th>
        <th>PackageID</th>
        <th>CLI</th>
        <th>ENV</th>
        <th>Action</th>
      </tr>
    </thead>
    <tbody>
    <%for(int i=0;i < cli.size();i++)
    {%>

          <tr>
            <td><%= date.toArray()[i] %></td>
            <td><%= packageid.toArray()[i]%></td>
            <td><%=cli.toArray()[i] %></td>
            <td><%=env.toArray()[i] %></td>
            <td><a href="del.jsp?id=<%=cli.toArray()[i] %>&dt=<%= date.toArray()[i] %>">Delete</a>/<a href="lock.jsp?cli=<%=cli.toArray()[i] %>&dt=<%= date.toArray()[i] %>">Lock</a>
          </tr>





    <%} 

}else{%>
<tr><b><td>No details </td><td>Present in </td><td>Queue to display.</td></b>
</tr>

<%}%>
   </tbody>
      </table> </div></div>

以这种类似的方式可以以表格格式打印我的2D数组吗? 请帮助

1 个答案:

答案 0 :(得分:0)

您可以使用2D数组字符串来存储表数据,并在jsp页面中显示迭代此2D数组。