网上收集的分页代码

  1. 相当不错的JS分页代码
  2. <!--
  3. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  4. /**
  5. * 用js分页显示ul/ol的列表
  6. *
  7. * 这里的演示设置了自动滚动
  8. *
  9. * PHP versions 4 and 5
  10. *
  11. * LICENSE: This source file is subject to version 3.0 of the PHP license
  12. * that is available through the world-wide-web at the following URI:
  13. * http://www.php.net/license/3_0.txt.   If you did not receive a copy of
  14. * the PHP License and are unable to obtain it through the web, please
  15. * send a note to license@php.net so we can mail you a copy immediately.
  16. *
  17. * @package     GCCMS
  18. * @author      Yi Bo <etng2004@gmail.com>
  19. * @copyright   2000-2005 GCSOFT.COM & ETNG.NET
  20. * @license    http://www.php.net/license/3_0.txt   PHP License 3.0
  21. * @version     SVN: $Id: page.html 21 2006-01-22 09:35:58Z etng $
  22. * @link        $HeadURL: svn://dev.gccms.net/trunk/page.html $
  23. * @see
  24. * @since
  25. */
  26. --><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  27. <html xmlns="http://www.w3.org/1999/xhtml">
  28. <head>
  29. <meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
  30. <title>无标题文档</title>
  31. <style type="text/css">
  32. <!--
  33. /*这里填写自己需要的css定义*/
  34. body { width: 760px; padding: 0 0 0 0; margin: 0 auto 0 auto; font-size: 12px; font-family: "Arial""Helvetica""sans-serif"; }
  35. td { font-size: 12px; }
  36. ul,li,form,div,span { padding: 0 0 0 0; margin: 0 0 0 0; }
  37. .ctrlPages {COLOR: #f60;}
  38. .curPage {COLOR: #f00;}
  39. -->
  40. </style>
  41. <script language="JavaScript">
  42. <!--
  43. var ETNGpager = function( srcName, dstName, cntPP, cntPS )
  44. {
  45. this.srcName = srcName;
  46. this.dstName = dstName;
  47. this.curP   = 1;//默认当前页为第一页
  48. this.cntPP   = cntPP || 2;//默认每页两条纪录
  49. this.cntPS   = cntPS || 3;//默认每页显示5个分页上下文
  50. this.items   = [];
  51. this.showPNP = true;/*显示上下页链接*/
  52. this.showType = true;/*滑动分页*/
  53. this.result   = {pagedata:[],pagebar:&apos;&apos;,limit:[0,0],report:&apos;&apos;};
  54. this.parse();/*总纪录数*/
  55. }
  56. ETNGpager.prototype.page = function (){
  57. this.cntP = Math.ceil(this.cntR/this.cntPP);/*总页数*/
  58. this.cntS = Math.ceil(this.cntP/this.cntPS);/*总段数*/
  59. this.curS = Math.ceil(this.curP/this.cntPS);/*当前段*/
  60. this.preP = this.curP -1;/*上一页*/
  61. this.nextP = this.curP +1;/*下一页*/
  62. this.preS = this.curS -1;/*上一段*/
  63. this.nextS = this.curS +1;/*下一段*/
  64. this.startR = (this.curP -1)*this.cntPP + 1;/*起始纪录*/
  65. this.endR = (this.curP*this.cntPP >this.cntR)?this.cntR:this.curP*this.cntPP;/*结束纪录*/
  66. this.result[&apos;pagedata&apos;]=[];
  67. if(this.showType){
  68. this.perSide = Math.floor(this.cntPS/2);
  69. this.startP   = (this.curP > this.perSide)?(this.curP - this.perSide):1;
  70. this.endP   = (this.startP + this.cntPS)>this.cntP?this.cntP:(this.startP + this.cntPS);
  71. }else{
  72. this.startP   = (this.curS-1)*this.cntPS+1;
  73. this.endP   = (this.curS*this.cntPS>this.cntP)?this.cntP:(this.curS*this.cntPS);
  74. }
  75. for(var i = this.startP;i<=this.endP;i++){
  76. this.result[&apos;pagedata&apos;].push((i==this.curP)?&apos;<span class="curPage">&apos;+i+&apos;</span>&apos;:&apos;<span οnclick="page(&apos;+i+&apos;)">&apos;+i+&apos;</span>&apos;);
  77. }
  78. if(this.showPNP){
  79. if(this.curP>1)this.result[&apos;pagedata&apos;].unshift(&apos;<span οnclick="page(&apos;+(this.curP-1)+&apos;)">上一页</span>&apos;);
  80. if(this.curP<this.cntP)this.result[&apos;pagedata&apos;].push(&apos;<span οnclick="page(&apos;+(this.curP+1)+&apos;)">下一页</span>&apos;);
  81. }
  82. this.result[&apos;pagebar&apos;] = this.result[&apos;pagedata&apos;].join(&apos;  &apos;);
  83. this.result[&apos;limit&apos;] = [this.startR,this.endR];
  84. this.result[&apos;report&apos;] = &apos;共&apos;+this.cntR+&apos;条,当前页&apos;+this.startR+&apos;-&apos;+this.endR+&apos;,&apos;+this.curP+&apos;/&apos;+this.cntP+&apos;页&apos;;
  85. }
  86. ETNGpager.prototype.parse = function (){
  87. var obj = document.getElementById(this.srcName);
  88. for(var i = 0;i<obj.childNodes.length;i++){
  89. if(obj.childNodes[i].nodeType!=3)this.items[this.items.length]=obj.childNodes[i].innerHTML;
  90. }
  91. this.cntR = this.items.length;
  92. return this.items.length;
  93. }
  94. ETNGpager.prototype.create=function(){
  95. this.page();
  96. document.getElementById(this.dstName).innerHTML=&apos;<li>&apos;+this.items.slice(this.startR-1,this.endR).join(&apos;</li><li>&apos;)+&apos;</li>&apos;;
  97. document.getElementById(this.dstName).innerHTML+=&apos;<span class="ctrlPages">&apos;+this.result[&apos;pagebar&apos;]+this.result[&apos;report&apos;]+&apos;</span>&apos;;
  98. }
  99. //-->
  100. </script>
  101. </head>
  102. <body>
  103. <ul id="listcontent" style="display:none;">
  104. <li><a href=http://tech.sina.com.cn/i/2005-12-08/1204786367.shtml target=&apos;_blank&apos;>支付宝与六大代理签订协议 </a></li>
  105. <li><a href=http://forum.taobao.com/showThread.htm?thread=3123988&forum=14 target=&apos;_blank&apos;>刷卡积分可网上购物 </a></li>
  106. <li><a href=/alipay/news/sample/23492.htm target=&apos;_blank&apos;>支付宝为网店保驾护航 </a></li>
  107. <li><a href=http://it.people.com.cn/GB/8219/50656/52310/3822563.html target=&apos;_blank&apos;>支付宝红包送来红地毯 </a></li>
  108. <li><a href=/alipay/news/sample/22701.htm target=&apos;_blank&apos;>紧急天气预报"红色风暴"空降支付宝 </a></li>
  109. <li><a href=/alipay/news/sample/22699.htm target=&apos;_blank&apos;>小红包背后大名堂 </a></li>
  110. <li><a href=http://www.q88.net/SHOP_2005A/zfb.aspx target=&apos;_blank&apos;>Q88.net全面无缝接合支付宝 </a></li>
  111. <li><a href=/alipay/news/sample/21529.htm target=&apos;_blank&apos;>电子支付规范走出第一步 使用专业版受鼓励 </a></li>
  112. <li><a href=/alipay/news/sample/19786.htm target=&apos;_blank&apos;>从支付宝看电子商务的发展 </a></li>
  113. <li><a href=/alipay/news/sample/19784.htm target=&apos;_blank&apos;>谁能与支付宝PK? </a></li>
  114. <li><a href=/alipay/news/sample/19618.htm target=&apos;_blank&apos;>国内第一家引入支付宝的网络图库正式开通 </a></li>
  115. <li><a href=/alipay/news/sample/19475.htm target=&apos;_blank&apos;>新浪网:中关村在线加入支付宝联盟 </a></li>
  116. <li><a href=/alipay/news/sample/19471.htm target=&apos;_blank&apos;>千家网店加入支付宝联盟 </a></li>
  117. <li><a href=/alipay/news/sample/18549.htm target=&apos;_blank&apos;>我与支付宝的分分秒秒 </a></li>
  118. <li><a href=/alipay/news/sample/18207.htm target=&apos;_blank&apos;>支付宝—放心“网宝”的理由 </a></li>
  119. <li><a href=/alipay/news/sample/17944.htm target=&apos;_blank&apos;>欧飞数卡携手支付宝,再创新高 </a></li>
  120. <li><a href=/alipay/news/sample/17803.htm target=&apos;_blank&apos;>莎莎香水网:支付宝助我完成销售计划 </a></li>
  121. <li><a href=/alipay/news/sample/17801.htm target=&apos;_blank&apos;>使用支付宝:一个月交易额翻5倍 </a></li>
  122. <li><a href=/alipay/news/sample/17799.htm target=&apos;_blank&apos;>支付宝:一个普通站长的自述 </a></li>
  123. <li><a href=/alipay/news/sample/17797.htm target=&apos;_blank&apos;>新开网店如何日交易额达8000元? </a></li>
  124. <li><a href=/alipay/news/sample/17563.htm target=&apos;_blank&apos;>名大数码:网店月交易额如何突破30万 </a></li>
  125. <li><a href=http://it.sohu.com/20050916/n240400443.shtml target=&apos;_blank&apos;>中国卡网结盟支付宝创交易量周增长新高 </a></li>
  126. <li><a href=http://it.people.com.cn/GB/42891/42894/3676101.html target=&apos;_blank&apos;>支付宝联盟与合作伙伴合作在人民网推广 </a></li>
  127. <li><a href=http://forum.taobao.com/show_thread-50---103546-.htm target=&apos;_blank&apos;>网络银行使用全攻略---足不出户查看汇款明细 </a></li>
  128. <li><a href=http://forum.taobao.com/show_thread-50---1561087-.htm target=&apos;_blank&apos;>"支付宝购物体验"征文-----贿赂 </a></li>
  129. <li><a href=http://forum.taobao.com/show_thread-50---2102458-.htm target=&apos;_blank&apos;>卖家谈:谁是支付宝最终的获利者? </a></li>
  130. <li><a href=http://forum.taobao.com/show_thread-50---1617047-.htm target=&apos;_blank&apos;>淘宝两钻卖家感悟支付宝 </a></li>
  131. <li><a href=http://forum.taobao.com/show_thread-50---1686484-.htm target=&apos;_blank&apos;>支付宝“即时到帐交易”的使用经验及建议 </a></li>
  132. <li><a href=http://forum.taobao.com/show_thread-50---1794216-.htm target=&apos;_blank&apos;>我的第一笔网上交易 </a></li>
  133. </ul>
  134. <ul id="listcontent2">列表信息加载中,请您稍等……</ul>
  135. <script language="JavaScript">
  136. <!--
  137. var pager = new ETNGpager(&apos;listcontent&apos;,&apos;listcontent2&apos;,10,5);
  138. var curP = 1;
  139. showtime = setInterval("page()"5000);
  140. function page(i){
  141. curP =(curP>pager.cntP)?1:curP;
  142. if(i){
  143. curP = n =i;
  144. }else{
  145. n = curP++;
  146. }
  147. pager.curP = (n>pager.cntP)?pager.cntP:n;
  148. pager.create();
  149. }
  150. //-->
  151. </script>
  152. </body>
  153. </html>
  154.  JAVA 反射技术 万用分页标签 核心代码
  155. Java中,反射是一种强大的工具。它使您能够创建灵活的代码,这些代码可以在运行时装配,无需在组件之间进行源代表链接。反射允许我们在编写与执行时, 使我们的程序代码能够接入装载到JVM中的类的内部信息,而不是源代码中选定的类协作的代码。这使反射成为构建灵活的应用的主要工具。但需注意的是:如果 使用不当,反射的成本很高。
  156. 下面要介绍的例子是我从事struts开发时开发 万用分页标签 所用到的技术,该标签能根据传入的bean list自动生成分页表格
  157. 很多框架级应用中都必须用反射技术才能达到灵活泛用的目的,典型的struts,spring,hibernate等
  158. 下面我们就切入正题,看看反射技术是怎么应用的
  159. 首先我们建立一个工程,名为JavaReflect,再建立TestBean,TestMain,TabTest存入com.sdna.reflect包中
  160. 结构如下
  161. 如图:1
  162. <!--[if !vml]--><!--[endif]-->
  163. ==========TestBean 为任意一个符合Bean命名规范的类
  164. package com.sdna.reflect;
  165. public class TestBean {
  166.     private int userID;
  167.     private String userName;
  168.     private String userSex;
  169.     private int userAge;
  170.     private String userAddr;
  171.     public TestBean() {
  172.         try {
  173.             jbInit();
  174.         } catch (Exception exception) {
  175.             exception.printStackTrace();
  176.         }
  177.     }
  178.     private void jbInit() throws Exception {
  179.     }
  180.     public void setUserID(int userID) {
  181.         this.userID = userID;
  182.     }
  183.     public void setUserName(String userName) {
  184.         this.userName = userName;
  185.     }
  186.     public void setUserSex(String userSex) {
  187.         this.userSex = userSex;
  188.     }
  189.     public void setUserAge(int userAge) {
  190.         this.userAge = userAge;
  191.     }
  192.     public void setUserAddr(String userAddr) {
  193.         this.userAddr = userAddr;
  194.     }
  195.     public int getUserID() {
  196.         return userID;
  197.     }
  198.     public String getUserName() {
  199.         return userName;
  200.     }
  201.     public String getUserSex() {
  202.         return userSex;
  203.     }
  204.     public int getUserAge() {
  205.         return userAge;
  206.     }
  207.     public String getUserAddr() {
  208.         return userAddr;
  209.     }
  210. }
  211. ====================
  212. ==========TestMain 为测试打印某实例所有方法,参数,返回值
  213. package com.sdna.reflect;
  214. import java.lang.reflect.*;
  215. public class TestMain {
  216.     public TestMain() {
  217.     }
  218.     public static void main(String[] args) {
  219.         TestBean obj = new TestBean();
  220.         Class c = obj.getClass();
  221.         Method[] theMethods = c.getMethods();
  222.         for (int i = 0; i < theMethods.length; i++) {
  223.             String methodString = theMethods[i].getName();
  224.             System.out.println("Name: " + methodString);
  225.             String returnString =
  226.                     theMethods[i].getReturnType().getName();
  227.             System.out.println(" Return Type: " + returnString);
  228.             Class[] parameterTypes = theMethods[i].getParameterTypes();
  229.             System.out.print(" Parameter Types:");
  230.             for (int k = 0; k < parameterTypes.length; k++) {
  231.                 String parameterString = parameterTypes[k].getName();
  232.                 System.out.print(" " + parameterString);
  233.             }
  234.             System.out.println("/n");
  235.         }
  236.         try {
  237.             theMethods[2].invoke(obj, "sky");
  238.             System.out.println(obj.getUserName());
  239.         } catch (InvocationTargetException ex) {
  240.             ex.printStackTrace();
  241.         } catch (IllegalArgumentException ex) {
  242.             ex.printStackTrace();
  243.         } catch (IllegalAccessException ex) {
  244.             ex.printStackTrace();
  245.         }
  246.     }
  247. }
  248. ====================
  249. ==========TabTest 为读任意一个符合Bean命名规范的实例
  250. package com.sdna.reflect;
  251. import java.lang.reflect.*;
  252. public class TabTest {
  253.     public TabTest() {
  254.     }
  255.     public static void main(String[] args) {
  256.         TestBean obj = new TestBean();
  257.         obj.setUserID(10001);
  258.         obj.setUserName("sky");
  259.         obj.setUserSex("boy");
  260.         obj.setUserAge(23);
  261.         obj.setUserAddr("wuhan");
  262.         Class objClass = obj.getClass();
  263.         Field fields[] = objClass.getDeclaredFields();
  264. //        Method[] methods = objClass.getMethods();
  265.         Method tempMethod = null;
  266.         String strMethod = null;
  267.         for (int i = 0; i < fields.length; i++) {
  268.             try {
  269.                 String fieldName = fields[i].getName();
  270.                 Class typeClass = fields[i].getType();
  271.                 String fieldType = typeClass.getName();
  272.                 strMethod = fieldName;
  273.                 strMethod = "get" +
  274.                             strMethod.replaceFirst(strMethod.substring(01),
  275.                         strMethod.substring(01).toUpperCase());
  276.                 tempMethod = objClass.getMethod(strMethod);
  277.                 System.out.println("Name: " + fieldName + ", Type: "
  278.                                    + fieldType + ", Values: " + tempMethod.invoke(obj));
  279.             } catch (Exception ex) {
  280.                 ex.printStackTrace();
  281.             }
  282.         }
  283.     }
  284. }
  285. ====================
  286. TestMain运行结果
  287. 如图:2
  288. <!--[if !vml]--><!--[endif]-->
  289. TabTest运行结果
  290. 如图:2
  291.   <!--[if !vml]--><!--[endif]-->
  292. jsp 自定义分页标签
  293. 花了一上午简单研究了下自定义标签。就弄了个自定义的分页标签。代码没有怎么仔细优化。先贴上来,:)
  294. 1,标签的实现类NumenTag.java
  295. import java.util.ArrayList;
  296. import java.util.HashMap;
  297. import java.util.List;
  298. import java.util.Map;
  299. import javax.servlet.jsp.JspException;
  300. import javax.servlet.jsp.JspWriter;
  301. import javax.servlet.jsp.tagext.TagSupport;
  302. /** *//**
  303.  * 自定义的分页标签
  304.  * 
  305.  * @author 马万林
  306.  * 
  307.  */
  308. public class NumenTag extends TagSupport {
  309.     private String url = null;
  310.     private Result result;
  311.     private String page = null;
  312.     private String[] p;
  313.     private Map kv = new HashMap();
  314.     private List key = null;
  315.     /** *//**
  316.      * 分离参数
  317.      * @param params
  318.      */
  319.     public void setParams(String params) {
  320.         key = new ArrayList();
  321.         p = params.split(";");
  322.         for (int i = 0; p.length>1&&i < p.length; i++) {
  323.             kv.put(p[i].split(":")[0], p[i].split(":")[1]);
  324.             key.add(p[i].split(":")[0]);
  325.         }
  326.     }
  327.     /** *//**
  328.      * 分页的结果
  329.      * @param result
  330.      */
  331.     public void setResult(String result) {
  332.         this.result = (Result) pageContext.getRequest().getAttribute("result");
  333.         this.page = (String) pageContext.getRequest().getAttribute("page");
  334.     }
  335.     public void setUrl(String url) {
  336.         this.url = url;
  337.     }
  338.     public int doStartTag() throws JspException {
  339.         StringBuffer sb = null;
  340.         if (result != null && result.getContent() != null
  341.                 && result.getContent().size() > 0) {
  342.             sb = new StringBuffer("分页: ");
  343.             if (result.getPage().getHasPrePage()){
  344.                 sb.append("<a href=&apos;").append(url);
  345.                 if (url.indexOf("?") == -1)
  346.                     sb.append("?page=1");
  347.                 else
  348.                     sb.append("&page=1");
  349.                 linkParams(sb);
  350.                 sb.append("&apos;>").append("首页").append("</a>").append(" | ");
  351.                 sb.append("<a href=&apos;").append(url);
  352.                 if (url.indexOf("?") == -1)
  353.                     sb.append("?page=").append(
  354.                             result.getPage().getPreviousPage());
  355.                 else
  356.                     sb.append("&page=").append(
  357.                             result.getPage().getPreviousPage());
  358.                 linkParams(sb);
  359.                 sb.append("&apos;>").append("上页").append("</a>").append(" | ");
  360.             }
  361.             if (result.getPage().getHasNextPage()) {
  362.                 sb.append("<a href=&apos;").append(url);
  363.                 if (url.indexOf("?") == -1)
  364.                     sb.append("?page=").append(result.getPage().getNextPage());
  365.                 else
  366.                     sb.append("&page=").append(result.getPage().getNextPage());
  367.                 linkParams(sb);
  368.                 sb.append("&apos;>").append("下页").append("</a>").append(" | ");
  369.                 sb.append("<a href=&apos;").append(url);
  370.                 if (url.indexOf("?") == -1)
  371.                     sb.append("?page=").append(result.getPage().getTotalPage());
  372.                 else
  373.                     sb.append("&page=").append(result.getPage().getTotalPage());
  374.                 linkParams(sb);
  375.                 sb.append("&apos;>").append("尾页").append("</a>");
  376.             }
  377.             sb
  378.                     .append(" 跳转到:<select name=&apos;goToPage&apos; οnchange=&apos;javascript:goPage(this.value)&apos;>");
  379.             int s = result.getPage().getTotalCount();
  380.             for (int i = 1; i <= s; i++) {
  381.                 sb.append("<option value=").append(i);
  382.                 if (Integer.parseInt(page) == i)
  383.                     sb.append(" selected");
  384.                 sb.append(">第").append(i).append("页</option>");
  385.             }
  386.             sb.append("</select>");
  387.             sb.append("<script language=javascript>");
  388.             sb.append("function goPage(page){").append("window.location=&apos;")
  389.                     .append(url);
  390.             if (url.indexOf("?") == -1)
  391.                 sb.append("?page=&apos;+").append("page");
  392.             else
  393.                 sb.append("&page=&apos;+").append("page");
  394.             sb.append("+&apos;");
  395.             linkParams(sb);
  396.             sb.append("&apos;;");
  397.             sb.append("}");
  398.             sb.append("</script>");
  399.         }
  400.         try {
  401.             if (sb != null) {
  402.                 JspWriter out = pageContext.getOut();
  403.                 out.print(sb);
  404.             }
  405.         } catch (Exception e) {
  406.             throw new JspException(e);
  407.         }
  408.         return EVAL_PAGE;
  409.     }
  410.     
  411.     /** *//**
  412.      * 链接参数处理
  413.      * @param sb
  414.      */
  415.     private void linkParams(StringBuffer sb) {
  416.         for (int i = 0; i < key.size(); i++) {
  417.             sb.append("&").append(key.get(i)).append("=").append(
  418.                     kv.get(key.get(i)));
  419.         }
  420.     }
  421. }
  422. 2,标签定义文件numentag.tld
  423. <?xml  version="1.0"  encoding="utf-8"  ?>
  424.  <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
  425. <taglib>
  426.     <tlib-version>1.0</tlib-version>
  427.     <jsp-version>1.2</jsp-version>
  428.     <short-name>numen</short-name>
  429.     <!--OutputTag-->
  430.     <tag>
  431.         <name>out</name>
  432.         <tag-class>cn.cityyouth.util.NumenTag</tag-class>
  433.         <body-content>empty</body-content>
  434.         <attribute>
  435.             <name>url</name>
  436.             <required>true</required>
  437.             <rtexprvalue>true</rtexprvalue>
  438.         </attribute>
  439.         <attribute>
  440.             <name>result</name>
  441.             <required>true</required>
  442.             <rtexprvalue>true</rtexprvalue>
  443.         </attribute>
  444.         <attribute>
  445.             <name>params</name>
  446.             <required>false</required>
  447.             <rtexprvalue>true</rtexprvalue>
  448.         </attribute>
  449.     </tag>
  450. </taglib>
  451. 3.js
  452. <%@ page contentType="text/html; charset=utf-8"%>
  453. <%@ include file="/common/taglibs.jsp"%>
  454. <%@ taglib  uri="/WEB-INF/numentag.tld"  prefix="n"%>
  455. <html>
  456. <head>
  457. </head>
  458. <body>
  459. <table>
  460. <tr><td>
  461. <n:out url="test.do" result="result" params="${type}"/>
  462. </td></tr>
  463. </table>
  464. </body>
  465. </html>
  466. 利用向量实现分页的一种方式
  467.   /** 当前页 */   
  468.           private int nowPage = 1;    
  469.      
  470.           /** 最大页 */   
  471.           private int maxPage = 0;    
  472.      
  473.           /** 显示数目 */   
  474.           private String showRows = new String();    
  475.      
  476.           /**全部Vector */   
  477.           private Vector allList = new Vector();    
  478.      
  479.           /** 每页Vector */   
  480.           private Vector resultList = new Vector();    
  481.      
  482.           //改变页数,更新变量    
  483.           public ChangePage(Vector pList, int pNowPage, String pShowRows) {    
  484.                    
  485.               this.allList = pList;    
  486.               //当前页    
  487.               try {    
  488.                   this.nowPage = pNowPage;    
  489.                   if (this.nowPage == 0) {    
  490.                       this.nowPage = 1;    
  491.                   }    
  492.               } catch (Exception e) {    
  493.                   this.nowPage = 1;    
  494.               }    
  495.      
  496.               //显示数目    
  497.               try {    
  498.                   if (!pShowRows.equals(Constants.SHOWALL)) {    
  499.                       if (Integer.parseInt(pShowRows)==0) {    
  500.                           this.showRows = Constants.SHOWALL;    
  501.                       } else {    
  502.                           this.showRows = pShowRows;    
  503.                       }    
  504.                   } else {    
  505.                       this.showRows = pShowRows;    
  506.                   }    
  507.               } catch (Exception e) {    
  508.                   this.showRows = Constants.SHOWALL;    
  509.               }    
  510.      
  511.               //最大页,每页    
  512.               if (this.showRows.equals(Constants.SHOWALL)) {    
  513.                   //当前页    
  514.                   this.resultList = pList;    
  515.                   //最大页    
  516.                   this.maxPage = 1;    
  517.               } else {    
  518.                   //显示数目    
  519.                   int counts = Integer.parseInt(this.showRows);    
  520.                   int tmpCount = 0;    
  521.                   Vector vAllPage = new Vector();    
  522.                   Vector vOnePage = new Vector();    
  523.                   for (int i=0; i<THIS.ALLLIST.SIZE(); (tmpCount if tmpCount++; { i++)>counts) {    
  524.                           vAllPage.add(vOnePage);    
  525.                           vOnePage = new Vector();    
  526.                           tmpCount = 1;    
  527.                       }    
  528.                       vOnePage.add(allList.get(i));    
  529.                   }    
  530.                   if (tmpCount<=counts) {    
  531.                       vAllPage.add(vOnePage);    
  532.                   }    
  533.      
  534.                   this.maxPage = vAllPage.size();    
  535.      
  536.                   if (this.nowPage>this.maxPage) {    
  537.                       this.nowPage = this.maxPage;    
  538.                   }    
  539.                   this.resultList = (Vector) vAllPage.get(this.nowPage-1);    
  540.      
  541.               }    
  542.           } 
  543. hibernate分页
  544. 分页在任何系统中都是非常头疼的事情,有的数据库在语法上支持分页,而有的数据库则需要使用可滚动游标来实现,并且在不支持可滚动游标的系统上只能使用单向游标逐步接近要取得的数据。
  545.  Hibernate提供了一个支持跨系统的分页机制,这样无论底层是什么样的数据库都能用统一的接口进行分页操作。比如下面的代码就是从第500条开始取出100条记录: 
  546. Query q = session.createQuery("from FooBar as f"); 
  547. q.setFirstResult(500); 
  548. q.setMaxResults(100); 
  549. List l = q.list(); 
  550. 那么Hibernate底层如何实现分页的呢?Hibernate根据Query拼装SQL语句的地方是在org.hibernate.loader.Loader类的prepareQueryStatement方法中,对分页支持的代码在这一段中可以发现:
  551. if (useLimit) 
  552. {
  553. sql = dialect.getLimitString( 
  554.      sql.trim(), //use of trim() here is ugly?
  555.      useOffset ? getFirstRow(selection) : 0
  556.      getMaxOrLimit(selection, dialect) 
  557.     );
  558.  }
  559.  此处调用Dialect的getLimitString方法来得到不同平台的分页语句。
  560. 在MySQLDialect中是如下实现getLimitString方法的:
  561. public String getLimitString(String sql, boolean hasOffset) 
  562. {
  563. return new StringBuffer( sql.length()+20 )
  564. .append(sql)
  565. .append( hasOffset ? " limit ?, ?" : " limit ?")
  566. .toString();
  567. }
  568.  这是MySQL的专用分页语句,再来看Oracle9Dialect:
  569.  public String getLimitString(String sql, boolean hasOffset) {
  570.   
  571.   sql = sql.trim();
  572.   boolean isForUpdate = false;
  573.   if ( sql.toLowerCase().endsWith(" for update") ) {
  574.    sql = sql.substring( 0, sql.length()-11 );
  575.    isForUpdate = true;
  576.   }
  577.   
  578.   StringBuffer pagingSelect = new StringBuffer( sql.length()+100 );
  579.   if (hasOffset) {
  580.    pagingSelect.append("select * from ( select row_.*, rownum rownum_ from ( ");
  581.   }
  582.   else {
  583.    pagingSelect.append("select * from ( ");
  584.   }
  585.   pagingSelect.append(sql);
  586.   if (hasOffset) {
  587.    pagingSelect.append(" ) row_ where rownum <= ?) where rownum_ > ?");
  588.   }
  589.   else {
  590.    pagingSelect.append(" ) where rownum <= ?");
  591.   } 
  592.   if ( isForUpdate ) {
  593.    pagingSelect.append( " for update" );
  594.   }
  595.   
  596.   return pagingSelect.toString();
  597.  } 
  598. Oracle采用嵌套3层的查询语句结合rownum来实现分页,这在Oracle上是最好的方式,因为如果只是一层或者两层的查询语句的rownum不能支持order by。
  599. 此 外Interbase,PostgreSQL,HSQL等也在语法级别上支持分页,具体实现可以查看相应的Dialect实现。如果数据库不支持分页的 SQL语句,那么如果数据库支持可滚动游标,那么Hibernate就会采使用ResultSet的absolute方法直接移到查询起点;否则使用循环 语句,通过rs.next一步步移动到要查询的数据处: 
  600. final int firstRow = getFirstRow( selection );
  601. if ( firstRow != 0 ) 
  602. {
  603. if ( getFactory().getSettings().isScrollableResultSetsEnabled() ) 
  604. {
  605. // we can go straight to the first required row
  606. rs.absolute( firstRow );
  607. }
  608. else 
  609. {
  610. // we need to step through the rows one row at a time (slow)
  611. for ( int m = 0; m < firstRow; m++ ) rs.next();
  612. }
  613. }
  614. 可见使用Hibernate,在进行查询分页的操作上,是具有非常大的灵活性,Hibernate会首先尝试用特定数据库的分页sql,如果没 用,再尝试Scrollable,如果不支持Scrollable再采用rset.next()移动的办法。这样既兼顾了查询分页的性能,同时又保证了代 码在不同的数据库之间的可移植性。
  615. Jsp 分页代码1
  616. <%@   page   contentType= "text/html;   charset=GBK "   %> 
  617. <%@page   import"java.util.ArrayList "%> 
  618. <%@page   import"java.util.Iterator "%> 
  619. <%@page   import"bean.xxx.bean名 "%> 
  620. <jsp:useBean   id= "db "   scope= "page "   class"database.连接数据库的类 "/> 
  621. <jsp:useBean   id= "bean自己起的名 "   scope= "page "   class"bean.xxx.bean名 "/> 
  622. ...... 
  623.   <!--分页================================================================--> 
  624.         <% 
  625.         String   p   =   request.getParameter( "page "); 
  626.         int   tempPage   =   1
  627.         if   (p   !=   null
  628.             tempPage   =   Integer.parseInt(p); 
  629.         //从会话里得到的   ArrayList 
  630.         ArrayList   list   =   (ArrayList)   session.getAttribute( "放入会话的   ArrayList   名 "); 
  631.         //如果会话里没有值   也就是第一次到本页面进行的处理   可保证不出现空指针异常 
  632.         if(list   ==   null){ 
  633.             String   sql   =   "select   *   from   表名 "
  634.             list   =   db.自己写的得到ArrayList的方法(sql); 
  635.         } 
  636.         int   recordsCount   =   list.size();   //总记录数 
  637. //         System.out.println(recordsCount); 
  638.         int   pageSize   =   10;   //每页记录数   可以改成任意数值   
  639.         int   pagesCount   =   (recordsCount   +   pageSize   -   1)   /   pageSize;   //总页数 
  640. //         System.out.println(pagesCount); 
  641.         if   (tempPage   <   0
  642.             tempPage   =   1
  643.         if   (tempPage   >   pagesCount) 
  644.             tempPage   =   pagesCount; 
  645.         int   curPage   =   tempPage;   //当前页结束页码 
  646. //         System.out.println(curPage); 
  647.         int   start   =   (curPage   -   1)*pageSize;   //当前页起始页码 
  648. //         System.out.println(start); 
  649.         int   end   =   curPage*pageSize   +   1;   //当前页结束页码 
  650. //         System.out.println(end); 
  651.         int   i   =   0
  652.         Iterator   it   =   list.iterator();   //叠代 
  653.         while   (it.hasNext())   {   //it.hasNext()判断有无下一条记录 
  654.             bean自己起的名   =   (bean名)   it.next();   //it.next()指针指向下一条 
  655.             i++; 
  656.             if   (i   >   start   &&   i   <   end)   { 
  657.             %> 
  658. <!--=====================================================================--> 
  659. ........ 
  660. <tr> 
  661.         <td> <%=bean自己起的名.getXXXXX()%> </td> 
  662. </tr> 
  663. ....... 
  664. <!--分页==========================================--> 
  665.         <% 
  666.         } 
  667.     } 
  668.     %> 
  669.         <tr   bgcolor= "#ffffff "
  670.             <td   height= "27 "   colspan= "3 "> <div   align= "center "
  671.                     <%if   (curPage   !=   1)   {%> 
  672.                     <a   href= "本页面名.jsp?page=1 "> 第一页 </a>   <a   href= "本页面名.jsp?page= <%=curPage-1%> "> 上一页 </a> 
  673.                     <% 
  674.     } 
  675.             if   (curPage   !=   pagesCount)   { 
  676. %> 
  677.                     <a   href= "本页面名.jsp?page= <%=curPage+1%> "> 下一页 </a>   <a   href= "本页面名.jsp?page= <%=pagesCount%> "> 最后一页 </a> 
  678.                     <%}%> 
  679.                 </div> </td> 
  680.         </tr> 
  681.         <!--=========================================================-->
  682. Jsp 分页代码2
  683. jsp列表下方加上 
  684. <% 
  685.       int   startRow   =   0,totalRow=0
  686.       try   {startRow=Integer.parseInt(request.getParameter( "statrow "));}catch   (Exception   ex)   {} 
  687.       try   {totalRow=Integer.parseInt((String)request.getAttribute( "totalRow "));}catch   (Exception   ex)   {} 
  688.               PageDivider   pageDiv   =   new   PageDivider(selfUrl, "a=a ",totalRow,startRow,15); 
  689. %> 
  690. <table   border= "0 "
  691.     <%=pageDiv.getFontTypeOut()%> 
  692. </table> 
  693. pageDiv类 
  694. package   com.sc.util; 
  695. /** 
  696.   *   <p> Title:用于分页时简化在jsp页面出现太多java代码   </p> 
  697.   *   <p> Description:   </p> 
  698.   *   <p> Copyright:   Copyright   (c)   2003 </p> 
  699.   *   <p> Company:drgsqd   </p> 
  700.   *   @author   unascribed 
  701.   *   @version   1.0 
  702.   */ 
  703. public   class   PageDivider   implements   java.io.Serializable   { 
  704.     private   String   _selfPath=null
  705.     private   String   _paraStr=null
  706.     private   int   _totalRow=0
  707.     private   int   _startRow=0
  708.     private   int   _showRow=0
  709. /自身参数 
  710.     private   int   _priRow   =   0
  711.     private   int   _nextRow   =   0
  712.     private   int   _endRow   =   0
  713.     private   int   _totalPage   =   0
  714.     private   int   _nowPage   =   0
  715.     public   PageDivider(String   selfPath,String   paraStr,int   totalRow,int   startRow,int   showRow)   { 
  716.         this._selfPath   =   selfPath; 
  717.         this._paraStr   =   paraStr; 
  718.         this._totalRow   =   totalRow; 
  719.         this._startRow   =   startRow; 
  720.         this._showRow   =   showRow; 
  721.         initValues(); 
  722.     } 
  723.     private   void     initValues(){ 
  724.         if(this._paraStr==null)this._paraStr= " "
  725.         if(this._selfPath==null)this._selfPath= " "
  726.         if(this._showRow> this._totalRow)this._startRow=0
  727.         if(this._startRow> this._totalRow)this._startRow=this._totalRow; 
  728.         this._priRow   =   this._startRow   -   this._showRow; 
  729.         this._nextRow   =   this._startRow   +   this._showRow; 
  730.         if(this._totalRow%this._showRow!=0
  731.                   this._totalPage   =   this._totalRow/this._showRow+1
  732.         else   this._totalPage   =   this._totalRow/this._showRow; 
  733.         this._endRow   =   this._showRow*(this._totalPage-1); 
  734.         this._nowPage   =   this._startRow/this._showRow+1
  735.     } 
  736.     public   String   getFontTypeOut(){ 
  737.         StringBuffer   outStrBuf=new   StringBuffer(); 
  738.                       outStrBuf.append( " <tr> <td>   "); 
  739.                   outStrBuf.append( " 第 <font   color=red> "+this._nowPage+ " </font> 页 / 共 <font   color=red> "+this._totalPage+ " </font> 页  共 <font   color=red> "+this._totalRow+ " </font> 条 </td> <td> "); 
  740.             if(this._startRow> 0){ 
  741.                       outStrBuf.append( " <a   href=/ " "+this._selfPath+ ""+this._paraStr+ "&statrow=0"> 首页 </a>    /n "); 
  742.                       outStrBuf.append( " <a   href=/ " "+this._selfPath+ ""+this._paraStr+ "&statrow= "+this._priRow+ ""> 上一页 </a> /n "); 
  743.             }else
  744.                       outStrBuf.append( " <font   color=#cccccc> 首页   上一页 </font> /n "); 
  745.             } 
  746.                       outStrBuf.append( " </td> <td>   "); 
  747.             if(this._nowPage <this._totalPage){ 
  748.                       outStrBuf.append( " <a   href=/ " "+this._selfPath+ ""+this._paraStr+ "&statrow= "+this._nextRow+ ""> 下一页 </a>    /n "); 
  749.                       outStrBuf.append( " <a   href=/ " "+this._selfPath+ ""+this._paraStr+ "&statrow= "+this._endRow+ ""> 尾页 </a> /n "); 
  750.             }else
  751.                       outStrBuf.append( " <font   color=#cccccc> 下一页   尾页 </font> /n "); 
  752.             } 
  753.             if(this._totalPage> 1){ 
  754.                 outStrBuf.append( " </td> <td> "); 
  755.                 outStrBuf.append( " <script>   function   pageGoTo(objUrl,objcont,objValue){window.location=   objUrl+(objValue*objcont);} </script> "); 
  756.                 outStrBuf.append( "转到第 <SELECT   name=/ "page/ "   class=/ "select/ "   οnchange=/ "pageGoTo( &apos; "+this._selfPath+ ""+this._paraStr+ "&statrow= &apos;, "+this._showRow+ ",this.value);/ "> "); 
  757.                     for(int   i=1;i <=this._totalPage;i++){ 
  758.                           outStrBuf.append( " <OPTION   "+(this._nowPage==i? "selected "" ")+ "   value= "+(i-1)+ "> "+i+ " </OPTION> "); 
  759.                     } 
  760.                 outStrBuf.append( " </SELECT> 页 "); 
  761.             } 
  762.             outStrBuf.append( "   </td> </tr> /n "); 
  763.                 return   outStrBuf.toString(); 
  764.     } 
  765.     public   String   getInputTypeOut(){ 
  766.             String   outStr= " "
  767.                 outStr+= " "
  768.             return   outStr; 
  769.     } 
  770. 查询语句加上参数startRow,表示页数
  771. Jsp 分页代码3
  772. /** 
  773. *   Created   by   cyril.gu   on   Mar   24,   2007   12:00:57   PM 
  774. *   Email:zjjsgwm@163.com 
  775. **/ 
  776. import   java.util.List; 
  777. public   class   PageList   { 
  778. private   int   currentPage   =   1//当前页码 
  779. private   int   countPage   =   0//总页数 
  780. private   int   pageRows   =   0//每页显示记录数 
  781. private   int   countRows   =   0;   //总记录数 
  782. //private   int   currentSize; //当前页记录数 
  783. private   List   list; //结果集 
  784. public   String   toString()   { 
  785. StringBuffer   s   =   new   StringBuffer(); 
  786. s.append( " <input   type=/ "hidden/ "   id=/ "pageList.currentPage/ "   name=/ "pageList.currentPage/ "/> "); 
  787. s.append( "第 "+currentPage+ "页/共 "+countPage+ "页 "); 
  788. s.append( " 总 "+countRows+ "条记录 "); 
  789. /**首页**/ 
  790. if(this.countPage> 1   &&   this.currentPage> 1
  791. s.append( "   <a   href=/ "javascript:document.getElementById( &apos;pageList.currentPage &apos;).value= &apos;1 &apos;;doPagination();/ "> 首页 </a> "); 
  792. else 
  793. s.append( "  首页 "); 
  794. /**上一页**/ 
  795. if(this.currentPage> 1
  796. s.append( "  <a   href=/ "javascript:document.getElementById( &apos;pageList.currentPage &apos;).value= &apos; "+(this.currentPage-1)+ " &apos;;doPagination();/ "> 上一页 </a> "); 
  797. else 
  798. s.append( " 上一页 "); 
  799. /**下一页**/ 
  800. if(this.countPage> 1   &&   this.currentPage <this.countPage) 
  801. s.append( "  <a   href=/ "javascript:document.getElementById( &apos;pageList.currentPage &apos;).value= &apos; "+(this.currentPage+1)+ " &apos;;doPagination();/ "> 下一页 </a> "); 
  802. else 
  803. s.append( " 下一页 "); 
  804. /**尾页**/ 
  805. if(this.countPage> 1   &&   this.currentPage <this.countPage) 
  806. s.append( "  <a   href=/ "javascript:document.getElementById( &apos;pageList.currentPage &apos;).value= &apos; "+this.countPage+ " &apos;;doPagination();/ "> 尾页 </a> "); 
  807. else 
  808. s.append( " 尾页 "); 
  809. /**转到**/ 
  810. s.append( "  转至: <input   type=/ "text/ "   id=/ "pageList.toPage/ "   name=/ "pageList.toPage/ "   value=/ " "+this.currentPage+ ""   style=/ "width:25px;/ "/> "); 
  811. s.append( " <input   type=/ "button/ "   value=/ "GO/ "   οnclick=/ "document.getElementById( &apos;pageList.currentPage &apos;).value=document.getElementById( &apos;pageList.toPage &apos;).value;doPagination();/ "/   style=/ "width:25px;/ "> "); 
  812. s.append( "  每页显示: <input   type=/ "text/ "   id=/ "pageList.pageRows/ "   name=/ "pageList.pageRows/ "   value=/ " "+this.pageRows+ ""   style=/ "width:25px;/ "/> "); 
  813. s.append( " <input   type=/ "button/ "   value=/ "OK/ "   οnclick=/ "doPagination();/ "/   style=/ "width:25px;/ "> "); 
  814. return   s.toString(); 
  815. public   int   getCountPage()   { 
  816. return   countPage; 
  817. public   int   getCountRows()   { 
  818. return   countRows; 
  819. /** 
  820.   *   判断要跳至的页不能大于总页数 
  821.   *   @return 
  822.   */ 
  823. public   int   getCurrentPage()   { 
  824. return   currentPage; 
  825. /** 
  826.   *   当每页设置数为0时,自动调整为每页显示20 
  827.   *   @return 
  828.   */ 
  829. public   int   getPageRows()   { 
  830. if(pageRows==0
  831. pageRows   =   20
  832. return   pageRows; 
  833. public   void   setCountPage(int   countPage)   { 
  834. this.countPage   =   countPage; 
  835. public   void   setCountRows(int   countRows)   { 
  836. this.countRows   =   countRows; 
  837. public   void   setCurrentPage(int   currentPage)   { 
  838. this.currentPage   =   currentPage; 
  839. public   void   setPageRows(int   pageRows)   { 
  840. this.pageRows   =   pageRows; 
  841. public   List   getList()   { 
  842. return   list; 
  843. public   void   setList(List   list)   { 
  844. this.list   =   list; 
  845. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值