Servlet實現后台分頁查詢


相信大家在搭建后台的時候,經常會使用到分頁功能,當然,目前有不少框架(如esayUI)都自帶分頁的實現,為了更好的理解分頁原理,近期本人自己摸索了關於分頁查詢的一些心得。

歸根結底,分頁的核心還是在封裝PageBean,並通過一定的算法對其進行判斷,賦值

 

public class PageBean<T> implements Serializable{
    /**
     * 
     */
    private static final long serialVersionUID = 1L;
    private int currentPage; //當前第幾頁 ,請求傳過來
     private int pageCount;  // 每頁顯示多少條數據
     private int totalCount; //總記錄數,查詢數據獲得
     
     private int totalPage;  //總頁數, 通過  totalCount 和 pageCount計算獲得
     
     private int startIndex; //開始索引,與pageCount 組成 limit 條件
     
     private List<T> pageData;
     
     //分頁顯示的頁數,如 1,2,3,4
     private int start;
     private int end;
     
     public PageBean(int currentPage,int pageCount,int totalCount){
         this.currentPage=currentPage;
         this.pageCount=pageCount;
         this.totalCount=totalCount;
         
         if(totalCount % pageCount==0){
             this.totalPage = totalCount/ pageCount;
         }else{
             this.totalPage= totalCount/pageCount+1;
         }
         
         this.startIndex = (currentPage-1)*pageCount;
         
         this.start=1;
         this.end=5;
         
         if(pageCount<=5){
             this.end= this.totalPage;
         }else{

             this.start=currentPage-2;
             this.end = currentPage+2;
             
             if(start<=0){
                 this.start=1;
                 this.end=5;
             }
             if(this.end > this.totalPage){
                 this.end=totalPage;
                 this.start=end-4;
             }
         }
     }

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageCount() {
        return pageCount;
    }

    public void setPageCount(int pageCount) {
        this.pageCount = pageCount;
    }

    public int getTotalCount() {
        return totalCount;
    }

