简体   繁体   中英

how to display list in tabular format in jsp using jstl

I have java code in which I'm retrieving data from database and putting it in list.But when I run the jsp page then the data is dispalyed in random wa.I want to arrange that data in tabular format .How to do that. MY java code is:

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 CODE IS:-

 <jsp:useBean id="ab" class="alarm.Alarm_Bean"> </jsp:useBean> <c:out value="${ab.alarm_Detail()}"></c:out>` 

You should create a bean class that contain all parameter you got as result of the query like below

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
}  

Now use this bean class to store your data.
Create new bean object for every records and add this to your list.

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  

Now use this bean list and iterate it JSP file to create table as show below :

<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>   

Try c:forEach to iterate over your alarm details as below:

<%-- 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>

Now define a class called AlarmDetails like below:

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
}

Now instead of defining list of Strings, define list of AlarmDetails like below:

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);
    }

Change the return type of this method to :

public List<AlarmDetails> alarm_Detail() {

Use table tag and iterate your list.

 <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> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM