首页 > 代码库 > Spring3+ibatis (SQL Server)+pager-taglib.tld查询分页的实现

Spring3+ibatis (SQL Server)+pager-taglib.tld查询分页的实现

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
  1. 标签库:  
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>  
  3. <%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>  
  4. <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>   
  5. <%@taglib uri="http://jsptags.com/tags/navigation/pager" prefix="pg" %>  
  6.   
  7. 实现分页用的几个变量:  
  8. <%  
  9. int currentPage = Integer.parseInt((String)request.getAttribute("page"));  
  10. int pageSize=25;  
  11. int resultCount = Integer.parseInt((String)request.getAttribute("resultCount"));  
  12. int pageCount=((resultCount - 1)/pageSize) + 1;  
  13. currentPage=Math.min(currentPage, pageCount);  
  14. int offset=(currentPage -1) * pageSize;  
  15. int totalpage = (resultCount % pageSize  == 0) ? resultCount / pageSize  
  16. : (resultCount / pageSize) + 1;  
  17. %>  
  18.   
  19. 分页标记開始(为了显示清晰。放在div里面):  
  20. <div>  
  21. <!-- 分页标记開始 -->  
  22. <ul style="float:right">  
  23. <pg:pager  
  24.           items="<%= resultCount %>"  
  25.           offset="<%= offset %>"  
  26.           index="center"  
  27.           maxPageItems="<%=pageSize%>"  
  28.           maxIndexPages="10"  
  29.           isOffset="false"  
  30.           export="currentPageNumber=pageNumber"  
  31.           scope="request">  
  32.           <pg:index export="totalItems=itemCount">  
  33.  <% if(((Integer)currentPageNumber).intValue() != 1) { %>  
  34.  <pg:first>  
  35. <li><a href="/PageProject/directcity/alllist/tj/<%= pageNumber %>.html">首页</a></li>   
  36.  </pg:first>  
  37.  <%}%>  
  38.  <pg:prev>  
  39.             <li><a href="/PageProject/directcity/alllist/tj/<%= pageNumber %>.html">上一页</a></li>  
  40.           </pg:prev>  
  41.  <pg:pages>  
  42.           <% if(pageNumber == currentPageNumber) { %>  
  43.             <li class="ifpon"><span><%= pageNumber %></span></li>  
  44.           <% } else { %>  
  45.             <li><a href="/PageProject/directcity/alllist/tj/<%= pageNumber %>.html"><%= pageNumber %></a></li>  
  46.           <% } %>  
  47.           </pg:pages>  
  48.  <pg:next>  
  49.             <li><a href="/PageProject/directcity/alllist/tj/<%= pageNumber %>.html">下一页</a></li>  
  50.           </pg:next>  
  51.  <% if(((Integer)currentPageNumber).intValue() != totalpage) { %>  
  52.  <pg:last>  
  53. <li><a href="/PageProject/directcity/alllist/tj/<%= pageNumber %>.html">尾页</a></li>   
  54.  </pg:last>  
  55.  <%}%>  
  56.           <pg:page export="firstItem, lastItem"></pg:page>  
  57. </pg:index>  
  58.       </pg:pager>  
  59.       </ul>  
  60. <!-- 分页标记结束 -->  
  61.   
  62. </div>  

SQL语句:

[sql] view plaincopy
  1. 例1 :select * from (  
  2.           select top pageSize * from (  
  3.           select top offset + pageSize  *  
  4.           from ( select * from 表1 union all  select * from 表2 union all  select * from 表3)   ) result    
  5.           where 条件          
  6.           order by 排序字段 desc )a  order by 排序字段 asc )b order by 排序字段 desc  
  7. 例2:  select   top  pageSize   *   from   表   where   主键   not   in(select   top   (currentPage-1)*pageSize      news_id   from   表)   

最后插张调试结果的图片:

技术分享

pager-taglib.tld文件内容:

