Java基础——Servlet(六)分页相关

前面写了Servlet(一)到(五),主要是在网上搜罗的视频、对分页这块还是不太清楚。于是有找到一些视频,重新学习了一下。主要是对分页的认识和设计思路。也是为了方便我以后回忆一下。。

一、分页常识

pageSize //每页有多少条记录 --用户设定

rowCount //一共有多少条记录 --从数据库中查的

pageIndex //当前查看的是第几页 --用户选择的

beginRow //从第几行开始查询 --计算得到 pageSize*(pageIndex-1)

pageCount //总共有多少页 --计算

//栗子
if(rowCount%pageSize==0){
    pageCount=rowCount/pageSize;
    }
    else{
    pageCount=rowCount/pageSize+1
}

二、PageInfo 类

public class PageInfo {
        private int pageSize; // 页面大小
        private int pageCount; // 页面总数
        private int pageIndex; // 当前页
        private int beginRow; // 开始页
        private int rowCount; // 总条数

        private boolean hasNext; // 是否有后一页
        private boolean hasPre; // 是否有前一页

        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 getPageIndex() {
            return pageIndex;
        }

        public void setPageIndex(int pageIndex) {
            this.pageIndex = pageIndex;
        }

        public int getBeginRow() {
            return beginRow;
        }

        public void setBeginRow(int beginRow) {
            this.beginRow = beginRow;
        }

        public int getRowCount() {
            return rowCount;
        }

        public void setRowCount(int rowCount) {
            this.rowCount = rowCount;
        }

        public boolean isHasNext() {
            return hasNext;
        }

        public void setHasNext(boolean hasNext) {
            this.hasNext = hasNext;
        }

        public boolean isHasPre() {
            return hasPre;
        }

        public void setHasPre(boolean hasPre) {
            this.hasPre = hasPre;
        }

    }

三、分页工具类

public class PageUtil {
    
        public static PageInfo getPageInfo(int pageSize, int rowCount, int pageIndex) {
            PageInfo p=new PageInfo();        
            pageSize=getPageSize(pageSize);
            p.setPageSize(pageSize);
            p.setRowCount(rowCount);
            p.setPageCount(getPageCount(pageSize,rowCount));
            p.setBeginRow(getBeginRow(pageSize,pageIndex));
            p.setHasNext(isHasNext(pageIndex,getPageCount(pageSize,rowCount)));
            p.setHasPre(isHasPre(pageIndex));
            p.setPageIndex(pageIndex);
            return p;
        }
    
        //判断当是否有前一页
        private static boolean isHasPre(int pageIndex) {
            if(pageIndex==1){
                return false;
            }
            else {
                return true;
            }
        }
    
        //判断是不是有下一页
        private static boolean isHasNext(int pageIndex, int pageCount) {
            if(pageIndex==pageCount||pageCount==0){
                return false;
            }
            else{
                return true;
            }
        }
    
        //计算从第几行开始查询
        private static int getBeginRow(int pageSize, int pageIndex) {
            return pageSize*(pageIndex-1);
        }
    
        //计算页的总数
        private static int getPageCount(int pageSize, int rowCount) {
            if(rowCount%pageSize==0){
                return  rowCount/pageSize;
            }
            else{
                return rowCount/pageSize+1;
            }    
        }
    
        //返回pageSize
        private static int getPageSize(int pageSize) {
            return pageSize==0?10:pageSize;
        }
    
    }

四、页面

<script type="text/javascript">
        function subForm(){
            //form1.action="AdminServlet?flag=manage";
            form1.submit();
        }
    </script>

  <body>
  
  <form action  ="AdminServlet" method="post" name="form1">
          <input type="hidden" name=flag value="manage" />
          <table width=80% border=1 cellspacing=0s>
            <c:forEach var="u" items="${adminList}">
                <tr>
                    <td>${u.id }</td>
                    <td>${u.userName }</td>
                    <td>${u.password }</td>
                    <td>${u.note }</td>
                    <td><a href="admin/admin_edit.jsp?id=${u.id }" >修改</a> | <a  onclick="return confirm('确定要删除吗?')"
href="AdminServlet?flag=del&id=${u.id }" >删除</a></td> </tr> </c:forEach> </table> 共有 ${ pageInfo.rowCount} 条记录,当前第 ${ pageInfo.pageIndex} 页, 共 ${ pageInfo.pageCount}页 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <c:choose> <c:when test="${pageInfo.hasPre }"> <a href="AdminServlet?flag=manage&pageIndex=1" >首页</a>
<a href="AdminServlet?flag=manage&pageIndex=${pageInfo.pageIndex-1 }" >前一页 </a> </c:when> <c:otherwise> 首页 前一页 </c:otherwise> </c:choose> <c:choose> <c:when test="${pageInfo.hasNext }"> <a href="AdminServlet?flag=manage&pageIndex=${pageInfo.pageIndex+1 }" >下一页</a>
<a href="AdminServlet?flag=manage&pageIndex=${pageInfo.pageCount }" >尾页 </a> </c:when> <c:otherwise> 下一页 尾页 </c:otherwise> </c:choose> <a href="javascript:subForm()" >转到</a> 第 <input type="text" name="pageIndex" value=${pageInfo.pageIndex }></form>

五、控制层

private void manage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        int pageSize=4;
        int rowCount=_dao.getUserCount();
        
        String pageIndexStr=request.getParameter("pageIndex");
        int pageIndex=1;
        if(!StrUtil.isNullOrEmpty(pageIndexStr)){
            pageIndex=Integer.parseInt(pageIndexStr); 
        }
        
        PageInfo pageInfo=PageUtil.getPageInfo(pageSize,rowCount,pageIndex);
    
        List<UserInfo> adminList=_dao.getUserList(pageInfo);

        request.setAttribute("adminList", adminList);
        request.setAttribute("pageInfo", pageInfo);
        
        request.getRequestDispatcher("/admin/admin_manage.jsp").forward(request, response);    
    }
原文地址:https://www.cnblogs.com/1693977889zz/p/7485497.html