如何将ArrayList值排列成html表的行和列?

时间:2016-01-19 19:12:54

标签: html jsp arraylist html-table

我是编程的初学者,如果我在编程中犯了任何错误,请善待。

下面是我的MySql表,它还有一行"优先级"

+-----------+-------------+-------------+-------------+--------+---------------------+
| pk_vcl_id | vehicle_nm  | reg_nu      | officer_mob | active | updated_at          |
+-----------+-------------+-------------+-------------+--------+---------------------+
|         1 | FireEngine1 | KL 07 AB 11 | 9744231482  |      0 | 2016-01-19 21:42:36 |
|         2 | Ambulance1  | KL 07 CD 43 | 9446238433  |      0 | 2016-01-15 00:23:20 |
|         3 | policecar1  | KL 07 E 52  | 8129938273  |      0 | 2016-01-15 00:23:38 |
|         8 | Signal      | NIL         | 1111111111  |      0 | 2016-01-18 23:42:01 |
+-----------+-------------+-------------+-------------+--------+---------------------+

我需要将从数组列表中获得的上述数据安排到html表的行和列。我尝试了几种方法但没有正确对齐。

下面是我在Java页面中的arraylist: -

    while(rsgsts.next())
        {
        database.add(rsgsts.getString("vehicle_nm"));
        database.add(rsgsts.getString("reg_nu"));
        database.add(rsgsts.getString("active"));
        database.add(rsgsts.getString("officer_mob"));
        database.add(rsgsts.getString("priority"));
        database.add(rsgsts.getString("updated_at"));

        }

以下是我的jsp页面: -

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%--     <%@page contentType="text/html; charset=UTF-8" errorPage="errorpage.jsp" pageEncoding="UTF-8"%> --%>
<%--      <%@ page import="com.my.classes.*" %> --%>

<%@ page import="java.util.*"%>
<%@ page import="java.util.Map"%>

<%@ page import="java.util.Arrays.*"%>
<%--         <%@page import="org.json.JSONArray"%> --%>
<%@ page import="java.util.ArrayList.*"%>
<%@ page import="java.io.*,java.util.*,java.sql.*"%>
<%@ page import="javax.servlet.http.*,javax.servlet.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<meta name="HandheldFriendly" content="true">
<meta name="viewport"
    content="width=device-width, initial-scale=0.666667, maximum-scale=0.666667, user-scalable=0">
<meta name="viewport" content="width=device-width">
<link rel="icon" type="image/png" href="favicon.png">

<title>Database</title>
</head>


<div id="header"></div>

<div id="logo"></div>



<%
    String xyz="name";
    //dbLayer db=new dbLayer();
    ArrayList<String> data = new ArrayList<String>();
    data.add("Test");
    data.add("Test1");
    data.add("Test2");
    data.add("Test3");
    data.add("Test4");
    data.add("Test5");
    data.add("Test6");
//    data.add("Test7");
//    data.add("Test8");
//    data.add("Test9");
    //db.getdata(xyz);

    int dbsize=data.size();
    int i=0;
    %>


<div>
    <table border="1" align="center">


        <tr>
            <td><b>Vehicle</b></td>
            <td><b>RegNumber</b></td>
            <td><b>Status</b></td>
            <td><b>MobileNum</b></td>
            <td><b>Priority</b></td>
            <td><b>Last_seen</b></td>
        </tr>
        <% //java goes here
            //int num = data.size() % 5;
            //if(num){}
                        /*To know how many 5 columns rows are in the arraylist*/
                        for (int n = 1; n < data.size() % 5; n++ ){
            %>
        <tr>
            <% //Loop through first row of table, aka first set of strings in arraylist

                        for (String str : data){
            %>

            <td>
                <% out.print(str);%>
            </td>


            <%
                     }
                 %>
            <%
                  }
                 %>
        </tr>
    </table>
</div>
</html>

更新输出2016年1月23日: -

Vehicle RegNumber   Status  MobileNum   Priority    Last_seen
Test    Test1   Test2   Test3   Test4   Test5   Test6

在Last_seen之后以及当我添加&#34; test7,8,9&#34;时,将测试6作为附加行打印。 arraylist没有被打印出来。当我尝试从数据库返回的实际arraylist时也一样。

0 个答案:

没有答案