• 关于jsp+servlet MVC模式中的分页操作


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

    先创建JavaBean

     1 import java.util.ArrayList;
     2 
     3 public class PageBean {
     4     private int pageNow;//第几页
     5     private int pageSize;//每页显示几个记录
     6     private int pageCount;//总页数
     7     private int rowCount;//总记录数
     8     private ArrayList al;//该页要显示的记录
     9     
    10     public PageBean() {
    11         
    12     }
    13     public int getPageNow() {
    14         return pageNow;
    15     }
    16     public void setPageNow(int pageNow) {
    17         this.pageNow = pageNow;
    18     }
    19     public int getPageSize() {
    20         return pageSize;
    21     }
    22     public void setPageSize(int pageSize) {
    23         this.pageSize = pageSize;
    24     }
    25     public int getPageCount() {
    26         return pageCount;
    27     }
    28     public void setPageCount(int pageCount) {
    29         this.pageCount = pageCount;
    30     }
    31     public int getRowCount() {
    32         return rowCount;
    33     }
    34     public void setRowCount(int rowCount) {
    35         this.rowCount = rowCount;
    36     }
    37     public ArrayList getAl() {
    38         return al;
    39     }
    40     public void setAl(ArrayList al) {
    41         this.al = al;
    42     }
    43     
    44 }

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

     1 //我以对论坛中的帖子分页为例,显示pid=0的帖子并分页,Article是帖子类
     2 public static void getPageInfo(PageBean pb){
     3     int pageNow = pb.getPageNow();//获得要显示的页数
     4     int pageSize = pb.getPageSize();//获得每页显示的记录数
     5     int rowCount = 0;
     6     int pageCount = 0;
     7     ArrayList al = new ArrayList();
     8     try {
     9         ct = getConnection();
    10         ps=ct.prepareStatement("select top "+pageSize
    11                 +" * from article where  pid = 0 and id not in (select top "
    12                 +pageSize*(pageNow-1)+" id from article where pid = 0) ");
    13         rs = ps.executeQuery();
    14         while (rs.next()){
    15             Article article= new Article();
    16             article.setId(rs.getInt(1));
    17             article.setPid(rs.getInt(2));
    18             article.setRootId(rs.getInt(3));
    19             article.setTitle(rs.getString(4));
    20             article.setCont(rs.getString(5));
    21             article.setPdate(rs.getTimestamp(6));
    22             article.setIsLeaf(rs.getInt(7));
    23             al.add(article);
    24         }
    25         pb.setAl(al);//赋值要显示页的记录
    26         ps = ct.prepareStatement("select count(*) from article where pid = 0");
    27         rs = ps.executeQuery();
    28         if (rs.next()){
    29             rowCount = rs.getInt(1);//得到记录总数
    30         }
    31         pageCount = (rowCount - 1) / pageSize + 1;//得到页总数
    32         pb.setPageCount(pageCount);
    33         pb.setRowCount(rowCount);
    34     } catch (Exception e) {
    35         e.printStackTrace();
    36         throw new RuntimeException(e.getMessage());
    37     } finally {
    38         close(rs, ps, ct);
    39     }
    40 }

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

     1 //我换了另一种思路,就是先把pid=0的帖子都取出来,再取出其中要显示页的记录封装到ArrayList中
     2 //上面一种是先把要显示页的记录取出来,再把记录封装到ArrayList中
     3 public static void getPageInfo(PageBean pb){
     4     int pageNow = pb.getPageNow();//获得要显示的页数
     5     int pageSize = pb.getPageSize();//获得每页显示的记录数
     6     int rowCount = 0;
     7     int pageCount = 0;
     8     ArrayList al = new ArrayList();
     9     try {
    10         ct = getConnection();
    11         ps=ct.prepareStatement("select * from article pid = 0"); 
    12         rs = ps.executeQuery();
    13         int startPos = (pageNow - 1 ) * pageSize;
    14         int i = 0;
    15         while (rs.next()){
    16             if (i == startPos){
    17                 for (int j = 0; j < pageSize; j++){
    18                     Article article= new Article();
    19                     article.setId(rs.getInt(1));
    20                     article.setPid(rs.getInt(2));
    21                     article.setRootId(rs.getInt(3));
    22                     article.setTitle(rs.getString(4));
    23                     article.setCont(rs.getString(5));
    24                     article.setPdate(rs.getTimestamp(6));
    25                     article.setIsLeaf(rs.getInt(7));
    26                     al.add(article);
    27                     if (!rs.next()){
    28                         break;
    29                     }
    30                 }
    31                 break;
    32             }
    33             i++;
    34         }
    35         pb.setAl(al);//赋值要显示页的记录
    36         ps = ct.prepareStatement("select count(*) from article where pid = 0");
    37         rs = ps.executeQuery();
    38         if (rs.next()){
    39             rowCount = rs.getInt(1);//得到记录总数
    40         }
    41         pageCount = (rowCount - 1) / pageSize + 1;//得到页总数
    42         pb.setPageCount(pageCount);
    43         pb.setRowCount(rowCount);
    44     } catch (Exception e) {
    45         e.printStackTrace();
    46         throw new RuntimeException(e.getMessage());
    47     } finally {
    48         close(rs, ps, ct);
    49     }
    50 }


    使用举例:

     1 int pageNow = 1;
     2 int pageSize = 4;
     3 String pagenow = request.getParameter("pageNow");
     4 if (pagenow != null){
     5     pageNow = Integer.parseInt(pagenow);
     6 }
     7 PageBean pb = new PageBean();
     8 pb.setPageNow(pageNow);
     9 pb.setPageSize(pageSize);
    10 SqlHelper.getPageInfo(pb);
    11 request.setAttribute("pagebean", pb);
    12 request.getRequestDispatcher("/XXX.jsp").forward(request, response);

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

  • 相关阅读:
    .NET: 如何在宿主中动态加载所有的服务
    SharePoint : 自定义权限设置中可能遇到的问题
    在javascript中进行cookie的读写操作
    .NET : 如何读取数据库中的bmp图片数据
    Microsoft Security Essential
    利用ashx和ajax实现表格的异步填充
    IPV6
    Windows 7 : 开发人员资源
    SQL Server : 使用SQL Express的User Instance特性
    .NET : 如何在Windows Forms中使用DataRepeater控件
  • 原文地址:https://www.cnblogs.com/miraclesnow/p/3086134.html
Copyright © 2020-2023  润新知