在JSP開發中,經常遇到數據的查詢問題,當查詢結果非常多時,就需要進行分頁顯示.有兩種方案解決,一是把所有的資料都查詢出來,然后在每頁中顯示指定資料;另一種是多次查詢數據庫,每次獲得本頁的數據.考慮到數據往往是大量甚至是海量,如果一次性的獲取,那么這些數據必然占用大量的服務器資源,使系統的性能大大降低..
=======頁面控制的JavaBean======
/*
?* JavaBean.java
?*
?* Created on 2006年4月14日, 下午3:00
?*
?* To change this template, choose Tools | Template Manager
?* and open the template in the editor.
?*/
package jspdev;
import java.util.Vector;
/**
?*
?* @author DuYang
?*/
public class PageBean {
??? public int curPage; //當前是第幾頁
??? public int maxPage; //一共有多少頁
??? public int maxRowCount; //一共多少行
??? public int rowsPerPage=5; //每頁多少行
??? public java.util.Vector data; //本頁中要顯示的資料
??? public PageBean() {
??? }
??? public void countMaxPage()
??? { //根據總行數計算總頁數
??????? if(this.maxRowCount % this.rowsPerPage==0)
??????? {
??????????? this.maxPage=this.maxRowCount/this.rowsPerPage;
??????? }
??????? else
??????? {
??????????? this.maxPage=this.maxRowCount/this.rowsPerPage+1;
??????? }
??? }
??? public Vector getResult()
??? {
??????? return this.data;?? //返回當前的資料
??? }
??? public PageBean(ContactBean contact) throws Exception
??? {
??????? this.maxRowCount=contact.getAvailableCount(); //得到總的行數
??????? this.date=contact.getResult();
??????? this.countMaxPage();
??? }
}
=========處理業務邏輯的ContactBean==========
/*
?* ContactBean.java
?*
?* Created on 2006年4月14日, 下午3:12
?*
?* To change this template, choose Tools | Template Manager
?* and open the template in the editor.
?*/
package jspdev;
import java.util.*;
import java.sql.*;
/**
?*
?* @author DuYang
?*/
public class ContactBean {
??? private Connection con;
??? Vector v;
??? /**
???? *構造方法
???? *創建數據庫連接
???? *初始化一個vector
???? */
??? public ContactBean() throws Exception {
??????? con=DatabaseConn.getConnection();
??????? v=new Vector();
??? }
??? /**
???? *返回查詢的記錄數
???? */
??? public int getAvailableCount()throws Exception
??? {
??????? int ret=0;
??????? Statement stmt=conn.createStatement();
??????? String sql="select count(*) from contact";
??????? ResultSet rest=stmt.executeQuery(sql);
??????? while(rest.next())
??????? {
??????????? ret=rest.getInt(1);
??????? }
??????? return ret;
??? }
??? /**
???? *獲得指定的頁面數據,并且封裝在PageBean中返回
???? */
??? public PageBean listDate(String page)throws Exception
??? {
????? try
????? {
????????? PageBean pageBean=new PageBean(this);
????????? int pageNum=Integer.parseInt(page);
????????? Statement stmt=conn.createStatement();
????????? /**注意: 查詢(pageNum-1)*pageBean.rowsPerPage到
?????????? *(pageNum-1)*pageBean.rowsPerPage之間的數據,
?????????? *這里只是一個簡單的實現方式
?????????? */
????????? String strSql="select top"+pageNum*pageBean.rowsPerPage+"*from contact order by userName";
????????? ResultSet rset=stmt.executeQuery(strSql);
????????? int i=0;
????????? while(rset.next())
????????? {
????????????? if(i>(pageNum-1)*pageBean.rowsPerPage-1)
????????????? {
????????????????? Object[] obj=new Object[6];
????????????????? obj[0]=rset.getString("userName");
????????????????? obj[1]=new Integer(rset.getInt("mobile"));
????????????????? obj[2]=rset.getString("phone");
????????????????? obj[3]=rset.getString("mail");
????????????????? obj[4]=rset.getDate("lastcontact");
????????????????? obj[5]=rset.getString("men");
????????????????? v.add(obj);
????????????? }
????????????? i++;
????????? }
????????? rset.close();
????????? stmt.close();
?????????
???????? pageBean.curPage=pageNum;
???????? pageBean.data=v;
???????? return pageBean;
????? }
????? catch(Exception e)
???????? {
???????????? e.printStackTrace();
???????????? throw e;
???????? }
??? }
??? /**
???? *返回結果集
???? */
??? public Vector getResult()throws Exception
??? {
??????? return v;
??? }
}
=======控制器Servlet==========
/*
?* ContactServlet.java
?*
?* Created on 2006年4月14日, 下午3:44
?*
?* To change this template, choose Tools | Template Manager
?* and open the template in the editor.
?*/
package jspdev;
import javax.servlet.http.*;
import java.io.*;
/**
?*
?* @author DuYang
?*/
public class ContactServlet extends javax.servlet.http.HttpServlet{
??? /**
???? *處理客戶端請求
???? */???
??? public void doPost(HttpServletRequest request,HttpServletResponse response)throws javax.servlet.ServletException,IOException
??? {
??????? response.setContentType("text/html");
??????? PrintWriter out=response.getWriter();
??????? try
??????? {
??????????? ContactBean contact=new ContactBean();
??????????? PageBean pageCtl=contact.listDate((String)request.getParameter("jumpPage"));
??????????? //把PageBean保存在request中
??????????? request.setAttribute("pageCtl",pageCtl);
??????? }
??????? catch(Exception e)
??????? {
??????????? e.printStackTrace();
??????? }
??????? //把試圖派發到目的
??????? javax.servlet.RequestDispatcher dis=request.getRequestDispatcher("/viewcontact");
??????? dis.forward(request,response);
??? }
?? public void doGet(HttpServletRequest request,HttpServletResponse response)throws javax.servlet.ServletException,IOException
?? {
?????? doGet(request,response);
?? }
}
=======使用翻頁的JSP文件==========
<jsp:useBean id="pageCtl" class="jspdev.PageBean" scope="request"/>
?<table border="1">
??<%
??java.util.Vector v=pageCtl.getResult();
??java.util.Enumeration e=v.elements();
??while(e.hasMoreElement())
??{
??Object[] obj=(Object)e.nextElement();
??%>
??<tr>
???<td align="center" width="95%"><%=obj[0]%></td>
???<td align="center" width="95%"><%=obj[1]%></td>
???<td align="center" width="95%"><%=obj[2]%></td>
???<td align="center" width="95%"><%=obj[3]%></td>
???<td align="center" width="95%"><%=obj[4]%></td>
???<td align="center" width="95%"><%=obj[5]%></td>???????????????
??</tr>
??<% } %>
?</table>
?<%if(pageCtl.maxPage!=1){%>
?<form name="PageForm" action="/ch13/servlet/contactservlet" method="post">
??<%@include file="/pageman.jsp"%>
?</form>
?<%}%>
======可重用的組件在你需要分頁的地方都可以使用它=======
<script language="javascript">
?<!--
?? function Jumping()
?? {
?? ?document.PageForm.submit();
?? ?return;
?? }
?? function gotoPage(pagenum)
?? {
?? ?document.PageForm.jumpPage.value=pagenum;
?? ?document.PageForm.submit();
?? ?return;
?? }
?-->
?</script>
?
?每頁<%=pageCtl.rowsPerPage%>行
?共<%=pageCtl.maxRowCount%>行
?第<%=pageCtl.curPage%>頁
?共<%=pageCtl.maxPage%>頁
?<br>
?<%if(pageCtl.curPage==1){out.print("首頁 上一頁");}
else
?{ %>
?<a href="javascript:gotoPage(1)">首頁</a>
?<a href="javascript:gogoPage(<%=pageCur.curPage-1%>)"上一頁</a>
?<%}%>
?<%if(pageCtl.curPage==pageCtl.maxPage){out.print("下一頁 尾頁");}
else
?{%>
?<a href="javascript:gotoPage(<%=pageCtl.curPage+1)">下一頁</a>
? <a href="javascript:gotoPage(<%=pageCtl.maxPage)">尾頁</a>
<%}%>
?轉到第<select name="jumpPage" onchange="Jumping()">
???? <% for(int i=1;i<pageCtl.maxPage;i++)
???? {
?????? if(i==pageCtl.curPage)
?????? {
?????? %>
?????? <option selected value=<%=i%>><%=i%></option>
????? <%} else{%>
????? <option value=<%=i%>><%=i%></option>
????? <%}}%>
???? </select>頁