java分页 Struts+Ibatis



首先写一个分页类:

  1. package com.zc.book.util;  
  2.   
  3. import java.util.List;  
  4.   
  5. public class PageUtil {  
  6.   
  7.      private int pageSize=20;//每页显示的条数 默认20  
  8.        
  9.      private int recordCount;//记录总数  
  10.        
  11.      private int currentPage;//当前页  
  12.        
  13.      private int pageIndex;//每页的第一条记录编号  
  14.        
  15.      private List pageList;//每页的记录集  
  16.        
  17.      private int totalPage;//总页数  
  18.   
  19.     public int getPageSize() {  
  20.         return pageSize;  
  21.     }  
  22.   
  23.     public void setPageSize(int pageSize) {  
  24.         this.pageSize = pageSize;  
  25.     }  
  26.   
  27.     public int getRecordCount() {  
  28.         return recordCount;  
  29.     }  
  30.   
  31.     public void setRecordCount(int recordCount) {  
  32.         this.recordCount = recordCount;  
  33.     }  
  34.   
  35.     public int getCurrentPage() {  
  36.         return currentPage;  
  37.     }  
  38.   
  39.     public void setCurrentPage(int currentPage) {  
  40.         this.currentPage = currentPage;  
  41.     }  
  42.   
  43.     public int getPageIndex() {  
  44.         return pageIndex;  
  45.     }  
  46.   
  47.     public void setPageIndex(int pageIndex) {  
  48.         this.pageIndex = pageIndex;  
  49.     }  
  50.   
  51.     public List getPageList() {  
  52.         return pageList;  
  53.     }  
  54.   
  55.     public void setPageList(List pageList) {  
  56.         this.pageList = pageList;  
  57.     }  
  58.   
  59.     public int getTotalPage() {  
  60.         return totalPage;  
  61.     }  
  62.   
  63.     public void setTotalPage(int totalPage) {  
  64.         this.totalPage = totalPage;  
  65.     }  
  66.       
  67.     //初始化 currentPage 当前页,pageSize 每页大小,recordCount 总记录数,pageIndex 每页第一条记录序号  
  68.     public void init(int currentPage,int pageSize,int recordCount){  
  69.         this.currentPage=currentPage>0?currentPage:1;//设置当前页  
  70.         this.pageSize=pageSize;//设置每页大小  
  71.         this.recordCount=recordCount;//设置总记录数  
  72.         this.pageIndex=(currentPage-1)*pageSize;//设置每页第一条记录  
  73.         this.totalPage=recordCount/pageSize+1;  
  74.         if(currentPage>totalPage){  
  75.             currentPage=totalPage;  
  76.         }  
  77.     }  
  78. }  

在Action中

  1. int currentPage = 1;  
  2.           
  3.         try {  
  4.             currentPage = Integer.parseInt(request.getParameter("currentPage"));  
  5.         } catch(Exception ex) {  
  6.         }  
  7.           
  8.         PageUtil pageUtil=new PageUtil();  
  9.           
  10.         pageUtil=bookService.pageBookInfo(currentPage);  
  11.         request.setAttribute("pageUtil", pageUtil);  
  12.   
  13.         return mapping.findForward("bookInfo");  

service中的pageBookInfo方法:

 

  1. public PageUtil pageBookInfo(int currentPage) {  
  2.         // TODO Auto-generated method stub  
  3.         List list=bookDao.bookInfo();  
  4.           
  5.         PageUtil pageUtil=new PageUtil();  
  6.           
  7.         //设置当前页,每页记录数,总记录数  
  8.         pageUtil.init(currentPage, 25, list.size());  
  9.           
  10.         //通过pageUtil的"pageIndex--每页的第一条记录编号"和"pageSize--每页显示的条数"  
  11.         //这两个参数去数据库中查询出其中一页的记录,放入List里  
  12.         list=bookDao.pageBook(pageUtil);  
  13.           
  14.         //将list中一页的记录放入pageUtil  
  15.         pageUtil.setPageList(list);  
  16.           
  17.         return pageUtil;  
  18.     }  

sqlMap中的语句:

  1.   
  2.     select * from bookinfo  
  3.   
  4.   
  5.   
  6.     select * from bookinfo limit #pageIndex#,#pageSize#  
  7.   

