Java标签分页实现

来源:互联网 发布:mac怎么玩穿越火线 编辑:程序博客网 时间:2024/05/02 04:58

Java实现标签分页

最近为了开发一个网站,里面要用分页功能,但是之前很少自己写分页标签,又不想用现成框架。所以自己参考了些资料,写了个分页例子测试了一下。

代码主要分为三个类:

  1. PageTag 分页标签类
  2. Page 分页bean
  3. Constant 设置常量

Page代码:

Java代码 复制代码 收藏代码
  1. /**
  2. *
  3. * @author byyang
  4. *
  5. */
  6. public class Page
  7. {
  8. private int current =0; //当前页,默认为第一页
  9. private int size; //记录总数
  10. private int length;//每页的长度
  11. private String url; //访问路径
  12. public Page(int offset,int size, int length) {
  13. super();
  14. this.current = offset;
  15. this.size = size;
  16. this.length = length;
  17. }
  18. /**
  19. * 获取总页数
  20. * @return
  21. */
  22. public int pageCount(){
  23. int pagecount = 0;
  24. if(this.size % this.length == 0){
  25. pagecount = this.size / this.length;
  26. }else{
  27. pagecount = this.size / this.length +1;
  28. }
  29. return pagecount;
  30. }
  31. //最后一页开始条数
  32. public int lastPageOffset(){
  33. return this.size - lastPageSize();
  34. }
  35. //最后一页页大小
  36. public int lastPageSize(){
  37. int lastpagesize = 0;
  38. if(this.size % this.length == 0){
  39. lastpagesize = this.length;
  40. }else{
  41. lastpagesize = this.size % this.length;
  42. }
  43. return lastpagesize;
  44. }
  45. //获取起始页
  46. public int getOffset() {
  47. return current;
  48. }
  49. //总记录数
  50. public int getSize() {
  51. return size;
  52. }
  53. //每页大小
  54. public int getLength() {
  55. return length;
  56. }
  57. //获取访问路径
  58. public String getUrl() {
  59. return url;
  60. }
  61. //上一页
  62. public int getLastOffset(){
  63. int offset = this.getOffset() -this.getLength();
  64. if(offset > 0){
  65. return offset;
  66. }else{
  67. return 0;
  68. }
  69. }
  70. //下一页
  71. public int getNextOffset(){
  72. int offset = this.getOffset() +this.getLength();
  73. if(offset > this.getSize()){
  74. return getLastOffset();
  75. }else{
  76. return offset;
  77. }
  78. }
  79. public String getPageNavigation(){
  80. String pageNavigation = "";
  81. return pageNavigation;
  82. }
  83. public void setOffset(int offset) {
  84. this.current = offset;
  85. }
  86. public void setSize(int size) {
  87. this.size = size;
  88. }
  89. public void setLength(int length) {
  90. this.length = length;
  91. }
  92. public void setUrl(String url) {
  93. this.url = url;
  94. }
  95. }