    public void setTotalCount(int totalCount) {
        this.totalCount = totalCount;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public int getStartIndex() {
        return startIndex;
    }

    public void setStartIndex(int startIndex) {
        this.startIndex = startIndex;
    }

    public List<T> getPageData() {
        return pageData;
    }

    public void setPageData(List<T> pageData) {
        this.pageData = pageData;
    }

    public int getStart() {
        return start;
    }

    public void setStart(int start) {
        this.start = start;
    }

    public int getEnd() {
        return end;
    }

    public void setEnd(int end) {
        this.end = end;
    }

    public static long getSerialversionuid() {
        return serialVersionUID;
    }
     
     
     
}

 

 

Controller層

1         int currentPage = Integer.valueOf(request.getParameter("currentPage"));
2         
3         int pageCount=7;
4         
5         BlogTypeService blogType = new BlogTypeServiceImpl();
6         
7         PageBean<BlogType> page = blogType.page(currentPage, pageCount);
8         request.setAttribute("page", page);
9         request.getRequestDispatcher("/jsp/blogType.jsp").forward(request, response);

service層

public PageBean<BlogType> page(int currentPage,int pageCount) {
        int totalCount=blogType.getTotal();
        
        PageBean<BlogType> pageBean = new PageBean<>(currentPage, pageCount, totalCount);
        int startIndex = pageBean.getStartIndex();
        pageBean.setPageData(blogType.page(startIndex, pageCount));
        return pageBean;
    }

dao層

public List<BlogType> page(int currentPage,int pageCount){
        String sql = "select * from t_blogtype limit ?,?";
        PageBean<BlogType> pageBean =null;
        Connection conn =null;
        PreparedStatement ps =null;
        ResultSet rs =null;
        BlogType blogType =null;
        try {
            conn =DBDao.connection();
            ps=conn.prepareStatement(sql);
            ps.setInt(1, currentPage);
            ps.setInt(2, pageCount);
            rs=ps.executeQuery();
            List<BlogType> list =new ArrayList<BlogType>();
            while(rs.next()){
                blogType = new BlogType();
                blogType.setId(rs.getInt("id"));
                blogType.setTypeName(rs.getString("typeName"));
                blogType.setOrderNum(pageCount);
                list.add(blogType);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public int getTotal() {
        String sql ="select count(id) from t_blogtype";
        Connection conn =null;
        PreparedStatement ps=null;  
        ResultSet rs=null;  
        int count=0;  
        try{  
            conn= DBDao.connection();
            ps=conn.prepareStatement(sql);  
            rs=ps.executeQuery();  
            rs.next();  
            count=rs.getInt(1);
              
        }catch(Exception e){
            e.printStackTrace();
        }
        return count;  
        
        
    }

   因為本文主要在說分頁查詢,所以關於 數據庫的連接就一並放到dao里了!

  (其實本人是不知道將數據庫的連接放在哪里。。。。嘻嘻)

 

jsp

 1 <body>
 2     共有${page.totalCount }個分類,共有${page.pageCount }頁,當前為第${page.currentPage }頁,每頁顯示${page.pageCount }條
 3     <table align="center" width="100%">
 4         <thead>
 5             <th>選擇</th>
 6             <th>序號</th>
 7             <th>博客類別</th>
 8             <th>博客排序</th>
 9             <th>操作</th>
10         </thead>
11         <tbody align="center" border="1">
12             <c:forEach items="${page.pageData }" var="page">
13                 <tr>
14                 <td><input  type="checkbox" name="id"/></td>
15                 <td>${page.id }</td>
16                 <td>${page.typeName }</td>
17                 <td>${page.orderNum }</td>
18                 <td><a>修改</a>|<a>刪除</a></td>
19                 <td></td>
20             </tr>
21             </c:forEach>
22         </tbody>
23     
24     </table>
25     <!-- 分頁 -->
26     <div style="text-align:center;">
27         <a href="${pageContext.request.contextPath }/blogType?currentPage=1">首頁</a>
28         <!-- 如果當前頁為第一頁,就沒有上一頁這個標簽 -->
29         <c:if test="${page.currentPage==1 }">
30             <c:forEach begin="${page.start }" end="${page.end }" step="1" var="i">
31                 <c:if test="${page.currentPage == i}">
32                         ${i}
33                 </c:if>                
34                 <c:if test="${page.currentPage != i}">
35                         <a href="${pageContext.request.contextPath}/blogType?currentPage=${i}">${i}</a>                                        
36                  </c:if> 
37             </c:forEach>
38             <a href="${pageContext.request.contextPath }/blogType?currentPage=${page.currentPage+1}">下一頁</a>
39         </c:if>
40         <!-- 如果不是首頁也不是尾頁,就與上一頁和下一頁 -->
41         <c:if test="${page.currentPage>1 && page.currentPage<page.totalPage }">
42             <a href="${pageContext.request.contextPath }/blogType?currentPage=${page.currentPage-1}">上一頁</a>
43             <c:forEach begin="${page.start }" end="${page.end }" step="1" var="i">
44                 <c:if test="${page.currentPage == i}">
45                         ${i}
46                 </c:if>                
47                 <c:if test="${page.currentPage != i}">
48                         <a href="${pageContext.request.contextPath}/blogType?currentPage=${i}">${i}</a>                                        
49                  </c:if> 
50             </c:forEach>
51             <a href="${pageContext.request.contextPath }/blogType?currentPage=${page.currentPage+1}">下一頁</a>
52         </c:if>
53         <!-- 如果是最后一頁,則沒有下一頁 -->
54         <c:if test="${page.currentPage==page.totalPage }">
55             <a href="${pageContext.request.contextPath }/blogType?currentPage=${page.currentPage-1}">上一頁</a>
56             <c:forEach begin="${page.start }" end="${page.end }" step="1" var="i">
57                 <c:if test="${page.currentPage == i}">
58                         ${i}
59                 </c:if>                
60                 <c:if test="${page.currentPage != i}">
61                         <a href="${pageContext.request.contextPath}/blogType?currentPage=${i}">${i}</a>                                        
62                  </c:if> 
63             </c:forEach>
64             
65         </c:if>
66         <a href="${pageContext.request.contextPath }/blogType?currentPage=${page.totalPage}">尾頁</a>
67     </div>
68     
69 </body>

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM