码迷,mamicode.com
首页 > Web开发 > 详细

jsp分页代码

时间:2015-09-05 23:53:02      阅读:280      评论:0      收藏:0      [点我收藏+]

标签:jsp servlet

<%@ 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;
                        }
                }
        }


本文出自 “任重道远” 博客,请务必保留此出处http://534256702.blog.51cto.com/10669156/1691632

jsp分页代码

标签:jsp servlet

原文地址:http://534256702.blog.51cto.com/10669156/1691632

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!