2010年8月31日星期二

一个分页工具类

  1. import java.util.List;
  2. public class SplitPage {
  3. private List totalResult = null; //所有的数据
  4. private List pageResult = null; //每页的数据
  5. private int pageSize = 10; // 每页显示的行数
  6. private int totalRow; //所有数据的行数
  7. private int totalPage; //总共的页数
  8. private int thisPage; //当前页面
  9. public SplitPage(List list) {
  10. super();
  11. this.totalResult = list;
  12. this.totalRow=list.size();
  13. this.thisPage=1;
  14. int page=this.totalRow/this.pageSize;
  15. if(this.totalRow%this.pageSize!=0)
  16. this.totalPage=page+1;
  17. else
  18. this.totalPage=page;
  19. }
  20. public SplitPage(List list,int num) {
  21. super();
  22. this.pageSize = num;
  23. this.totalResult = list;
  24. this.totalRow=list.size();
  25. this.thisPage=1;
  26. int page=this.totalRow/this.pageSize;
  27. if(this.totalRow%this.pageSize!=0)
  28. this.totalPage=page+1;
  29. else
  30. this.totalPage=page;
  31. }
  32. public List getFirstPage(){
  33. thisPage = 1;
  34. if(totalRow <>
  35. pageResult = totalResult;
  36. else
  37. pageResult = totalResult.subList(0, pageSize);
  38. return pageResult;
  39. }
  40. public List getLastPage(){
  41. thisPage = totalPage;
  42. if(totalRow <>
  43. pageResult = totalResult;
  44. else
  45. pageResult = totalResult.subList((totalPage-1)*pageSize,totalRow);
  46. return pageResult;
  47. }
  48. public List gotoPage(int go){
  49. thisPage = go;
  50. if(go == 1)
  51. return getFirstPage();
  52. else if(go == totalPage)
  53. return getLastPage();
  54. else{
  55. pageResult = totalResult.subList((go-1)*pageSize, go*pageSize);
  56. return pageResult;
  57. }
  58. }
  59. public List goNextPage(int current){
  60. current++;
  61. return gotoPage(current);
  62. }
  63. public int getPageSize() {
  64. return pageSize;
  65. }
  66. public void setPageSize(int pageSize) {
  67. this.pageSize = pageSize;
  68. }
  69. public List goPrePage(int current){
  70. current--;
  71. return gotoPage(current);
  72. }
  73. public int getTotalPage() {
  74. return totalPage;
  75. }
  76. public void setTotalPage(int totalPage) {
  77. this.totalPage = totalPage;
  78. }
  79. public int getThisPage() {
  80. return thisPage;
  81. }
  82. public void setThisPage(int thisPage) {
  83. this.thisPage = thisPage;
  84. }
  85. }