分享

java分页导航生成工具

 根root 2017-01-18

网上流传的一些Java生成分页导航工具都是有那么些bug,抽空自己写了个,如果发现问题请联系我;

顺便利用这种思路写了个java分页导航生成工具


看看效果图




  1. /** 
  2.  * 分页导航生成工具类 
  3.  *  
  4.  * @author shadow 
  5.  *  
  6.  */  
  7. public class NavUtils {  
  8.   
  9.     private static final int show_ = 10;  
  10.   
  11.     private static final int sep_ = show_ / 2;  
  12.   
  13.     private static final int split_ = sep_ / 2;  
  14.   
  15.     public static String build(int offset, int limit, int total) {  
  16.   
  17.         int count = total % limit == 0 ? total / limit : total / limit + 1;  
  18.   
  19.         StringBuffer buffer = new StringBuffer();  
  20.   
  21.         buffer.append("<span class='count_result'>共 " + count + " 页 " + total + " 条记录 </span>");  
  22.   
  23.         // 判断是否显示上页  
  24.         if (offset > 1) {  
  25.             int prev = offset - 1;  
  26.             buffer.append(getNormalPart("上页", prev, limit));  
  27.         }  
  28.   
  29.         // 页数不超过限制的情况  
  30.         if (count <= show_)  
  31.             buffer.append(getPart(1, count, offset, limit));  
  32.   
  33.         // 页数大于限制的情况  
  34.   
  35.         if (count > show_) {  
  36.             if (offset <= sep_) {  
  37.                 buffer.append(getPart(1, sep_ + split_, offset, limit));  
  38.                 buffer.append(getEllipsis("...")).append(getNormalPart(String.valueOf(count), count, limit));  
  39.             } else if (offset > (count - sep_)) {  
  40.                 buffer.append(getNormalPart(String.valueOf(1), 1, limit)).append(getEllipsis("..."));  
  41.                 buffer.append(getPart(count - sep_ - 1, count, offset, limit));  
  42.             } else {  
  43.                 buffer.append(getNormalPart(String.valueOf(1), 1, limit)).append(getEllipsis("..."));  
  44.                 buffer.append(getPart(offset - split_ - 1, offset + split_ + 1, offset, limit));  
  45.                 buffer.append(getEllipsis("...")).append(getNormalPart(String.valueOf(count), count, limit));  
  46.             }  
  47.         }  
  48.   
  49.         // 判断是否显示下页  
  50.         if (offset < count) {  
  51.             int next = offset + 1;  
  52.             buffer.append(getNormalPart("下页", next, limit));  
  53.         }  
  54.   
  55.         return buffer.toString();  
  56.   
  57.     }  
  58.   
  59.     // 一般按钮  
  60.     private static StringBuffer getNormalPart(String content, int offset, int limit) {  
  61.         StringBuffer buffer = new StringBuffer();  
  62.         buffer.append("<a href='javascript:void(0);' ").append("onclick='pageClick(").append(offset).append(",").append(limit).append(  
  63.                 ");'").append("'>").append(content).append("</a>");  
  64.         return buffer;  
  65.     }  
  66.   
  67.     // 拼接中间部分  
  68.     private static StringBuffer getPart(int begin, int end, int offset, int limit) {  
  69.         StringBuffer buffer = new StringBuffer();  
  70.         for (int i = begin; i <= end; i++) {  
  71.             if (offset == i)  
  72.                 buffer.append(getSelectedPart(String.valueOf(i), i));  
  73.             else  
  74.                 buffer.append(getNormalPart(String.valueOf(i), i, limit));  
  75.         }  
  76.         return buffer;  
  77.     }  
  78.   
  79.     // 选中按钮  
  80.     private static StringBuffer getSelectedPart(String content, Integer value) {  
  81.         StringBuffer buffer = new StringBuffer();  
  82.         buffer.append("<a href='javascript:void(0);'").append("' class='current'>").append(content).append("</a>");  
  83.         return buffer;  
  84.     }  
  85.   
  86.     // 省略部分  
  87.     private static StringBuffer getEllipsis(String content) {  
  88.         StringBuffer buffer = new StringBuffer();  
  89.         buffer.append("<a href='javascript:void(0);'>").append(content).append("</a>");  
  90.         return buffer;  
  91.     }  


怎么使用呢?看我下面的使用方法(参数vo是继承Page类)

  1. /** 
  2.  * 分页参数对象 
  3.  *  
  4.  * @author shadow 
  5.  * @email 124010356@qq.com 
  6.  * @create 2012.04.28 
  7.  */  
  8. @SuppressWarnings("serial")  
  9. public class Page<T> implements Serializable {  
  10.   
  11.     private Integer offset = 1; // 索引值  
  12.     private Integer limit = 10; // 显示数  
  13.     private Integer total = 0; // 记录数  
  14.   
  15.     private Integer index;  
  16.   
  17.     private String navigation; // 分页导航  
  18.   
  19.     private List<T> list; // 分页数据  
  20.   
  21.     public Integer getLimit() {  
  22.         return limit;  
  23.     }  
  24.   
  25.     public void setLimit(Integer limit) {  
  26.         this.limit = limit;  
  27.     }  
  28.   
  29.     public Integer getCount() {  
  30.         return total % limit == 0 ? total / limit : total / limit + 1;  
  31.     }  
  32.   
  33.     public Integer getTotal() {  
  34.         return total;  
  35.     }  
  36.   
  37.     public void setTotal(Integer total) {  
  38.         this.total = total;  
  39.     }  
  40.   
  41.     public Integer getOffset() {  
  42.         int count = getCount();  
  43.         if (offset > count)  
  44.             offset = count;  
  45.         if (offset < 1)  
  46.             offset = 1;  
  47.         return offset;  
  48.     }  
  49.   
  50.     public void setIndex(Integer index) {  
  51.         this.index = index;  
  52.     }  
  53.   
  54.     public Integer getIndex() {  
  55.         if (offset < 1)  
  56.             offset = 1;  
  57.         index = (offset - 1) * limit;  
  58.         return index;  
  59.     }  
  60.   
  61.     public void setOffset(Integer offset) {  
  62.         this.offset = offset;  
  63.     }  
  64.   
  65.     public String getNavigation() {  
  66.         return navigation;  
  67.     }  
  68.   
  69.     public void setNavigation(String navigation) {  
  70.         this.navigation = navigation;  
  71.     }  
  72.   
  73.     public List<T> getList() {  
  74.         return list;  
  75.     }  
  76.   
  77.     public void setList(List<T> list) {  
  78.         this.list = list;  
  79.     }  
  80.   
  81. }  


  1. @Override  
  2.     public Result page(AdminUserVo vo) {  
  3.         Result result = getInstance();  
  4.         try {  
  5.             vo.setTotal(dao.page4total(vo));  
  6.             List<AdminUser> models = dao.page4list(vo);  
  7.             vo.setList(models);  
  8.             vo.setNavigation(NavUtils.build(vo.getOffset(), vo.getLimit(), vo.getCount()));  
  9.             result.setSuccess(true).setValue(vo);  
  10.         } catch (Exception e) {  
  11.             result.setException(new MVCException(e));  
  12.         }  
  13.         return result;  
  14.     }  


最后页面增加一个div存放page.navigation这个字符变成html标签即可,以及在页面增加pageClick的js函数作为回调响应请求,相信大家都懂了吧...thx


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多