Jsp分页模板代码,实现页面内套用分页逻辑

来源:互联网 发布:日本股票行情查看软件 编辑:程序博客网 时间:2024/05/16 15:46

分页模板页面名:paging.jsp

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%
/*接收分页参数*/
int _totalPageNum = Integer.parseInt(request.getParameter("totalPageNum"));
int _currentPageNo = Integer.parseInt(request.getParameter("currentPageNo"));
String _pageName = request.getParameter("pageName");
int _maxShowNums = Integer.parseInt(request.getParameter("maxShowNums"));
/*分页核心逻辑*/
int _pageStartNo = 1;//首页
int _pageEndNo = _totalPageNum;//末页
int _prePageNo = _currentPageNo - 1;//上一页
int _nextPageNo = _currentPageNo + 1;//下一页
int _thisPageStartNo;//实际首页
int _thisPageEndNo;//实际末页
if(_currentPageNo == _pageStartNo){
_prePageNo = _pageStartNo;
}
if(_currentPageNo == _pageEndNo){
_nextPageNo = _pageEndNo;
}
if(_totalPageNum < _maxShowNums){
_thisPageStartNo = _pageStartNo;
_thisPageEndNo = _totalPageNum;
}else{
if(_maxShowNums%2 == 0){
if(_currentPageNo < (_maxShowNums/2 + 1)){
_thisPageStartNo = _pageStartNo;
_thisPageEndNo = _maxShowNums;
}else if(_currentPageNo > (_totalPageNum - (_maxShowNums/2 - 1))){
_thisPageStartNo = _totalPageNum - (_maxShowNums-1);
_thisPageEndNo = _totalPageNum;
}else{
_thisPageStartNo = _currentPageNo - _maxShowNums/2;
_thisPageEndNo = _currentPageNo + (_maxShowNums/2 - 1);
}
}else{
if(_currentPageNo < (_maxShowNums/2 + 1)){
_thisPageStartNo = _pageStartNo;
_thisPageEndNo = _maxShowNums;
}else if(_currentPageNo > (_totalPageNum - _maxShowNums/2)){
_thisPageStartNo = _totalPageNum - (_maxShowNums-1);
_thisPageEndNo = _totalPageNum;
}else{
_thisPageStartNo = _currentPageNo - _maxShowNums/2;
_thisPageEndNo = _currentPageNo + _maxShowNums/2;
}
}
}
%>
<a href="<%=_pageName%>?pageNum=1" target ="_self">首页</a>
<a href="<%=_pageName%>?pageNum=<%=_prePageNo%>" target ="_self">上一页</a>
<%
for(int i = _thisPageStartNo;i <= _thisPageEndNo;i++){
if(i == _currentPageNo){
%>
<span><%=i%></span>
<%
}else{
%>
<a href="<%=_pageName%>?pageNum=<%=i%>" target="_self"><%=i%></a>
<%
}
}
%>
<a href="<%=_pageName%>?pageNum=<%=_nextPageNo%>" target ="_self">下一页</a>
<a href="<%=_pageName%>?pageNum=<%=_totalPageNum%>" target ="_self">末页</a>
<span>&nbsp;<%=_totalPageNum%>&nbsp;</span>
<span>跳转至</span>
<input type="text" id="pageNum" maxlength="5" style="width:30px;text-align:center;"/>
<span style="padding-right:5px;"></span>
<a href="javascript:forwardPageTo($('#pageNum').val());" target="_self">确定</a>

应用例子:demo.jsp

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
<%@ page language="java" contentType="text/html; charset=GBK" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>分页应用例子</title>
<script type="text/javascript" src="<%=path%>/js/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
function forwardPageTo(pageNum){
location.href = "<%=path%>/admin/paging/demo.jsp?pageNum=" + $.trim(pageNum);
}
</script>
</head>
<body>
<%
int totalPageNum = 8; //总页数
int currentPageNo = 1; //当前页数
if(request.getParameter("pageNum") != null){
currentPageNo = Integer.parseInt(request.getParameter("pageNum"));
}
String pageName = path + "/admin/paging/demo.jsp"; //页面路径
int maxShowNums = 14; //总显示页数
if(totalPageNum < maxShowNums){
maxShowNums = totalPageNum;
}
%>
<div>
<jsp:include page="paging.jsp">
<jsp:param value="<%=totalPageNum%>" name="totalPageNum"/>
<jsp:param value="<%=currentPageNo%>" name="currentPageNo"/>
<jsp:param value="<%=pageName%>" name="pageName"/>
<jsp:param value="<%=maxShowNums%>" name="maxShowNums"/>
</jsp:include>
</div>
</body>
</html>



0 0
原创粉丝点击