页面明智的结果显示在jsp页面中

时间:2013-10-30 09:21:11

标签: jsp pagination jstl

我有一个Search JSP页面,它接受一些输入字段并使用相应的给定字段向数据库发送查询。如果数据库中有2000个相关记录,该页面将显示前30个结果,下一个按钮显示后续结果或之前结果的上一个按钮,就像我们在Google中搜索一样。 现在我的问题是我不知道如何做这些事情。我知道servlet,JSP,JDBC的基础知识。有没有人解决这个问题?建议和想法非常感谢。 代码将是这样的

<%@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和Dao类将是这样的

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 个答案:

答案 0 :(得分:2)

您可以使用displaytag

  

显示标记库是一个开源的自定义标记套件,提供可在MVC模型中使用的高级Web演示模式。该库提供了大量功能,同时仍然易于使用。

您需要从this site displaytag-1.2.jar

下载jar

使用显示标记,您可以以各种格式导出数据,例如Excel,PDF等 只需单击列名链接即可进行列排序。

网站上有很多例子http://displaytag.org

JSP代码:

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

有两种方法可以在JSP中获取列表 1)使用jstl :(推荐)

只需使用sql:query标记,然后以这种方式将结果传递给表格标记

    <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)使用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" />  

您可以使用任何一种方式,但建议使用第一种JSTL。

您可以使用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>  

在类路径和JSP中使用<sql:query>添加JSTL 1.2 jar

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

您需要在类路径中添加Commons Lang 2.6 jar以避免此

  

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

另见

  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