分页显示的解决方案

来源:互联网 发布:网页编程工具 编辑:程序博客网 时间:2024/05/01 18:41

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

原创粉丝点击