如何使用jstl在jsp中以表格格式显示列表

时间:2014-12-09 09:17:30

标签: java list

我有java代码,我从数据库中检索数据并将其放入列表中。但是当我运行jsp页面时,数据随机显示出来。我想以表格格式排列数据。怎么做。 我的java代码是:

public List<String> alarm_Detail()

{

     String []alarm = null;
        try
        {  
          con = getConnection();

        stmt = con.createStatement();
        String sql= "select distinct * from i2alarmlog where Ack_status=0 AND Direction='CAME' ";
        stmt.executeQuery(sql);
        rs = stmt.getResultSet();

        while(rs.next()) 
        {

            lt.add(rs.getString("device_name"));

            lt.add(rs.getString("system_name"));

            lt.add(rs.getString("alert_text"));

            lt.add(rs.getString("arrival_time"));

            lt.add(rs.getString("ack_time"));

            lt.add(rs.getString("ack_status"));

        }



        }

        catch( Exception e )
        {
            System.out.println("\nException in Display Bean in getDbTable(String code):"+e);
        }
        finally
        {
            closeConnection(stmt, rs, con);
        }

        return lt;
}`

JSP代码是: -

&#13;
&#13;
<jsp:useBean id="ab" class="alarm.Alarm_Bean">

</jsp:useBean>
<c:out value="${ab.alarm_Detail()}"></c:out>`
&#13;
&#13;
&#13;

3 个答案:

答案 0 :(得分:2)

您应该创建一个bean类,其中包含您在查询结果中获得的所有参数,如下所示

class MyBean 
{
    private String device_name;
    private String system_name;
    private String alert_text;
    private String arrival_time;
    private String ack_time;
    private String ack_status;

    //Getter and Setter method
}  

现在使用此bean类来存储您的数据 为每个记录创建新的bean对象,并将其添加到列表中。

List<MyBean> lt = new ArrayList<MyBean>(); 
while(rs.next()) 
{
    MyBean obj = new MyBean(); 

    //Set your recodrs using setter method.
    obj.setSystem_name(rs.getString("system_name"));
    obj.setArrival_time(rs.getString("arrival_time"));

    lt.add(obj);
}
//Return your list to JSP side  

现在使用这个bean列表并迭代它JSP文件来创建表,如下所示:

<table>
   <tr>
      <th>Col1</th>
      <th>Col2</th>
      <!-- rest of you columns -->
   </tr>

   <c:forEach items="${beanListObj}" var="list">
     <tr>
         <td>${list.device_name}</td>
         <td>${list.system_name} - ${list.arrival_time}</td> // you can add values in one column
         <!-- rest of you columns data-->
     </tr>
   </c:forEach>

 </table>   

答案 1 :(得分:1)

尝试c:forEach迭代你的闹钟详情如下:

<%-- Table definition and other view logic goes here -->
<c:forEach var="alarmDetails" items="${alarmDetailsList}"> <%-- assuming you set the list -->
   <tr>
       <td>${alarmDetails["arrivalTime"]}</td>
       <td>${alarmDetails["systemName"]}</td>
   </tr>
<c:forEach>

现在定义一个名为AlarmDetails的类,如下所示:

public class AlarmDetails {
    String systemName;
    String arrivalTime;//change data type as per your field type in DB
    //other fields goes here
    //getter and setter for the same
}

现在不是定义字符串列表,而是定义AlarmDetails列表,如下所示:

List<AlarmDetails> alaramDetailsList = new ArrayList<AlarmDetails>();
 AlarmDetails alarmDetails;
 while(rs.next()) 
    {
        alarmDetails = new AlarmDetails();
        alarmDetails.setSystemName(rs.getString("system_name"));
        alarmDetails.setArrivalTime(rs.getString("arrival_time"));
        //other fields
        alarmDetails.add(alarmDetails);
    }

将此方法的返回类型更改为:

public List<AlarmDetails> alarm_Detail() {

答案 2 :(得分:0)

使用表格标签并重复列表。

 <table>
  <tr>
    <th>Heading1</th>
    <th>Heading2</th>
  </tr>

<c:forEach items="${yourList}" var="i">
<tr>
    <td>${i.id}</td>
    <td>${i.name}</td>
</tr>
</c:forEach>

</table>