java分页 Struts+Ibatis

来源:互联网 发布:head first python知乎 编辑:程序博客网 时间:2024/06/05 03:13


首先写一个分页类:

  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. <select id="listAllBook" resultClass="Book">  
  2.     select * from bookinfo  
  3. </select>  
  4.   
  5. <select id="pageBook" resultClass="Book" parameterClass="PageUtil">  
  6.     select * from bookinfo limit #pageIndex#,#pageSize#  
  7. </select>  

在jsp中添加:

  1. <table width="100%" border="0" cellspacing="0" cellpadding="0">  
  2.           <tr>  
  3.             <td width="25%" height="29" nowrap="nowrap"><span class="STYLE1">共${pageUtil.recordCount }条纪录,当前第${pageUtil.currentPage }/${pageUtil.totalPage }页,每页${pageUtil.pageSize }条纪录</span></td>  
  4.             <td width="75%" valign="top" class="STYLE1"><div align="right">  
  5.               <table width="352" height="20" border="0" cellpadding="0" cellspacing="0">  
  6.                 <tr>  
  7.            <c:if test="${pageUtil.currentPage == 1}">  
  8.                   <td width="62" height="22" valign="middle"><div align="right"><img src="tab/images/first.gif" mce_src="tab/images/first.gif" width="37" height="15" /></div></td>  
  9.                   <td width="50" height="22" valign="middle"><div align="right"><img src="tab/images/back.gif" mce_src="tab/images/back.gif" width="43" height="15" /></div></td>  
  10.                     
  11.            </c:if>  
  12.            <c:if test="${pageUtil.currentPage != 1}">  
  13.                   <td width="62" height="22" valign="middle" align="right" ><a href="<%=basePath%>book.do?method=page¤tPage=1" target='I1'><img src="tab/images/first.gif" mce_src="tab/images/first.gif" width="37" height="15"  border="0"/></a></td>  
  14.                   <td width="50" height="22" valign="middle" align="right"><a href="<%=basePath%>book.do?method=page¤tPage=${pageUtil.currentPage-1 }" target='I1'><img src="tab/images/back.gif" mce_src="tab/images/back.gif" width="43" height="15" border="0"></a></td>  
  15.            </c:if>  
  16.            <c:if test="${pageUtil.currentPage == pageUtil.totalPage}">  
  17.                   <td width="54" height="22" valign="middle" align="right"><img src="tab/images/next.gif" mce_src="tab/images/next.gif" width="43" height="15" /></td>  
  18.                   <td width="49" height="22" valign="middle" align="right"><img src="tab/images/last.gif" mce_src="tab/images/last.gif" width="37" height="15" /></td>  
  19.            </c:if>  
  20.            <c:if test="${pageUtil.currentPage != pageUtil.totalPage}">  
  21.                   <td width="54" height="22" valign="middle" align="right"><a href="<%=basePath%>book.do?method=page¤tPage=${pageUtil.currentPage+1 }" target='I1'><img src="tab/images/next.gif" mce_src="tab/images/next.gif" width="43" height="15" border="0"/></a></td>  
  22.                   <td width="49" height="22" valign="middle" align="right"><a href="<%=basePath%>book.do?method=page¤tPage=${pageUtil.totalPage }" target='I1'><img src="tab/images/last.gif" mce_src="tab/images/last.gif" width="37" height="15" border="0"/></a></td>  
  23.            </c:if>  
  24.                   <td width="59" height="22" valign="middle"><div align="right"><span class="STYLE1">转到第</span></div></td>  
  25.                   <td width="25" height="22" valign="middle"><span class="STYLE7">  
  26.                     <!-- <input name="textfield" type="text" class="STYLE1" style="height:15px; width:30px;" size="5" /> -->  
  27.                       
  28.                     <select id="selectPage" name="selectPage">  
  29.                     <c:forEach var="i" begin="1" end="${pageUtil.totalPage }" step="1">  
  30.                         <option value="${i}"><c:out value="${i}" /></option>  
  31.                     </c:forEach>  
  32.                     </select>  
  33.                       
  34.                   </span></td>  
  35.                   <td width="23" height="22" valign="middle"><span class="STYLE1">页</span></td>  
  36.                   <td width="30" height="22" valign="middle"><a href="javascript:goPage()" mce_href="javascript:goPage()"><img src="tab/images/go.gif" mce_src="tab/images/go.gif" width="37" height="15" border="0"/></a></td>  
  37.                 </tr>  
  38.               </table>  
  39.             </div></td>  
  40. <td width="14"><img src="tab/images/tab_22.gif" mce_src="tab/images/tab_22.gif" width="14" height="29" /></td>  
  41.           </tr>  
  42.         </table>  

  1. <mce:script type="text/javascript"><!--  
  2. //页数select随页数自动变化  
  3.       
  4.     function selectPage(currentPage){  
  5.     var i,num,op  
  6.     num=document.getElementById("selectPage").length;  
  7.       
  8.         for(i=0;i<num;i++){  
  9.             op=document.getElementById("selectPage").options[i];  
  10.             if(op.value==currentPage) op.selected=true;  
  11.         }  
  12.     }  
  13.     //根据所选页数进行跳转  
  14.     function goPage(){  
  15.         var currentpage;  
  16.         currentpage=document.getElementById("selectPage").value;  
  17.         window.location="<%=basePath%>book.do?method=page&currentPage="+currentpage;  
  18.     }  
  19. // --></mce:script>  

