page wise results displaying in jsp page

2019-01-29 13:12发布

问题:

I have a Search JSP page that will accept some input fields and send query to database with respective given fields . if there are 2000 relevant records in the database, the page will show the first 30 results and a next button to show succeeding results or previous button for previous results, just like when we search in Google. Now my problem is I don't have any idea how to do these things. I know basic of servlets, JSP, JDBC. is there anyone resolve this problem? suggestions and ideas are most appreciated. code will be like this

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page import = "com.preva.vo.StoppageDetails"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<html>
<head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">

<link href="css/cal.css" rel="stylesheet" type="text/css" />
<link href="css/sty.css" rel="stylesheet" type="text/css" />
<link href="css/tabborder.css" rel="stylesheet" type="text/css" />
</head>
<body>
                 <jsp:include page="Header.jsp" />

            <table align=center border=0 cellspacing=0 cellpadding=0>
            <tr ><td colSpan=5 align=center><b>Overspeed Details</b></td></tr>
            <tr >
      <td colspan=5 align=center>
      <b><%=request.getParameter("vehicleId") %></b></td></tr>
            <tr><td>From &nbsp;
    <%=session.getAttribute("fromdate") %>
     &nbsp;to&nbsp;       
     <%=session.getAttribute("startdate") %></td></tr>

            </table><br></br>
     <table class='rptTbl_sortable' width='80%' align=center cellspacing='2'  cellpadding='0' border='0'>

          <thead>
          <tr class="rptHdrRow">
         <th id="index" class="rptHdrCol_sort" nowrap>DeviceID</th>
         <th id="date" class="rptHdrCol_sort" nowrap>Date</th>
         <th id="time" class="rptHdrCol_sort" nowrap>Speed</th>
         <th id="statusdesc" class="rptHdrCol_sort" nowrap>Status</th>
                 <th id="address" class="rptHdrCol_sort" nowrap>Address</th>
</tr>
</thead>
<tbody>

<c:forEach items="${sessionScope.overspeeddetails}" var="overspeeddetailsvar">
<tr class="rptBodyRowOdd">
<td><c:out value="${overspeeddetailsvar.deviceID}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.TIMESTAMP}"></c:out></td>   
<td><c:out value="${overspeeddetailsvar.speed}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.statuscode}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.address}"></c:out></td>

</tr>
<tr class="rptBodyRowEven">
<td><c:out value="${overspeeddetailsvar.deviceID}"> </c:out>  </td>
<td><c:out value="${overspeeddetailsvar.TIMESTAMP}"></c:out></td>   
<td> <c:out value="${overspeeddetailsvar.speed}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.statuscode}"></c:out></td>
<td><c:out value="${overspeeddetailsvar.address}"></c:out></td>

</tr>   </c:forEach>    
    </tbody>            


        </table> 


 </body>

  </html>

bean and Dao classes will be like this

public class OverspeedDetails {
private String  deviceID,timestamp,statuscode,address;
private double speed;
public double getSpeed() {
    return speed;
}

public void setSpeed(double speed) {
    this.speed = speed;
}

public String getDeviceID() {
    return deviceID;
}

public void setDeviceID(String deviceID) {
    this.deviceID = deviceID;
}

public String getTIMESTAMP() {
    return timestamp;
}

public void setTIMESTAMP(String TIMESTAMP) {
    this.timestamp = TIMESTAMP;
}

public String getStatuscode() {
    return statuscode;
}

public void setStatuscode(String statuscode) {
    this.statuscode = statuscode;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

public List<OverspeedDetails> getosDetails(String accountID,String deviceID,String   Timestamp1,String Timestamp2,double speed) {
        Connection con=null;
        List<OverspeedDetails> overspeeddetail = new ArrayList<OverspeedDetails>();
        try{


            con= DBConnectionFactory.getDBConnection();

            String sql="SELECT deviceID,TIMESTAMP,speedKPH,statuscode,address  FROM eventdata WHERE (TIMESTAMP BETWEEN '"+Timestamp1+"' AND '"+Timestamp2+"') AND accountID='"+accountID+"' AND deviceID='"+deviceID+"'and speedKPH >"+speed+";";
            Statement st=con.createStatement();
            ResultSet rs=st.executeQuery(sql);
            System.out.println("Dao over"+rs.next());
            while (rs.next()){
                OverspeedDetails od=new OverspeedDetails();
                od.setDeviceID(rs.getString(1));
                String stringtimestamp=rs.getString(2);
                 long l=Long.parseLong(stringtimestamp);
                 long longtimestamp = l * 1000L;
                 String str = new java.text.SimpleDateFormat("dd/MM/yyyy").format(new java.util.Date(longtimestamp));

                    od.setTIMESTAMP(str);
                od.setSpeed(rs.getDouble(3));
                od.setStatuscode(rs.getString(4));
                String add=rs.getString(5);
                String add1[]=add.split(" \\d");
                //String add2=java.util.Arrays.toString(add.split(" "));
                od.setAddress(add1[0]);
                overspeeddetail.add(od);
              } 

            }catch (Exception e) {
                e.printStackTrace();

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


        }

回答1:

You can use displaytag

The display tag library is an open source suite of custom tags that provide high-level web presentation patterns which will work in an MVC model. The library provides a significant amount of functionality while still being easy to use.

You need to download jar from this site displaytag-1.2.jar

Using display tag you can export the data in various formats e.g. Excel, PDF ect.
Column sorting is just on click on the column name link.

There are lot of examples on the site http://displaytag.org

JSP code :

<%@ taglib uri="http://displaytag.sf.net" prefix="display" %>

There are two ways to get the list in JSP
1) Using jstl: (Recommended)

Just use the sql:query tag and pass the result to the table tag in this way

    <sql:query var="results">
      select * from table
    </sql:query>

    <display:table name="${results.rows}" />

    (or
    <display:table name="pageScope.results.rows" />
    if not using the EL version)

2) Using dynabeans

<%
  Connection con = ...; // just open a connection

  Statement stmt = con.createStatement();
  ResultSet rs = stmt.executeQuery("SELECT * from table");
  RowSetDynaClass resultSet = new RowSetDynaClass(rs, false);
  stmt.close();
  con.close();
  request.setAttribute("results", resultSet);
%>

  <display:table name="requestScope.results.rows" />  

You can use either way but first JSTL is recommended.

You can use sessionScope

  <display:table id="deviceDetailsID" name="sessionScope.overspeeddetails" pagesize="10" style="width:99%;">

    <display:column property="deviceID" title="Device ID" sortable="true" headerClass="sortable" style="width: 1%"/>
    <display:column property="TIMESTAMP" title="TIMESTAMP" sortable="true" headerClass="sortable" format="{0,date,dd-MM-yyyy}"/>
    <display:column property="speed" title="Speed" sortable="true"/>
    <display:column property="statuscode" title="Status Code"/>
    <display:column property="address" title="Address" sortable="true" headerClass="sortable" />
</display:table>  

To use <sql:query> add JSTL 1.2 jar in classpath and on JSP

<%@ taglib prefix="sql" uri="http://java.sun.com/jsp/jstl/sql" %>

You need to add Commons Lang 2.6 jar in your class path to avoid this

java.lang.ClassNotFoundException: org.apache.commons.lang.UnhandledException

See also

  1. How to avoid Java Code in JSP-Files?
  2. JSTL sql
  3. Tag query
  4. display tag install guide
  5. displaytag-export-poi 1.2 jar to use export option
  6. displaytag-portlet 1.2 jar