﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>BlogJava-果酱-文章分类-Java web技术</title><link>http://www.blogjava.net/chen45257211/category/49088.html</link><description /><language>zh-cn</language><lastBuildDate>Sat, 10 Nov 2012 16:40:16 GMT</lastBuildDate><pubDate>Sat, 10 Nov 2012 16:40:16 GMT</pubDate><ttl>60</ttl><item><title>Java编程中“为了性能”需做的26件事（转）</title><link>http://www.blogjava.net/chen45257211/articles/391124.html</link><dc:creator>七孑</dc:creator><author>七孑</author><pubDate>Sat, 10 Nov 2012 01:40:00 GMT</pubDate><guid>http://www.blogjava.net/chen45257211/articles/391124.html</guid><wfw:comment>http://www.blogjava.net/chen45257211/comments/391124.html</wfw:comment><comments>http://www.blogjava.net/chen45257211/articles/391124.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/chen45257211/comments/commentRss/391124.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/chen45257211/services/trackbacks/391124.html</trackback:ping><description><![CDATA[<p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>1.尽量在合适的场合使用单例</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">使用单例可以减轻加载的负担，缩短加载的时间，提高加载的效率，但并不是所有地方都适用于单例，简单来说，单例主要适用于以下三个方面：</p><ul style="margin: 0px 0px 1em 20px; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><li style="margin: 0px; padding: 0px; list-style: disc; ">控制资源的使用，通过线程同步来控制资源的并发访问；</li><li style="margin: 0px; padding: 0px; list-style: disc; ">控制实例的产生，以达到节约资源的目的；</li><li style="margin: 0px; padding: 0px; list-style: disc; ">控制数据共享，在不建立直接关联的条件下，让多个不相关的进程或线程之间实现通信。</li></ul><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>2.尽量避免随意使用静态变量</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">要知道，当某个对象被定义为stataic的变量所引用，那么GC通常是不会回收这个对象所占有的内存，如</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">public class A{ static B b = new B（）;} 此时静态变量b的生命周期与A类同步，如果A类不会卸载，那么b对象会常驻内存，直到程序终止。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>3.尽量避免过多过常的创建Java对象</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">尽量避免在经常调用的方法，循环中new对象，由于系统不仅要花费时间来创建对象，而且还要花时间对这些对象进行垃圾回收和处理，在我们可以控制的范围内，最大限度的重用对象，最好能用基本的数据类型或数组来替代对象。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>4.尽量使用final修饰符</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">带有final修饰符的类是不可派生的。在Java核心API中，有许多应用final的例子，例如java.lang.String。为String类指定final防止使用者覆盖length（）方法。另外，如果一个类是final的，则该类所有方法都是final的。Java编译器会寻找机会内联（inline）所有的final方法（这和具体的编译器实现有关）。此举能够使性能平均提高50%。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>5.尽量使用局部变量</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">调用方法时传递的参数以及在调用中创建的临时变量都保存在栈（Stack）中，速度较快。其他变量，如静态变量、实例变量等，都在堆（Heap）中创建，速度较慢。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>6.尽量处理好包装类型和基本类型两者的使用场所</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">虽然包装类型和基本类型在使用过程中是可以相互转换，但它们两者所产生的内存区域是完全不同的，基本类型数据产生和处理都在栈中处理，包装类型是对象，是在堆中产生实例。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">在集合类对象中，有对象方面需要的处理适用包装类型，其他的处理提倡使用基本类型。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>7.慎用synchronized,尽量减小synchronize的方法</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">都知道，实现同步是要很大的系统开销作为代价的，甚至可能造成死锁，所以尽量避免无谓的同步控制。synchronize方法被调用时，直接会把当前对象锁了，在方法执行完之前其他线程无法调用当前对象的其他方法。所以synchronize的方法尽量小，并且应尽量使用方法同步代替代码块同步。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>8.尽量使用StringBuilder和StringBuffer进行字符串连接</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">这个就不多讲了。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>9.尽量不要使用finalize方法</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">实际上，将资源清理放在finalize方法中完成是非常不好的选择，由于GC的工作量很大，尤其是回收Young代内存时，大都会引起应用程序暂停，所以再选择使用finalize方法进行资源清理，会导致GC负担更大，程序运行效率更差。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>10.尽量使用基本数据类型代替对象</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">String str = "hello"; 上面这种方式会创建一个&#8220;hello&#8221;字符串，而且JVM的字符缓存池还会缓存这个字符串；</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">String str = new String（"hello"）； 此时程序除创建字符串外，str所引用的String对象底层还包含一个char[]数组，这个char[]数组依次存放了h,e,l,l,o</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>11.单线程应尽量使用HashMap、ArrayList</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">HashTable、Vector等使用了同步机制，降低了性能。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>12.尽量合理的创建HashMap</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">当你要创建一个比较大的hashMap时，要充分利用另一个构造函数public HashMap（int initialCapacity, float loadFactor）避免HashMap多次进行了hash重构，扩容是一件很耗费性能的事，在默认中initialCapacity只有16，而loadFactor是 0.75，需要多大的容量，你最好能准确的估计你所需要的最佳大小，同样的Hashtable，Vectors也是一样的道理。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">#p#</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>13.尽量减少对变量的重复计算&nbsp;</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">如</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">for（int i=0;i&lt;list.size();i++）应该改为</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">for（int i=0,len=list.size();i&lt;len;i++）并且在循环中应该避免使用复杂的表达式，在循环中，循环条件会被反复计算，如果不使用复杂表达式，而使循环条件值不变的话，程序将会运行的更快。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>14.尽量避免不必要的创建</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">如</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">A a = new A(); if(i==1){list.add(a）;}应该改为</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">if(i==1){ A a = new A();list.add(a);}</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>15.尽量在finally块中释放资源</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">程序中使用到的资源应当被释放，以避免资源泄漏。这最好在finally块中去做。不管程序执行的结果如何，finally块总是会执行的，以确保资源的正确关闭。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>16.尽量使用移位来代替'a/b'的操作</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">&#8220;/&#8221;是一个代价很高的操作，使用移位的操作将会更快和更有效</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">如</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">int num = a / 4; int num = a / 8;应该改为</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">int num = a 》 2; int num = a 》 3;但注意的是使用移位应添加注释，因为移位操作不直观，比较难理解。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>17.尽量使用移位来代替'a*b'的操作</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">同样的，对于'*'操作，使用移位的操作将会更快和更有效</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">如</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">int num = a * 4; int num = a * 8;应该改为</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">int num = a 《 2; int num = a 《 3;</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>18.尽量确定StringBuffer的容量</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">StringBuffer的构造器会创建一个默认大小（通常是16）的字符数组。在使用中，如果超出这个大小，就会重新分配内存，创建一个更大的数组，并将原先的数组复制过来，再丢弃旧的数组。在大多数情况下，你可以在创建StringBuffer的时候指定大小，这样就避免了在容量不够的时候自动增长，以提高性能。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">如：</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">StringBuffer buffer = new StringBuffer（1000）；</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">&nbsp;<strong>19.尽量早释放无用对象的引用</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">大部分时间，方法局部引用变量所引用的对象会随着方法结束而变成垃圾，因此，大部分时候程序无需将局部，引用变量显式设为null.</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">例如：</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">Public void test(){ Object obj = new Object(); &#8230;&#8230; Obj=null; }上面这个就没必要了，随着方法test（）的执行完成，程序中obj引用变量的作用域就结束了。但是如果是改成下面：</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">Public void test(){ Object obj = new Object(); &#8230;&#8230; Obj=null;//执行耗时，耗内存操作；或调用耗时，耗内存的方法 &#8230;&#8230; } 这时候就有必要将obj赋值为null,可以尽早的释放对Object对象的引用。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>20.尽量避免使用二维数组</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">二维数据占用的内存空间比一维数组多得多，大概10倍以上。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>21.尽量避免使用split</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">除非是必须的，否则应该避免使用split，split由于支持正则表达式，所以效率比较低，如果是频繁的几十，几百万的调用将会耗费大量资源，如果确实需要频繁的调用split，可以考虑使用apache的StringUtils.split（string,char），频繁split的可以缓存结果。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>22.ArrayList&amp;LinkedList</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">一个是线性表，一个是链表，一句话，随机查询尽量使用ArrayList，ArrayList优于LinkedList，LinkedList还要移动指针，添加删除的操作LinkedList优于ArrayList，ArrayList还要移动数据，不过这是理论性分析，事实未必如此，重要的是理解好2者的数据结构，对症下药。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>23.尽量使用System.arraycopy（）代替通过来循环复制数组</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">System.arraycopy（）要比通过循环来复制数组快的多。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>24.尽量缓存经常使用的对象</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">尽可能将经常使用的对象进行缓存，可以使用数组，或HashMap的容器来进行缓存，但这种方式可能导致系统占用过多的缓存，性能下降，推荐可以使用一些第三方的开源工具，如EhCache,Oscache进行缓存，他们基本都实现了FIFO/FLU等缓存算法。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>25.尽量避免非常大的内存分配</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">有时候问题不是由当时的堆状态造成的，而是因为分配失败造成的。分配的内存块都必须是连续的，而随着堆越来越满，找到较大的连续块越来越困难。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; "><strong>26.慎用异常</strong></p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">当创建一个异常时，需要收集一个栈跟踪（stack track），这个栈跟踪用于描述异常是在何处创建的。构建这些栈跟踪时需要为运行时栈做一份快照，正是这一部分开销很大。当需要创建一个Exception时，JVM不得不说：先别动，我想就您现在的样子存一份快照，所以暂时停止入栈和出栈操作。栈跟踪不只包含运行时栈中的一两个元素，而是包含这个栈中的每一个元素。</p><p style="margin: 0px 0px 1.5em; padding: 0px; list-style: none; color: #333333; font-family: Helvetica, Tahoma, Arial, sans-serif; line-height: 24px; text-align: left; background-color: #ffffff; ">如果您创建一个Exception，那么就得付出代价。好在捕获异常开销不大，因此可以使用try-catch将核心内容包起来。从技术上讲，您甚至可以随意地抛出异常，而不用花费很大的代价。招致性能损失的并不是throw操作（尽管在没有预先创建异常的情况下就抛出异常是有点不寻常）。真正要花代价的是创建异常。幸运的是，好的编程习惯已教会我们，不应该不管三七二十一就抛出异常。异常是为异常的情况而设计的，使用时也应该牢记这一原则。<br /><br /><br /><br />原文地址：<a href="http://www.csdn.net/article/2012-06-01/2806249">http://www.csdn.net/article/2012-06-01/2806249</a></p><img src ="http://www.blogjava.net/chen45257211/aggbug/391124.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/chen45257211/" target="_blank">七孑</a> 2012-11-10 09:40 <a href="http://www.blogjava.net/chen45257211/articles/391124.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>JSF的commandButton、commandLink、outputLink用法小结&lt;转&gt;</title><link>http://www.blogjava.net/chen45257211/articles/354956.html</link><dc:creator>七孑</dc:creator><author>七孑</author><pubDate>Mon, 25 Jul 2011 02:22:00 GMT</pubDate><guid>http://www.blogjava.net/chen45257211/articles/354956.html</guid><wfw:comment>http://www.blogjava.net/chen45257211/comments/354956.html</wfw:comment><comments>http://www.blogjava.net/chen45257211/articles/354956.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/chen45257211/comments/commentRss/354956.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/chen45257211/services/trackbacks/354956.html</trackback:ping><description><![CDATA[<span class="Apple-style-span" style="font-family: Arial, sans-serif, Helvetica, Tahoma; line-height: 25px; "><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; ">comanndButton和commandLink:&nbsp;<br /><br />h:commandButton&nbsp;<br />可以提交表单，但不能传递参数&nbsp;<br />h:commandLink&nbsp;<br />可以提交表单，又能传递参数，但是以链接的形式展现&nbsp;<br /><br />commandLink必须要在一个from中。&nbsp;<br /><br />comanndButton和commandLink要在一个from中才能提交表单内容。&nbsp;<br /><br />会发送回本页面，并触发JSF的生命周期，比如：重建组件树、应用请求值等，因此，允许设置actionListener和action属性，这样他们可以很轻松完成强大的功能。&nbsp;</p><div class="dp-highlighter" id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; font-size: 12px; background-color: transparent; width: 694px; overflow-x: auto; overflow-y: auto; margin-left: 9px; padding-top: 1px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; "><div class="bar"><div class="tools" style="padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; text-align: left; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: black; font-weight: bold; ">Java代码&nbsp;<embed src="http://gaolixu.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=%3Ch%3AcommandButton%20actionListener%3D%22%23%7BactionListener.check%7D%22%3B%20value%3D%22%E9%80%81%E5%87%BA%22%3B%20action%3D%22%23%7Buser.check%7D%22%20%2F%3E" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="http://gaolixu.iteye.com/blog/310477" title="收藏这段代码" style="color: #108ac6; text-decoration: underline; "><img class="star" src="http://gaolixu.iteye.com/images/icon_star.png" alt="收藏代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></a></div></div><ol start="1" class="dp-j" style="font-size: 1em; line-height: 1.4em; margin-top: 0px; margin-right: 0px; margin-bottom: 1px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #d1d7dc; border-right-color: #d1d7dc; border-bottom-color: #d1d7dc; border-left-color: #d1d7dc; list-style-type: decimal; list-style-position: initial; list-style-image: initial; background-color: #ffffff; color: #2b91af; "><li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; "><span style="color: black; "><span style="color: black; ">&lt;h:commandButton&nbsp;actionListener=</span><span class="string" style="color: blue; ">"#{actionListener.check}"</span><span style="color: black; ">;&nbsp;value=</span><span class="string" style="color: blue; ">"送出"</span><span style="color: black; ">;&nbsp;action=</span><span class="string" style="color: blue; ">"#{user.check}"</span><span style="color: black; ">&nbsp;/&gt;&nbsp;&nbsp;</span></span></li></ol></div><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><br /><br />actionListener 响应的一个事件,当然这个和js中的事件不一样.actionListener="#{actionListener.check}"响应的是服务器端的事件actionListener类的check方法.&nbsp;<br /><br />action 用过STRUTS的都知道,提交后执行的方法.当然在SRTUTS中action="URL"，而这里是一个user类的check方法.&nbsp;<br /><br />不足在于：如果重建组件树的成本比较高（比如：当前页面显示一个数据表格），而这些组件对于即将跳转到的页面没什么用时，就应该考虑使用outputLink了。&nbsp;</p><div class="dp-highlighter" id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; font-size: 12px; background-color: transparent; width: 694px; overflow-x: auto; overflow-y: auto; margin-left: 9px; padding-top: 1px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; "><div class="bar"><div class="tools" style="padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; text-align: left; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: black; font-weight: bold; ">Java代码&nbsp;<embed src="http://gaolixu.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=%3Ch%3AcommandLink%20action%3D%22%23%7Buser.testLink%7D%22%3E%3Cf%3Averbatim%3E%E5%A2%9E%E5%8A%A0%3C%2Ff%3Averbatim%3E%3C%2Fh%3AcommandLink%3E" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="http://gaolixu.iteye.com/blog/310477" title="收藏这段代码" style="color: #108ac6; text-decoration: underline; "><img class="star" src="http://gaolixu.iteye.com/images/icon_star.png" alt="收藏代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></a></div></div><ol start="1" class="dp-j" style="font-size: 1em; line-height: 1.4em; margin-top: 0px; margin-right: 0px; margin-bottom: 1px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #d1d7dc; border-right-color: #d1d7dc; border-bottom-color: #d1d7dc; border-left-color: #d1d7dc; list-style-type: decimal; list-style-position: initial; list-style-image: initial; background-color: #ffffff; color: #2b91af; "><li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; "><span style="color: black; "><span style="color: black; ">&lt;h:commandLink&nbsp;action=</span><span class="string" style="color: blue; ">"#{user.testLink}"</span><span style="color: black; ">&gt;&lt;f:verbatim&gt;增加&lt;/f:verbatim&gt;&lt;/h:commandLink&gt;&nbsp;&nbsp;</span></span></li></ol></div><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><br /><br />另外：如果需要传递参数&lt;f:param.../&gt;，使用commandLink&nbsp;<br />在action或actionListener中获取&lt;f:param.../&gt;：</p><div class="dp-highlighter" id="" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; font-size: 12px; background-color: transparent; width: 694px; overflow-x: auto; overflow-y: auto; margin-left: 9px; padding-top: 1px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; "><div class="bar"><div class="tools" style="padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; text-align: left; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: black; font-weight: bold; ">Java代码&nbsp;<embed src="http://gaolixu.iteye.com/javascripts/syntaxhighlighter/clipboard_new.swf" width="14" height="15" flashvars="clipboard=FacesContext%20ctx%20%3D%20FacesContext.getCurrentInstance()%3B%20%0Aint%20productId%20%3D%20Integer.parseInt((String)ctx.getExternalContext().getRequestParameterMap().get(%22productId%22))%3B%20" quality="high" allowscriptaccess="always" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">&nbsp;<a href="http://gaolixu.iteye.com/blog/310477" title="收藏这段代码" style="color: #108ac6; text-decoration: underline; "><img class="star" src="http://gaolixu.iteye.com/images/icon_star.png" alt="收藏代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></a></div></div><ol start="1" class="dp-j" style="font-size: 1em; line-height: 1.4em; margin-top: 0px; margin-right: 0px; margin-bottom: 1px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #d1d7dc; border-right-color: #d1d7dc; border-bottom-color: #d1d7dc; border-left-color: #d1d7dc; list-style-type: decimal; list-style-position: initial; list-style-image: initial; background-color: #ffffff; color: #2b91af; "><li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; "><span style="color: black; "><span style="color: black; ">FacesContext&nbsp;ctx&nbsp;=&nbsp;FacesContext.getCurrentInstance();&nbsp;&nbsp;&nbsp;</span></span></li><li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; "><span style="color: black; "><span class="keyword" style="color: #7f0055; font-weight: bold; ">int</span><span style="color: black; ">&nbsp;productId&nbsp;=&nbsp;Integer.parseInt((String)ctx.getExternalContext().getRequestParameterMap().get(</span><span class="string" style="color: blue; ">"productId"</span><span style="color: black; ">));&nbsp;&nbsp;&nbsp;</span></span></li></ol></div><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><br /><br />outputLink&nbsp;<br /><br />比起前两个来说，他相当的轻量级了。他会直接产生一个&lt;a href=""&gt;&lt;/a&gt;链接，跳转到相应的页面，因此没有进入JSF生命周期的额外开销，跟我们直接写一个html的链接没什么区别。&nbsp;<br /><br />如果需要传递参数，嵌入&lt;f:param name="a" value="b"/&gt;就可以了，当然这里的value可以用表达式来表示，比如value="#{param.productId}"，用起来是相当方便的。</p><div class="dp-highlighter" style="font-family: Monaco, 'DejaVu Sans Mono', 'Bitstream Vera Sans Mono', Consolas, 'Courier New', monospace; font-size: 12px; background-color: transparent; width: 694px; overflow-x: auto; overflow-y: auto; margin-left: 9px; padding-top: 1px; padding-right: 1px; padding-bottom: 1px; padding-left: 1px; word-break: break-all; word-wrap: break-word; "><div class="bar"><div class="tools" style="padding-top: 3px; padding-right: 3px; padding-bottom: 3px; padding-left: 3px; text-align: left; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; color: black; font-weight: bold; ">Java代码&nbsp;<a href="http://origin100.iteye.com/blog/267167#" title="复制代码" style="color: #108ac6; text-decoration: underline; "><img src="http://origin100.iteye.com/images/icon_copy.gif" alt="复制代码" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px; border-style: initial; border-color: initial; " /></a></div></div><ol class="dp-j" style="font-size: 1em; line-height: 1.4em; margin-top: 0px; margin-right: 0px; margin-bottom: 1px; margin-left: 0px; padding-top: 2px; padding-right: 0px; padding-bottom: 2px; padding-left: 0px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: #d1d7dc; border-right-color: #d1d7dc; border-bottom-color: #d1d7dc; border-left-color: #d1d7dc; list-style-type: decimal; list-style-position: initial; list-style-image: initial; background-color: #ffffff; color: #2b91af; "><li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; "><span style="color: black; "><span style="color: black; ">&lt;h:outputLink&nbsp;value=</span><span class="string" style="color: blue; "><span style="color: #0000ff; ">"productEdit.faces"</span></span><span style="color: black; ">&gt;&nbsp; &nbsp;&nbsp;</span></span></li><li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; "><span style="color: black; ">&lt;h:outputText&nbsp;value=</span><span class="string" style="color: blue; "><span style="color: #0000ff; ">"编辑"</span></span><span style="color: black; ">/&gt;&nbsp; &nbsp;&nbsp;</span></li><li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; "><span style="color: black; ">&lt;f:param&nbsp;name=</span><span class="string" style="color: blue; "><span style="color: #0000ff; ">"productId"</span></span><span style="color: black; ">&nbsp;value=</span><span class="string" style="color: blue; "><span style="color: #0000ff; ">"#{item.productId}"</span></span><span style="color: black; ">/&gt;&nbsp; &nbsp;&nbsp;</span></li><li style="font-size: 1em; margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 38px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 10px; border-left-width: 1px; border-left-style: solid; border-left-color: #d1d7dc; background-color: #fafafa; line-height: 18px; "><span style="color: black; ">&lt;/h:outputLink&gt; &nbsp;</span></li></ol></div><p style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; "><br /><br />其效果为&lt;a href="..jsf?productId=..."&gt;&lt;/a&gt;&nbsp;<br /><br />h:commandButton、h:commandLink 和h:outputLink的差别在于：h:outputLink没有进入JSF的生命周期,而h:commandLink和h:commandButton都要进入JSF的生命周期.</p></span><img src ="http://www.blogjava.net/chen45257211/aggbug/354956.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/chen45257211/" target="_blank">七孑</a> 2011-07-25 10:22 <a href="http://www.blogjava.net/chen45257211/articles/354956.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>编写 JSF 自定义复合组件的技巧和窍门</title><link>http://www.blogjava.net/chen45257211/articles/354763.html</link><dc:creator>七孑</dc:creator><author>七孑</author><pubDate>Thu, 21 Jul 2011 02:56:00 GMT</pubDate><guid>http://www.blogjava.net/chen45257211/articles/354763.html</guid><wfw:comment>http://www.blogjava.net/chen45257211/comments/354763.html</wfw:comment><comments>http://www.blogjava.net/chen45257211/articles/354763.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/chen45257211/comments/commentRss/354763.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/chen45257211/services/trackbacks/354763.html</trackback:ping><description><![CDATA[&nbsp;&nbsp;&nbsp;&nbsp; 摘要: JavaServer Faces（JSF）提供可扩展的组件模型，开发人员可以创建可重用的组件，使用这些自定义组件提高开发效率和降低开发成本。虽然对于定制和重用而言 JSF 的组件模型非常强大，但是开发人员普遍认为开发 JSF 自定义组件并不容易，因为通常至少需要熟悉 JSF encode/decode 和 state holder 的内部机制并覆盖相应的方法（参考&#8220;怀疑论者的 JSF:...&nbsp;&nbsp;<a href='http://www.blogjava.net/chen45257211/articles/354763.html'>阅读全文</a><img src ="http://www.blogjava.net/chen45257211/aggbug/354763.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/chen45257211/" target="_blank">七孑</a> 2011-07-21 10:56 <a href="http://www.blogjava.net/chen45257211/articles/354763.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item><item><title>Js通用分页方法（转载）</title><link>http://www.blogjava.net/chen45257211/articles/354367.html</link><dc:creator>七孑</dc:creator><author>七孑</author><pubDate>Fri, 15 Jul 2011 01:12:00 GMT</pubDate><guid>http://www.blogjava.net/chen45257211/articles/354367.html</guid><wfw:comment>http://www.blogjava.net/chen45257211/comments/354367.html</wfw:comment><comments>http://www.blogjava.net/chen45257211/articles/354367.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.blogjava.net/chen45257211/comments/commentRss/354367.html</wfw:commentRss><trackback:ping>http://www.blogjava.net/chen45257211/services/trackbacks/354367.html</trackback:ping><description><![CDATA[<div><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; "><a href="http://java.chinaitlab.com/" target="_blank" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-size: 14px; font-family: 宋体; text-decoration: none; color: #0000ff; line-height: 22px; ">Java</a>代码</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　<a href="http://java.chinaitlab.com/" target="_blank" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-size: 14px; font-family: 宋体; text-decoration: none; color: #0000ff; line-height: 22px; ">Java</a>后台：</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　public class PageEntity {</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　private int page;//当前页码</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　private int total;//一共多少条</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　private int totalPage;//一共多少页</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　private int firstResult;//当前首记录</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　private int lastResult;//当前尾记录</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　private int pageSize = 10; //每页几条数据</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　public int getPage() {</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　return page;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　public void setPage(int page) {</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　this.page = page;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　public int getTotal() {</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　return total;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　public void setTotal(int total) {</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　this.total = total;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　public int getTotalPage() {</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　return totalPage;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　public void setTotalPage(int totalPage) {</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　this.totalPage = totalPage;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　public int getFirstResult() {</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　return firstResult;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　public void setFirstResult(int firstResult) {</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　this.firstResult = firstResult;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　public int getLastResult() {</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　return lastResult;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　public void setLastResult(int lastResult) {</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　this.lastResult = lastResult;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　public int getPageSize() {</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　return pageSize;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　public void setPageSize(int pageSize) {</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　this.pageSize = pageSize;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　//这里注意page必须在firstResult之前计算</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　public PageEntity getPageEntity(HttpServletRequest request,GenericIface entityDao,String where) {</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　String strPage = request.getParameter("page") == null ? "1":request.getParameter("page");</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　int page = Integer.parseInt(strPage);//要查询的页数</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　int total = entityDao.findNumber(where);//当前数据库总记录数</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　int pageSize = 20;//每页要显示的记录</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　int firstResult = 0;//起始记录</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　int lastResult = 0;//末尾记录</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　int totalPage = 0;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　if(total &gt; 0){</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　totalPage = (total + pageSize -1)/pageSize;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　if(page == -1){</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　page = totalPage;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}else if(page &lt; 1){</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　page = 1;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}else if(page &gt; totalPage){</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　page = totalPage;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　firstResult = (page-1)*pageSize;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　lastResult = firstResult + pageSize;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}else{</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　//如果没有记录&nbsp; 则清零&nbsp; 因为页面有加1处理&nbsp; 所以firstResult=-1</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　page = 0;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　firstResult = -1;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　lastResult = 0;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　PageEntity pageEntity = new PageEntity();</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　pageEntity.setFirstResult(firstResult);</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　pageEntity.setLastResult(lastResult);</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　pageEntity.setPage(page);</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　pageEntity.setTotal(total);</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　pageEntity.setTotalPage(totalPage);</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　pageEntity.setPageSize(pageSize);</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　return pageEntity;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　前台js：</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　//分页程序 1表示第一页 2表示上一页 3表示下一页 4表示尾页</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　function changePage(e){</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　var page = document.getElementById("nowPage") == null ? "1":document.getElementById("nowPage").value;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　switch(e){</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　case 1:page=1;break;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　case 2:page--;break;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　case 3:page++;break;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　case 4:page=-1;break;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　var url = "action.do?page=" + page + getCondition(page);</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　document.location.href = url;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　function forwardPage(){</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　var page = document.getElementById("nowPage") == null ? "1":document.getElementById("nowPage").value;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　var exp = /^(0|[1-9]\d*)$/;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　var result = page.match(exp);</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　if(result != null){</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　var url = " action.do?page=" + page + getCondition(page);</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　document.location.href = url;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}else{</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　alert("请输入合法数字!");</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　}</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　Jsp页面:</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　&lt;div id="page" class="w_872 mrg_s10"&gt;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　&lt;label&gt;&lt;input name="" type="button" class="tnleft1" onClick="changePage(1)" title="首页" /&gt;&lt;/label&gt;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　&lt;label&gt;&lt;input name="" type="button" class="tnleft2" onClick="changePage(2)" title="上一页" /&gt;&lt;/label&gt;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　&lt;label class="fo"&gt;页码 &lt;input type="text" class="input5" id="nowPage" value="${pageEntity.page }" onblur="forwardPage()"&gt; of &lt;span&gt;${pageEntity.totalPage }&lt;/span&gt;&lt;/label&gt;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　&lt;label&gt;&lt;input name="" type="button" class="tnrig2" onClick="changePage(3)" title="下一页" /&gt;&lt;/label&gt;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　&lt;label&gt;&lt;input name="" type="button" class="tnrig1" onClick="changePage(4)" title="尾页" /&gt;&lt;/label&gt;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　&lt;label class="rig"&gt;&lt;span&gt;${pageEntity.firstResult+1 }&lt;/span&gt; to &lt;span&gt;${pageEntity.lastResult }&lt;/span&gt;，共&lt;span&gt;${pageEntity.total }&lt;/span&gt;条&lt;/label&gt;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　&lt;/div&gt;</p><p style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; ">　　页面效果展示:</p><p align="center" style="margin-top: 10px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-family: 宋体; font-size: 14px; line-height: 25px; "><a href="http://java.chinaitlab.com/UploadFiles_8734/201007/2010071410051533.jpg" target="_blank" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-size: 14px; font-family: 宋体; text-decoration: none; color: #333333; "><img height="24" src="http://java.chinaitlab.com/UploadFiles_8734/201007/2010071410051533.jpg" width="600" border="0" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; "  alt="" /><br />文章出处 ：<a href="http://java.chinaitlab.com/ServletJsp/816055.html">http://java.chinaitlab.com/ServletJsp/816055.html</a><span class="Apple-style-span" style="color: #000000; "><a href="http://java.chinaitlab.com/UploadFiles_8734/201007/2010071410051533.jpg" target="_blank" style="margin-top: 0px; margin-right: 0px; margin-bottom: 0px; margin-left: 0px; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; word-wrap: break-word; font-size: 14px; font-family: 宋体; text-decoration: none; color: #333333; ">&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;</a></span><br /><br /><br /></a></p></div><img src ="http://www.blogjava.net/chen45257211/aggbug/354367.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.blogjava.net/chen45257211/" target="_blank">七孑</a> 2011-07-15 09:12 <a href="http://www.blogjava.net/chen45257211/articles/354367.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>