pager-taglib分页開始~
查了好多关于分页的技术,终于选定下面方法实现~
1.首先下载jar包:pager-taglib.jar,pager-taglib.jar放在WEB-INF/lib文件夹下;
下载地址:http://download.csdn.net/detail/jeofey/8641287
2.将pager-taglib.tld文件放在WEB-INF文件夹下,pager-taglib.tld文件的内容见文章末尾;
3.仅仅粘贴实现分页的关键部分
jsp中:
-
标签库:
-
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
-
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
-
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>
-
<%@taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg" %>
-
-
实现分页用的几个变量:
-
<%
-
int currentPage = Integer.parseInt((String)request.getAttribute("page"));
-
int pageSize=25;
-
int resultCount = Integer.parseInt((String)request.getAttribute("resultCount"));
-
int pageCount=((resultCount - 1)/pageSize) + 1;
-
currentPage=Math.min(currentPage, pageCount);
-
int offset=(currentPage -1) * pageSize;
-
int totalpage = (resultCount % pageSize == 0) ? resultCount / pageSize
-
: (resultCount / pageSize) + 1;
-
%>
-
-
分页标记開始(为了显示清晰。放在div里面):
-
<div>
-
-
<ul style="float:right">
-
<pg:pager
-
items="<%= resultCount %>"
-
offset="<%= offset %>"
-
index="center"
-
maxPageItems="<%=pageSize%>"
-
maxIndexPages="10"
-
isOffset="false"
-
export="currentPageNumber=pageNumber"
-
scope="request">
-
<pg:index export="totalItems=itemCount">
-
<% if(((Integer)currentPageNumber).intValue() != 1) { %>
-
<pg:first>
-
<li><a href="/PageProject/directcity/alllist/tj/<%= pageNumber %>.html">首页</a></li>
-
</pg:first>
-
<%}%>
-
<pg:prev>
-
<li><a href="/PageProject/directcity/alllist/tj/<%= pageNumber %>.html">上一页</a></li>
-
</pg:prev>
-
<pg:pages>
-
<% if(pageNumber == currentPageNumber) { %>
-
<li class="ifpon"><span><%= pageNumber %></span></li>
-
<% } else { %>
-
<li><a href="/PageProject/directcity/alllist/tj/<%= pageNumber %>.html"><%= pageNumber %></a></li>
-
<% } %>
-
</pg:pages>
-
<pg:next>
-
<li><a href="/PageProject/directcity/alllist/tj/<%= pageNumber %>.html">下一页</a></li>
-
</pg:next>
-
<% if(((Integer)currentPageNumber).intValue() != totalpage) { %>
-
<pg:last>
-
<li><a href="/PageProject/directcity/alllist/tj/<%= pageNumber %>.html">尾页</a></li>
-
</pg:last>
-
<%}%>
-
<pg:page export="firstItem, lastItem"></pg:page>
-
</pg:index>
-
</pg:pager>
-
</ul>
-
-
-
</div>
SQL语句:
-
例1 :select * from (
-
select top pageSize * from (
-
select top offset + pageSize *
-
from ( select * from 表1 union all select * from 表2 union all select * from 表3) ) result
-
where 条件
-
order by 排序字段 desc )a order by 排序字段 asc )b order by 排序字段 desc
-
例2: select top pageSize * from 表 where 主键 not in(select top (currentPage-1)*pageSize news_id from 表)
最后插张调试结果的图片:
pager-taglib.tld文件内容: