关于java Web分页操作

     大二的计科专业学生,感觉课堂上学的不够,所以自学了jsp+servlet,跟着视频做网上商城和论坛的项目,用的是mvc模式,这当中都涉及到了分页操作,我看完后觉得比较好的是一种是这样的:

先创建JavaBean

import java.util.ArrayList;
 
 public class PageBean {
     private int pageNow;//第几页
     private int pageSize;//每页显示几个记录
     private int pageCount;//总页数
     private int rowCount;//总记录数
     private ArrayList al;//该页要显示的记录
     
     public PageBean() {
         
     }
     public int getPageNow() {
         return pageNow;
     }
     public void setPageNow(int pageNow) {
         this.pageNow = pageNow;
     }
     public int getPageSize() {
         return pageSize;
     }
     public void setPageSize(int pageSize) {
         this.pageSize = pageSize;
     }
     public int getPageCount() {
         return pageCount;
     }
     public void setPageCount(int pageCount) {
         this.pageCount = pageCount;
     }
     public int getRowCount() {
         return rowCount;
     }
     public void setRowCount(int rowCount) {
         this.rowCount = rowCount;
     }
     public ArrayList getAl() {
         return al;
     }
     public void setAl(ArrayList al) {
         this.al = al;
     }
     
 }

 然后是一个操作数据库的工具类,其中进行分页的方法是:

//我以对论坛中的帖子分页为例,显示pid=0的帖子并分页,Article是帖子类
 public static void getPageInfo(PageBean pb){
     int pageNow = pb.getPageNow();//获得要显示的页数
     int pageSize = pb.getPageSize();//获得每页显示的记录数
     int rowCount = 0;
     int pageCount = 0;
     ArrayList al = new ArrayList();
     try {
         ct = getConnection();
         ps=ct.prepareStatement("select top "+pageSize
                 +" * from article where  pid = 0 and id not in (select top "
                 +pageSize*(pageNow-1)+" id from article where pid = 0) ");
         rs = ps.executeQuery();
         while (rs.next()){
             Article article= new Article();
             article.setId(rs.getInt(1));
             article.setPid(rs.getInt(2));
             article.setRootId(rs.getInt(3));
             article.setTitle(rs.getString(4));
             article.setCont(rs.getString(5));
             article.setPdate(rs.getTimestamp(6));
             article.setIsLeaf(rs.getInt(7));
             al.add(article);
         }
         pb.setAl(al);//赋值要显示页的记录
         ps = ct.prepareStatement("select count(*) from article where pid = 0");
         rs = ps.executeQuery();
         if (rs.next()){
             rowCount = rs.getInt(1);//得到记录总数
         }
         pageCount = (rowCount - 1) / pageSize + 1;//得到页总数
         pb.setPageCount(pageCount);
         pb.setRowCount(rowCount);
     } catch (Exception e) {
         e.printStackTrace();
         throw new RuntimeException(e.getMessage());
     } finally {
         close(rs, ps, ct);
     }
 }

 在昨天晚上,我突然想到了另外一种思路:

//我换了另一种思路,就是先把pid=0的帖子都取出来,再取出其中要显示页的记录封装到ArrayList中
 //上面一种是先把要显示页的记录取出来,再把记录封装到ArrayList中
 public static void getPageInfo(PageBean pb){
     int pageNow = pb.getPageNow();//获得要显示的页数
     int pageSize = pb.getPageSize();//获得每页显示的记录数
     int rowCount = 0;
     int pageCount = 0;
     ArrayList al = new ArrayList();
     try {
         ct = getConnection();
         ps=ct.prepareStatement("select * from article pid = 0"); 
         rs = ps.executeQuery();
         int startPos = (pageNow - 1 ) * pageSize;
         int i = 0;
         while (rs.next()){
             if (i == startPos){
                 for (int j = 0; j < pageSize; j++){
                     Article article= new Article();
                     article.setId(rs.getInt(1));
                     article.setPid(rs.getInt(2));
                     article.setRootId(rs.getInt(3));
                     article.setTitle(rs.getString(4));
                     article.setCont(rs.getString(5));
                     article.setPdate(rs.getTimestamp(6));
                     article.setIsLeaf(rs.getInt(7));
                     al.add(article);
                     if (!rs.next()){
                         break;
                     }
                 }
                 break;
             }
             i++;
         }
         pb.setAl(al);//赋值要显示页的记录
         ps = ct.prepareStatement("select count(*) from article where pid = 0");
         rs = ps.executeQuery();
         if (rs.next()){
             rowCount = rs.getInt(1);//得到记录总数
         }
         pageCount = (rowCount - 1) / pageSize + 1;//得到页总数
         pb.setPageCount(pageCount);
         pb.setRowCount(rowCount);
     } catch (Exception e) {
         e.printStackTrace();
         throw new RuntimeException(e.getMessage());
     } finally {
         close(rs, ps, ct);
     }
 }

 使用举例:

int pageNow = 1;
 int pageSize = 4;
 String pagenow = request.getParameter("pageNow");
 if (pagenow != null){
     pageNow = Integer.parseInt(pagenow);
 }
 PageBean pb = new PageBean();
 pb.setPageNow(pageNow);
 pb.setPageSize(pageSize);
 SqlHelper.getPageInfo(pb);
 request.setAttribute("pagebean", pb);
 request.getRequestDispatcher("/XXX.jsp").forward(request, response);

上述就是我对分页操作的一点想法,一种是先把pid=0的帖子都取出来,再取出其中要显示页的记录封装到ArrayList中,另一种是先把要显示页的记录取出来,再把记录封装到ArrayList中。不知道实际开发中是怎么进行分页操作的,这仅是我自学后的一点点思考,希望前辈们给我点意见。

猜你喜欢

转载自miraclesnow.iteye.com/blog/1871559