首先写一个分页类:
- package com.zc.book.util;
- import java.util.List;
- public class PageUtil {
- private int pageSize=20;//每页显示的条数 默认20
- private int recordCount;//记录总数
- private int currentPage;//当前页
- private int pageIndex;//每页的第一条记录编号
- private List pageList;//每页的记录集
- private int totalPage;//总页数
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public int getRecordCount() {
- return recordCount;
- }
- public void setRecordCount(int recordCount) {
- this.recordCount = recordCount;
- }
- public int getCurrentPage() {
- return currentPage;
- }
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- }
- public int getPageIndex() {
- return pageIndex;
- }
- public void setPageIndex(int pageIndex) {
- this.pageIndex = pageIndex;
- }
- public List getPageList() {
- return pageList;
- }
- public void setPageList(List pageList) {
- this.pageList = pageList;
- }
- public int getTotalPage() {
- return totalPage;
- }
- public void setTotalPage(int totalPage) {
- this.totalPage = totalPage;
- }
- //初始化 currentPage 当前页,pageSize 每页大小,recordCount 总记录数,pageIndex 每页第一条记录序号
- public void init(int currentPage,int pageSize,int recordCount){
- this.currentPage=currentPage>0?currentPage:1;//设置当前页
- this.pageSize=pageSize;//设置每页大小
- this.recordCount=recordCount;//设置总记录数
- this.pageIndex=(currentPage-1)*pageSize;//设置每页第一条记录
- this.totalPage=recordCount/pageSize+1;
- if(currentPage>totalPage){
- currentPage=totalPage;
- }
- }
- }
在Action中
- int currentPage = 1;
- try {
- currentPage = Integer.parseInt(request.getParameter("currentPage"));
- } catch(Exception ex) {
- }
- PageUtil pageUtil=new PageUtil();
- pageUtil=bookService.pageBookInfo(currentPage);
- request.setAttribute("pageUtil", pageUtil);
- return mapping.findForward("bookInfo");
service中的pageBookInfo方法:
- public PageUtil pageBookInfo(int currentPage) {
- // TODO Auto-generated method stub
- List list=bookDao.bookInfo();
- PageUtil pageUtil=new PageUtil();
- //设置当前页,每页记录数,总记录数
- pageUtil.init(currentPage, 25, list.size());
- //通过pageUtil的"pageIndex--每页的第一条记录编号"和"pageSize--每页显示的条数"
- //这两个参数去数据库中查询出其中一页的记录,放入List里
- list=bookDao.pageBook(pageUtil);
- //将list中一页的记录放入pageUtil
- pageUtil.setPageList(list);
- return pageUtil;
- }
sqlMap中的语句:
- <select id="listAllBook" resultClass="Book">
- select * from bookinfo
- </select>
- <select id="pageBook" resultClass="Book" parameterClass="PageUtil">
- select * from bookinfo limit #pageIndex#,#pageSize#
- </select>
在jsp中添加:
- <table width="100%" border="0" cellspacing="0" cellpadding="0">
- <tr>
- <td width="25%" height="29" nowrap="nowrap"><span class="STYLE1">共${pageUtil.recordCount }条纪录,当前第${pageUtil.currentPage }/${pageUtil.totalPage }页,每页${pageUtil.pageSize }条纪录</span></td>
- <td width="75%" valign="top" class="STYLE1"><div align="right">
- <table width="352" height="20" border="0" cellpadding="0" cellspacing="0">
- <tr>
- <c:if test="${pageUtil.currentPage == 1}">
- <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>
- <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>
- </c:if>
- <c:if test="${pageUtil.currentPage != 1}">
- <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>
- <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>
- </c:if>
- <c:if test="${pageUtil.currentPage == pageUtil.totalPage}">
- <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>
- <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>
- </c:if>
- <c:if test="${pageUtil.currentPage != pageUtil.totalPage}">
- <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>
- <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>
- </c:if>
- <td width="59" height="22" valign="middle"><div align="right"><span class="STYLE1">转到第</span></div></td>
- <td width="25" height="22" valign="middle"><span class="STYLE7">
- <!-- <input name="textfield" type="text" class="STYLE1" style="height:15px; width:30px;" size="5" /> -->
- <select id="selectPage" name="selectPage">
- <c:forEach var="i" begin="1" end="${pageUtil.totalPage }" step="1">
- <option value="${i}"><c:out value="${i}" /></option>
- </c:forEach>
- </select>
- </span></td>
- <td width="23" height="22" valign="middle"><span class="STYLE1">页</span></td>
- <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>
- </tr>
- </table>
- </div></td>
- <td width="14"><img src="tab/images/tab_22.gif" mce_src="tab/images/tab_22.gif" width="14" height="29" /></td>
- </tr>
- </table>
- <mce:script type="text/javascript"><!--
- //页数select随页数自动变化
- function selectPage(currentPage){
- var i,num,op
- num=document.getElementById("selectPage").length;
- for(i=0;i<num;i++){
- op=document.getElementById("selectPage").options[i];
- if(op.value==currentPage) op.selected=true;
- }
- }
- //根据所选页数进行跳转
- function goPage(){
- var currentpage;
- currentpage=document.getElementById("selectPage").value;
- window.location="<%=basePath%>book.do?method=page¤tPage="+currentpage;
- }
- // --></mce:script>
首先写一个分页类:
- package com.zc.book.util;
- import java.util.List;
- public class PageUtil {
- private int pageSize=20;//每页显示的条数 默认20
- private int recordCount;//记录总数
- private int currentPage;//当前页
- private int pageIndex;//每页的第一条记录编号
- private List pageList;//每页的记录集
- private int totalPage;//总页数
- public int getPageSize() {
- return pageSize;
- }
- public void setPageSize(int pageSize) {
- this.pageSize = pageSize;
- }
- public int getRecordCount() {
- return recordCount;
- }
- public void setRecordCount(int recordCount) {
- this.recordCount = recordCount;
- }
- public int getCurrentPage() {
- return currentPage;
- }
- public void setCurrentPage(int currentPage) {
- this.currentPage = currentPage;
- }
- public int getPageIndex() {
- return pageIndex;
- }
- public void setPageIndex(int pageIndex) {
- this.pageIndex = pageIndex;
- }
- public List getPageList() {
- return pageList;
- }
- public void setPageList(List pageList) {
- this.pageList = pageList;
- }
- public int getTotalPage() {
- return totalPage;
- }
- public void setTotalPage(int totalPage) {
- this.totalPage = totalPage;
- }
- //初始化 currentPage 当前页,pageSize 每页大小,recordCount 总记录数,pageIndex 每页第一条记录序号
- public void init(int currentPage,int pageSize,int recordCount){
- this.currentPage=currentPage>0?currentPage:1;//设置当前页
- this.pageSize=pageSize;//设置每页大小
- this.recordCount=recordCount;//设置总记录数
- this.pageIndex=(currentPage-1)*pageSize;//设置每页第一条记录
- this.totalPage=recordCount/pageSize+1;
- if(currentPage>totalPage){
- currentPage=totalPage;
- }
- }
- }
在Action中
- int currentPage = 1;
- try {
- currentPage = Integer.parseInt(request.getParameter("currentPage"));
- } catch(Exception ex) {
- }
- PageUtil pageUtil=new PageUtil();
- pageUtil=bookService.pageBookInfo(currentPage);
- request.setAttribute("pageUtil", pageUtil);
- return mapping.findForward("bookInfo");
service中的pageBookInfo方法:
- public PageUtil pageBookInfo(int currentPage) {
- // TODO Auto-generated method stub
- List list=bookDao.bookInfo();
- PageUtil pageUtil=new PageUtil();
- //设置当前页,每页记录数,总记录数
- pageUtil.init(currentPage, 25, list.size());
- //通过pageUtil的"pageIndex--每页的第一条记录编号"和"pageSize--每页显示的条数"
- //这两个参数去数据库中查询出其中一页的记录,放入List里
- list=bookDao.pageBook(pageUtil);
- //将list中一页的记录放入pageUtil
- pageUtil.setPageList(list);
- return pageUtil;
- }
sqlMap中的语句:
- <select id="listAllBook" resultClass="Book">
- select * from bookinfo
- </select>
- <select id="pageBook" resultClass="Book" parameterClass="PageUtil">
- select * from bookinfo limit #pageIndex#,#pageSize#
- </select>
在jsp中添加:
- <table width="100%" border="0" cellspacing="0" cellpadding="0">
- <tr>
- <td width="25%" height="29" nowrap="nowrap"><span class="STYLE1">共${pageUtil.recordCount }条纪录,当前第${pageUtil.currentPage }/${pageUtil.totalPage }页,每页${pageUtil.pageSize }条纪录</span></td>
- <td width="75%" valign="top" class="STYLE1"><div align="right">
- <table width="352" height="20" border="0" cellpadding="0" cellspacing="0">
- <tr>
- <c:if test="${pageUtil.currentPage == 1}">
- <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>
- <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>
- </c:if>
- <c:if test="${pageUtil.currentPage != 1}">
- <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>
- <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>
- </c:if>
- <c:if test="${pageUtil.currentPage == pageUtil.totalPage}">
- <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>
- <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>
- </c:if>
- <c:if test="${pageUtil.currentPage != pageUtil.totalPage}">
- <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>
- <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>
- </c:if>
- <td width="59" height="22" valign="middle"><div align="right"><span class="STYLE1">转到第</span></div></td>
- <td width="25" height="22" valign="middle"><span class="STYLE7">
- <!-- <input name="textfield" type="text" class="STYLE1" style="height:15px; width:30px;" size="5" /> -->
- <select id="selectPage" name="selectPage">
- <c:forEach var="i" begin="1" end="${pageUtil.totalPage }" step="1">
- <option value="${i}"><c:out value="${i}" /></option>
- </c:forEach>
- </select>
- </span></td>
- <td width="23" height="22" valign="middle"><span class="STYLE1">页</span></td>
- <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>
- </tr>
- </table>
- </div></td>
- <td width="14"><img src="tab/images/tab_22.gif" mce_src="tab/images/tab_22.gif" width="14" height="29" /></td>
- </tr>
- </table>
- <mce:script type="text/javascript"><!--
- //页数select随页数自动变化
- function selectPage(currentPage){
- var i,num,op
- num=document.getElementById("selectPage").length;
- for(i=0;i<num;i++){
- op=document.getElementById("selectPage").options[i];
- if(op.value==currentPage) op.selected=true;
- }
- }
- //根据所选页数进行跳转
- function goPage(){
- var currentpage;
- currentpage=document.getElementById("selectPage").value;
- window.location="<%=basePath%>book.do?method=page¤tPage="+currentpage;
- }
- // --></mce:script>