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中:
[html] view
plaincopy
- 标签库:
- <%@ 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语句:
[sql] view
plaincopy
- 例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文件内容:
[plain] view
plaincopy
- <?
xml version="1.0" encoding="ISO-8859-1" ?>
- <!DOCTYPE taglib
- PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"
- "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
- <taglib>
- <tlibversion>1.0</tlibversion>
- <jspversion>1.1</jspversion>
- <shortname>pg</shortname>
- <uri>http://jsptags.com/tags/navigation/pager</uri>
- <info>
- The Pager Tag Library is the easy and flexible way to implement paging of
- large data sets in JavaServer Pages (JSP). It can emulate all currently
- known paging styles with minimal effort. It also includes re-usable index
- styles that emulate the search result navigators of popular web sites
- such as Google[sm], AltaVista?? and Yahoo!. The Pager Tag Library does most
- of the work for you by dynamically organizing your data set into pages and
- generating a browsable index with virtually any look desired.
- </info>
- <tag>
- <name>pager</name>
- <tagclass>com.jsptags.navigation.pager.PagerTag</tagclass>
- <teiclass>com.jsptags.navigation.pager.PagerTagExtraInfo</teiclass>
- <bodycontent>JSP</bodycontent>
- <attribute>
- <name>id</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>url</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>items</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>offset</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>maxItems</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>maxPageItems</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>maxIndexPages</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>isOffset</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>index</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>export</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>scope</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- <tag>
- <name>param</name>
- <tagclass>com.jsptags.navigation.pager.ParamTag</tagclass>
- <bodycontent>empty</bodycontent>
- <attribute>
- <name>id</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>name</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>value</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- <tag>
- <name>item</name>
- <tagclass>com.jsptags.navigation.pager.ItemTag</tagclass>
- <bodycontent>JSP</bodycontent>
- <attribute>
- <name>id</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- <tag>
- <name>index</name>
- <tagclass>com.jsptags.navigation.pager.IndexTag</tagclass>
- <teiclass>com.jsptags.navigation.pager.IndexTagExtraInfo</teiclass>
- <bodycontent>JSP</bodycontent>
- <attribute>
- <name>id</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>export</name>
- <required>false</required>
- <rtexprvalue>false</rtexprvalue>
- </attribute>
- </tag>
- <tag>
- <name>first</name>
- <tagclass>com.jsptags.navigation.pager.FirstTag</tagclass>
- <teiclass>com.jsptags.navigation.pager.JumpTagExtraInfo</teiclass>
- <bodycontent>JSP</bodycontent>
- <attribute>
- <name>id</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>export</name>
- <required>false</required>
- <rtexprvalue>false</rtexprvalue>
- </attribute>
- <attribute>
- <name>unless</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- <tag>
- <name>prev</name>
- <tagclass>com.jsptags.navigation.pager.PrevTag</tagclass>
- <teiclass>com.jsptags.navigation.pager.PageTagExtraInfo</teiclass>
- <bodycontent>JSP</bodycontent>
- <attribute>
- <name>id</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>export</name>
- <required>false</required>
- <rtexprvalue>false</rtexprvalue>
- </attribute>
- <attribute>
- <name>ifnull</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- <tag>
- <name>page</name>
- <tagclass>com.jsptags.navigation.pager.PageTag</tagclass>
- <teiclass>com.jsptags.navigation.pager.JumpTagExtraInfo</teiclass>
- <bodycontent>JSP</bodycontent>
- <attribute>
- <name>id</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>export</name>
- <required>false</required>
- <rtexprvalue>false</rtexprvalue>
- </attribute>
- </tag>
- <tag>
- <name>pages</name>
- <tagclass>com.jsptags.navigation.pager.PagesTag</tagclass>
- <teiclass>com.jsptags.navigation.pager.PageTagExtraInfo</teiclass>
- <bodycontent>JSP</bodycontent>
- <attribute>
- <name>id</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>export</name>
- <required>false</required>
- <rtexprvalue>false</rtexprvalue>
- </attribute>
- </tag>
- <tag>
- <name>next</name>
- <tagclass>com.jsptags.navigation.pager.NextTag</tagclass>
- <teiclass>com.jsptags.navigation.pager.PageTagExtraInfo</teiclass>
- <bodycontent>JSP</bodycontent>
- <attribute>
- <name>id</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>export</name>
- <required>false</required>
- <rtexprvalue>false</rtexprvalue>
- </attribute>
- <attribute>
- <name>ifnull</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- <tag>
- <name>last</name>
- <tagclass>com.jsptags.navigation.pager.LastTag</tagclass>
- <teiclass>com.jsptags.navigation.pager.JumpTagExtraInfo</teiclass>
- <bodycontent>JSP</bodycontent>
- <attribute>
- <name>id</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>export</name>
- <required>false</required>
- <rtexprvalue>false</rtexprvalue>
- </attribute>
- <attribute>
- <name>unless</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- <tag>
- <name>skip</name>
- <tagclass>com.jsptags.navigation.pager.SkipTag</tagclass>
- <teiclass>com.jsptags.navigation.pager.PageTagExtraInfo</teiclass>
- <bodycontent>JSP</bodycontent>
- <attribute>
- <name>id</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>export</name>
- <required>false</required>
- <rtexprvalue>false</rtexprvalue>
- </attribute>
- <attribute>
- <name>ifnull</name>
- <required>false</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- <attribute>
- <name>pages</name>
- <required>true</required>
- <rtexprvalue>true</rtexprvalue>
- </attribute>
- </tag>
- </taglib>
时间: 2024-10-05 15:24:15