<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!-- 分页导航开始 --> 第${page.pageNum}页/共${page.totalPage}页 <a href="${pageContext.request.contextPath}${page.servletUrl}">首页</a> <a href="${pageContext.request.contextPath}${page.servletUrl}?pagenum=${(page.pageNum-1)>0?(page.pageNum-1):1}">上一页</a> <c:forEach begin="${page.startPage}" end="${page.endPage}" var="num"> <a href="${pageContext.request.contextPath}${page.servletUrl}?pagenum=${num}">[${num}]</a> </c:forEach> <a href="${pageContext.request.contextPath}${page.servletUrl}?pagenum=${(page.pageNum+1)>page.totalPage?page.totalPage:(page.pageNum+1)}">下一页</a> <a href="${pageContext.request.contextPath}${page.servletUrl}?pagenum=${page.totalPage}">尾页</a>
<select id="s1" name="s1"> <c:forEach begin="1" end="${page.totalPage}" var="num"> <option value="${num}" ${page.pageNum==num?'selected="selected"':''}>${num}</option> </c:forEach> </select> <input type="button" value="跳转" onclick="jump()"/> <script type="text/javascript"> function jump(){ var num = document.getElementById("s1").value; window.location.href="${pageContext.request.contextPath}${page.servletUrl}?pagenum="+num; } </script> <!-- 分页导航结束 --> javabean层: private List records;//分页显示的记录 Dao可以查出来 private int pageNum;//当前页码 页面上传递过来,默认第1页 private int totalPage;//总页数 计算出来:总记录数和pageSize private int pageSize = 10;//每页显示的记录条数 private int totalRecords;//总记录条数 Dao可以查出来 private int startIndex;//每页开始记录的索引号 可以算出来: private int startPage;//页码显示:起始页码 private int endPage;//页码显示:结束页码 public Page(int pageNum,int totalRecords){ this.pageNum = pageNum; this.totalRecords = totalRecords; //计算总页数 totalPage = (totalRecords%pageSize==0)?(totalRecords/pageSize):(totalRecords/pageSize+1); //计算每页开始记录的索引 startIndex = (pageNum-1)*pageSize; //计算起始页码和结束页码 if(totalPage<=9){ startPage = 1; endPage = totalPage; }else{ startPage = pageNum-4; endPage = pageNum+4; if(startPage<1){ startPage = 1; endPage = 9; } if(endPage>totalPage){ endPage = totalPage; startPage = endPage-8; } } }