在jsp中添加:

  1.   
  2.             
  3.             共${pageUtil.recordCount }条纪录,当前第${pageUtil.currentPage }/${pageUtil.totalPage }页,每页${pageUtil.pageSize }条纪录  
  4.               
  5.                 
  6.                   
  7.              
  8.                   
  
  •                   
  •   
  •                     
  •              
  •              
  •                   book.do?method=page¤tPage=1" target='I1'>  
  •                   book.do?method=page¤tPage=${pageUtil.currentPage-1 }" target='I1'>  
  •              
  •              
  •                     
  •                     
  •              
  •              
  •                   book.do?method=page¤tPage=${pageUtil.currentPage+1 }" target='I1'>  
  •                   book.do?method=page¤tPage=${pageUtil.totalPage }" target='I1'>  
  •              
  •                   转到第
  •   
  •                     
  •                       
  •                       
  •                       
  •                       
  •                           
  •                       
  •                       
  •                       
  •                     
  •                   页  
  •                     
  •                   
  •                 
  •             
  •   
  •   
  •             
  •           
    1.   

    首先写一个分页类:

    1. package com.zc.book.util;  
    2.   
    3. import java.util.List;  
    4.   
    5. public class PageUtil {  
    6.   
    7.      private int pageSize=20;//每页显示的条数 默认20  
    8.        
    9.      private int recordCount;//记录总数  
    10.        
    11.      private int currentPage;//当前页  
    12.        
    13.      private int pageIndex;//每页的第一条记录编号  
    14.        
    15.      private List pageList;//每页的记录集  
    16.        
    17.      private int totalPage;//总页数  
    18.   
    19.     public int getPageSize() {  
    20.         return pageSize;  
    21.     }  
    22.   
    23.     public void setPageSize(int pageSize) {  
    24.         this.pageSize = pageSize;  
    25.     }  
    26.   
    27.     public int getRecordCount() {  
    28.         return recordCount;  
    29.     }  
    30.   
    31.     public void setRecordCount(int recordCount) {  
    32.         this.recordCount = recordCount;  
    33.     }  
    34.   
    35.     public int getCurrentPage() {  
    36.         return currentPage;  
    37.     }  
    38.   
    39.     public void setCurrentPage(int currentPage) {  
    40.         this.currentPage = currentPage;  
    41.     }  
    42.   
    43.     public int getPageIndex() {  
    44.         return pageIndex;  
    45.     }  
    46.   
    47.     public void setPageIndex(int pageIndex) {  
    48.         this.pageIndex = pageIndex;  
    49.     }  
    50.   
    51.     public List getPageList() {  
    52.         return pageList;  
    53.     }  
    54.   
    55.     public void setPageList(List pageList) {  
    56.         this.pageList = pageList;  
    57.     }  
    58.   
    59.     public int getTotalPage() {  
    60.         return totalPage;  
    61.     }  
    62.   
    63.     public void setTotalPage(int totalPage) {  
    64.         this.totalPage = totalPage;  
    65.     }  
    66.       
    67.     //初始化 currentPage 当前页,pageSize 每页大小,recordCount 总记录数,pageIndex 每页第一条记录序号  
    68.     public void init(int currentPage,int pageSize,int recordCount){  
    69.         this.currentPage=currentPage>0?currentPage:1;//设置当前页  
    70.         this.pageSize=pageSize;//设置每页大小  
    71.         this.recordCount=recordCount;//设置总记录数  
    72.         this.pageIndex=(currentPage-1)*pageSize;//设置每页第一条记录  
    73.         this.totalPage=recordCount/pageSize+1;  
    74.         if(currentPage>totalPage){  
    75.             currentPage=totalPage;  
    76.         }  
    77.     }  
    78. }  

    在Action中

    1. int currentPage = 1;  
    2.           
    3.         try {  
    4.             currentPage = Integer.parseInt(request.getParameter("currentPage"));  
    5.         } catch(Exception ex) {  
    6.         }  
    7.           
    8.         PageUtil pageUtil=new PageUtil();  
    9.           
    10.         pageUtil=bookService.pageBookInfo(currentPage);  
    11.         request.setAttribute("pageUtil", pageUtil);  
    12.   
    13.         return mapping.findForward("bookInfo");  

    service中的pageBookInfo方法:

     

    1. public PageUtil pageBookInfo(int currentPage) {  
    2.         // TODO Auto-generated method stub  
    3.         List list=bookDao.bookInfo();  
    4.           
    5.         PageUtil pageUtil=new PageUtil();  
    6.           
    7.         //设置当前页,每页记录数,总记录数  
    8.         pageUtil.init(currentPage, 25, list.size());  
    9.           
    10.         //通过pageUtil的"pageIndex--每页的第一条记录编号"和"pageSize--每页显示的条数"  
    11.         //这两个参数去数据库中查询出其中一页的记录,放入List里  
    12.         list=bookDao.pageBook(pageUtil);  
    13.           
    14.         //将list中一页的记录放入pageUtil  
    15.         pageUtil.setPageList(list);  
    16.           
    17.         return pageUtil;  
    18.     }  

    sqlMap中的语句:

    1.   
    2.     select * from bookinfo  
    3.   
    4.   
    5.   
    6.     select * from bookinfo limit #pageIndex#,#pageSize#  
    7.   

    在jsp中添加:

    1.   
    2.             
    3.             共${pageUtil.recordCount }条纪录,当前第${pageUtil.currentPage }/${pageUtil.totalPage }页,每页${pageUtil.pageSize }条纪录  
    4.               
    5.                 
    6.                   
    7.              
    8.                   
      
  •                   
  •   
  •                     
  •              
  •              
  •                   book.do?method=page¤tPage=1" target='I1'>  
  •                   book.do?method=page¤tPage=${pageUtil.currentPage-1 }" target='I1'>  
  •              
  •              
  •                     
  •                     
  •              
  •              
  •                   book.do?method=page¤tPage=${pageUtil.currentPage+1 }" target='I1'>  
  •                   book.do?method=page¤tPage=${pageUtil.totalPage }" target='I1'>  
  •              
  •                   转到第
  •   
  •                     
  •                       
  •                       
  •                       
  •                       
  •                           
  •                       
  •                       
  •                       
  •                     
  •                   页  
  •                     
  •                   
  •                 
  •             
  •   
  •   
  •             
  •           
    1.   

    你可能感兴趣的:(java分页 Struts+Ibatis)