[plain] view plaincopy
  1. <?

    xml version="1.0" encoding="ISO-8859-1" ?

    >  

  2. <!DOCTYPE taglib  
  3.   PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"  
  4.   "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">  
  5.   
  6.   
  7. <taglib>  
  8.   <tlibversion>1.0</tlibversion>  
  9.   <jspversion>1.1</jspversion>  
  10.   <shortname>pg</shortname>  
  11.   <uri>http://jsptags.com/tags/navigation/pager</uri>  
  12.   <info>  
  13.     The Pager Tag Library is the easy and flexible way to implement paging of  
  14.     large data sets in JavaServer Pages (JSP). It can emulate all currently  
  15.     known paging styles with minimal effort. It also includes re-usable index  
  16.     styles that emulate the search result navigators of popular web sites  
  17.     such as Google[sm], AltaVista?? and Yahoo!. The Pager Tag Library does most  
  18.     of the work for you by dynamically organizing your data set into pages and  
  19.     generating a browsable index with virtually any look desired.  
  20.   </info>  
  21.   
  22.   
  23.   <tag>  
  24.     <name>pager</name>  
  25.     <tagclass>com.jsptags.navigation.pager.PagerTag</tagclass>  
  26.     <teiclass>com.jsptags.navigation.pager.PagerTagExtraInfo</teiclass>  
  27.     <bodycontent>JSP</bodycontent>  
  28.     <attribute>  
  29.       <name>id</name>  
  30.       <required>false</required>  
  31.       <rtexprvalue>true</rtexprvalue>  
  32.     </attribute>  
  33.     <attribute>  
  34.       <name>url</name>  
  35.       <required>false</required>  
  36.       <rtexprvalue>true</rtexprvalue>  
  37.     </attribute>  
  38.     <attribute>  
  39.       <name>items</name>  
  40.       <required>false</required>  
  41.       <rtexprvalue>true</rtexprvalue>  
  42.     </attribute>  
  43.     <attribute>  
  44.       <name>offset</name>  
  45.       <required>false</required>  
  46.       <rtexprvalue>true</rtexprvalue>  
  47.     </attribute>  
  48.     <attribute>  
  49.       <name>maxItems</name>  
  50.       <required>false</required>  
  51.       <rtexprvalue>true</rtexprvalue>  
  52.     </attribute>  
  53.     <attribute>  
  54.       <name>maxPageItems</name>  
  55.       <required>false</required>  
  56.       <rtexprvalue>true</rtexprvalue>  
  57.     </attribute>  
  58.     <attribute>  
  59.       <name>maxIndexPages</name>  
  60.       <required>false</required>  
  61.       <rtexprvalue>true</rtexprvalue>  
  62.     </attribute>  
  63.     <attribute>  
  64.       <name>isOffset</name>  
  65.       <required>false</required>  
  66.       <rtexprvalue>true</rtexprvalue>  
  67.     </attribute>  
  68.     <attribute>  
  69.       <name>index</name>  
  70.       <required>false</required>  
  71.       <rtexprvalue>true</rtexprvalue>  
  72.     </attribute>  
  73.     <attribute>  
  74.       <name>export</name>  
  75.       <required>false</required>  
  76.       <rtexprvalue>true</rtexprvalue>  
  77.     </attribute>  
  78.     <attribute>  
  79.       <name>scope</name>  
  80.       <required>false</required>  
  81.       <rtexprvalue>true</rtexprvalue>  
  82.     </attribute>  
  83.   </tag>  
  84.   
  85.   
  86.   <tag>  
  87.     <name>param</name>  
  88.     <tagclass>com.jsptags.navigation.pager.ParamTag</tagclass>  
  89.     <bodycontent>empty</bodycontent>  
  90.     <attribute>  
  91.       <name>id</name>  
  92.       <required>false</required>  
  93.       <rtexprvalue>true</rtexprvalue>  
  94.     </attribute>  
  95.     <attribute>  
  96.       <name>name</name>  
  97.       <required>true</required>  
  98.       <rtexprvalue>true</rtexprvalue>  
  99.     </attribute>  
  100.     <attribute>  
  101.       <name>value</name>  
  102.       <required>false</required>  
  103.       <rtexprvalue>true</rtexprvalue>  
  104.     </attribute>  
  105.   </tag>  
  106.   
  107.   
  108.   <tag>  
  109.     <name>item</name>  
  110.     <tagclass>com.jsptags.navigation.pager.ItemTag</tagclass>  
  111.     <bodycontent>JSP</bodycontent>  
  112.     <attribute>  
  113.       <name>id</name>  
  114.       <required>false</required>  
  115.       <rtexprvalue>true</rtexprvalue>  
  116.     </attribute>  
  117.   </tag>  
  118.   
  119.   
  120.   <tag>  
  121.     <name>index</name>  
  122.     <tagclass>com.jsptags.navigation.pager.IndexTag</tagclass>  
  123.     <teiclass>com.jsptags.navigation.pager.IndexTagExtraInfo</teiclass>  
  124.     <bodycontent>JSP</bodycontent>  
  125.     <attribute>  
  126.       <name>id</name>  
  127.       <required>false</required>  
  128.       <rtexprvalue>true</rtexprvalue>  
  129.     </attribute>  
  130.     <attribute>  
  131.       <name>export</name>  
  132.       <required>false</required>  
  133.       <rtexprvalue>false</rtexprvalue>  
  134.     </attribute>  
  135.   </tag>  
  136.   
  137.   
  138.   <tag>  
  139.     <name>first</name>  
  140.     <tagclass>com.jsptags.navigation.pager.FirstTag</tagclass>  
  141.     <teiclass>com.jsptags.navigation.pager.JumpTagExtraInfo</teiclass>  
  142.     <bodycontent>JSP</bodycontent>  
  143.     <attribute>  
  144.       <name>id</name>  
  145.       <required>false</required>  
  146.       <rtexprvalue>true</rtexprvalue>  
  147.     </attribute>  
  148.     <attribute>  
  149.       <name>export</name>  
  150.       <required>false</required>  
  151.       <rtexprvalue>false</rtexprvalue>  
  152.     </attribute>  
  153.     <attribute>  
  154.       <name>unless</name>  
  155.       <required>false</required>  
  156.       <rtexprvalue>true</rtexprvalue>  
  157.     </attribute>  
  158.   </tag>  
  159.   
  160.   
  161.   <tag>  
  162.     <name>prev</name>  
  163.     <tagclass>com.jsptags.navigation.pager.PrevTag</tagclass>  
  164.     <teiclass>com.jsptags.navigation.pager.PageTagExtraInfo</teiclass>  
  165.     <bodycontent>JSP</bodycontent>  
  166.     <attribute>  
  167.       <name>id</name>  
  168.       <required>false</required>  
  169.       <rtexprvalue>true</rtexprvalue>  
  170.     </attribute>  
  171.     <attribute>  
  172.       <name>export</name>  
  173.       <required>false</required>  
  174.       <rtexprvalue>false</rtexprvalue>  
  175.     </attribute>  
  176.     <attribute>  
  177.       <name>ifnull</name>  
  178.       <required>false</required>  
  179.       <rtexprvalue>true</rtexprvalue>  
  180.     </attribute>  
  181.   </tag>  
  182.   
  183.   
  184.   <tag>  
  185.     <name>page</name>  
  186.     <tagclass>com.jsptags.navigation.pager.PageTag</tagclass>  
  187.     <teiclass>com.jsptags.navigation.pager.JumpTagExtraInfo</teiclass>  
  188.     <bodycontent>JSP</bodycontent>  
  189.     <attribute>  
  190.       <name>id</name>  
  191.       <required>false</required>  
  192.       <rtexprvalue>true</rtexprvalue>  
  193.     </attribute>  
  194.     <attribute>  
  195.       <name>export</name>  
  196.       <required>false</required>  
  197.       <rtexprvalue>false</rtexprvalue>  
  198.     </attribute>  
  199.   </tag>  
  200.   
  201.   
  202.   <tag>  
  203.     <name>pages</name>  
  204.     <tagclass>com.jsptags.navigation.pager.PagesTag</tagclass>  
  205.     <teiclass>com.jsptags.navigation.pager.PageTagExtraInfo</teiclass>  
  206.     <bodycontent>JSP</bodycontent>  
  207.     <attribute>  
  208.       <name>id</name>  
  209.       <required>false</required>  
  210.       <rtexprvalue>true</rtexprvalue>  
  211.     </attribute>  
  212.     <attribute>  
  213.       <name>export</name>  
  214.       <required>false</required>  
  215.       <rtexprvalue>false</rtexprvalue>  
  216.     </attribute>  
  217.   </tag>  
  218.   
  219.   
  220.   <tag>  
  221.     <name>next</name>  
  222.     <tagclass>com.jsptags.navigation.pager.NextTag</tagclass>  
  223.     <teiclass>com.jsptags.navigation.pager.PageTagExtraInfo</teiclass>  
  224.     <bodycontent>JSP</bodycontent>  
  225.     <attribute>  
  226.       <name>id</name>  
  227.       <required>false</required>  
  228.       <rtexprvalue>true</rtexprvalue>  
  229.     </attribute>  
  230.     <attribute>  
  231.       <name>export</name>  
  232.       <required>false</required>  
  233.       <rtexprvalue>false</rtexprvalue>  
  234.     </attribute>  
  235.     <attribute>  
  236.       <name>ifnull</name>  
  237.       <required>false</required>  
  238.       <rtexprvalue>true</rtexprvalue>  
  239.     </attribute>  
  240.   </tag>  
  241.   
  242.   
  243.   <tag>  
  244.     <name>last</name>  
  245.     <tagclass>com.jsptags.navigation.pager.LastTag</tagclass>  
  246.     <teiclass>com.jsptags.navigation.pager.JumpTagExtraInfo</teiclass>  
  247.     <bodycontent>JSP</bodycontent>  
  248.     <attribute>  
  249.       <name>id</name>  
  250.       <required>false</required>  
  251.       <rtexprvalue>true</rtexprvalue>  
  252.     </attribute>  
  253.     <attribute>  
  254.       <name>export</name>  
  255.       <required>false</required>  
  256.       <rtexprvalue>false</rtexprvalue>  
  257.     </attribute>  
  258.     <attribute>  
  259.       <name>unless</name>  
  260.       <required>false</required>  
  261.       <rtexprvalue>true</rtexprvalue>  
  262.     </attribute>  
  263.   </tag>  
  264.   
  265.   
  266.   <tag>  
  267.     <name>skip</name>  
  268.     <tagclass>com.jsptags.navigation.pager.SkipTag</tagclass>  
  269.     <teiclass>com.jsptags.navigation.pager.PageTagExtraInfo</teiclass>  
  270.     <bodycontent>JSP</bodycontent>  
  271.     <attribute>  
  272.       <name>id</name>  
  273.       <required>false</required>  
  274.       <rtexprvalue>true</rtexprvalue>  
  275.     </attribute>  
  276.     <attribute>  
  277.       <name>export</name>  
  278.       <required>false</required>  
  279.       <rtexprvalue>false</rtexprvalue>  
  280.     </attribute>  
  281.     <attribute>  
  282.       <name>ifnull</name>  
  283.       <required>false</required>  
  284.       <rtexprvalue>true</rtexprvalue>  
  285.     </attribute>  
  286.     <attribute>  
  287.       <name>pages</name>  
  288.       <required>true</required>  
  289.       <rtexprvalue>true</rtexprvalue>  
  290.     </attribute>  
  291.   </tag>  
  292.   
  293.   
  294. </taglib>  

Spring3+ibatis (SQL Server)+pager-taglib.tld查询分页的实现