首先写一个分页类:

  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. <select id="listAllBook" resultClass="Book">  
  2.     select * from bookinfo  
  3. </select>  
  4.   
  5. <select id="pageBook" resultClass="Book" parameterClass="PageUtil">  
  6.     select * from bookinfo limit #pageIndex#,#pageSize#  
  7. </select>  

在jsp中添加:

  1. <table width="100%" border="0" cellspacing="0" cellpadding="0">  
  2.           <tr>  
  3.             <td width="25%" height="29" nowrap="nowrap"><span class="STYLE1">共${pageUtil.recordCount }条纪录,当前第${pageUtil.currentPage }/${pageUtil.totalPage }页,每页${pageUtil.pageSize }条纪录</span></td>  
  4.             <td width="75%" valign="top" class="STYLE1"><div align="right">  
  5.               <table width="352" height="20" border="0" cellpadding="0" cellspacing="0">  
  6.                 <tr>  
  7.            <c:if test="${pageUtil.currentPage == 1}">  
  8.                   <td width="62" height="22" valign="middle"><div align="right"><img src="tab/images/first.gif" mce_src="tab/images/first.gif" width="37" height="15" /></div></td>  
  9.                   <td width="50" height="22" valign="middle"><div align="right"><img src="tab/images/back.gif" mce_src="tab/images/back.gif" width="43" height="15" /></div></td>  
  10.                     
  11.            </c:if>  
  12.            <c:if test="${pageUtil.currentPage != 1}">  
  13.                   <td width="62" height="22" valign="middle" align="right" ><a href="<%=basePath%>book.do?method=page¤tPage=1" target='I1'><img src="tab/images/first.gif" mce_src="tab/images/first.gif" width="37" height="15"  border="0"/></a></td>  
  14.                   <td width="50" height="22" valign="middle" align="right"><a href="<%=basePath%>book.do?method=page¤tPage=${pageUtil.currentPage-1 }" target='I1'><img src="tab/images/back.gif" mce_src="tab/images/back.gif" width="43" height="15" border="0"></a></td>  
  15.            </c:if>  
  16.            <c:if test="${pageUtil.currentPage == pageUtil.totalPage}">  
  17.                   <td width="54" height="22" valign="middle" align="right"><img src="tab/images/next.gif" mce_src="tab/images/next.gif" width="43" height="15" /></td>  
  18.                   <td width="49" height="22" valign="middle" align="right"><img src="tab/images/last.gif" mce_src="tab/images/last.gif" width="37" height="15" /></td>  
  19.            </c:if>  
  20.            <c:if test="${pageUtil.currentPage != pageUtil.totalPage}">  
  21.                   <td width="54" height="22" valign="middle" align="right"><a href="<%=basePath%>book.do?method=page¤tPage=${pageUtil.currentPage+1 }" target='I1'><img src="tab/images/next.gif" mce_src="tab/images/next.gif" width="43" height="15" border="0"/></a></td>  
  22.                   <td width="49" height="22" valign="middle" align="right"><a href="<%=basePath%>book.do?method=page¤tPage=${pageUtil.totalPage }" target='I1'><img src="tab/images/last.gif" mce_src="tab/images/last.gif" width="37" height="15" border="0"/></a></td>  
  23.            </c:if>  
  24.                   <td width="59" height="22" valign="middle"><div align="right"><span class="STYLE1">转到第</span></div></td>  
  25.                   <td width="25" height="22" valign="middle"><span class="STYLE7">  
  26.                     <!-- <input name="textfield" type="text" class="STYLE1" style="height:15px; width:30px;" size="5" /> -->  
  27.                       
  28.                     <select id="selectPage" name="selectPage">  
  29.                     <c:forEach var="i" begin="1" end="${pageUtil.totalPage }" step="1">  
  30.                         <option value="${i}"><c:out value="${i}" /></option>  
  31.                     </c:forEach>  
  32.                     </select>  
  33.                       
  34.                   </span></td>  
  35.                   <td width="23" height="22" valign="middle"><span class="STYLE1">页</span></td>  
  36.                   <td width="30" height="22" valign="middle"><a href="javascript:goPage()" mce_href="javascript:goPage()"><img src="tab/images/go.gif" mce_src="tab/images/go.gif" width="37" height="15" border="0"/></a></td>  
  37.                 </tr>  
  38.               </table>  
  39.             </div></td>  
  40. <td width="14"><img src="tab/images/tab_22.gif" mce_src="tab/images/tab_22.gif" width="14" height="29" /></td>  
  41.           </tr>  
  42.         </table>  

  1. <mce:script type="text/javascript"><!--  
  2. //页数select随页数自动变化  
  3.       
  4.     function selectPage(currentPage){  
  5.     var i,num,op  
  6.     num=document.getElementById("selectPage").length;  
  7.       
  8.         for(i=0;i<num;i++){  
  9.             op=document.getElementById("selectPage").options[i];  
  10.             if(op.value==currentPage) op.selected=true;  
  11.         }  
  12.     }  
  13.     //根据所选页数进行跳转  
  14.     function goPage(){  
  15.         var currentpage;  
  16.         currentpage=document.getElementById("selectPage").value;  
  17.         window.location="<%=basePath%>book.do?method=page&currentPage="+currentpage;  
  18.     }  
  19. // --></mce:script>  
0 0
原创粉丝点击