java分页导航生成工具

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

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

看看效果图

/** * 分页导航生成工具类 *  * @author shadow *  */public class NavUtils {private static final int show_ = 10;private static final int sep_ = show_ / 2;private static final int split_ = sep_ / 2;public static String build(int offset, int limit, int total) {int count = total % limit == 0 ? total / limit : total / limit + 1;StringBuffer buffer = new StringBuffer();buffer.append("<span class='count_result'>共 " + count + " 页 " + total + " 条记录 </span>");// 判断是否显示上页if (offset > 1) {int prev = offset - 1;buffer.append(getNormalPart("上页", prev, limit));}// 页数不超过限制的情况if (count <= show_)buffer.append(getPart(1, count, offset, limit));// 页数大于限制的情况if (count > show_) {if (offset <= sep_) {buffer.append(getPart(1, sep_ + split_, offset, limit));buffer.append(getEllipsis("...")).append(getNormalPart(String.valueOf(count), count, limit));} else if (offset > (count - sep_)) {buffer.append(getNormalPart(String.valueOf(1), 1, limit)).append(getEllipsis("..."));buffer.append(getPart(count - sep_ - 1, count, offset, limit));} else {buffer.append(getNormalPart(String.valueOf(1), 1, limit)).append(getEllipsis("..."));buffer.append(getPart(offset - split_ - 1, offset + split_ + 1, offset, limit));buffer.append(getEllipsis("...")).append(getNormalPart(String.valueOf(count), count, limit));}}// 判断是否显示下页if (offset < count) {int next = offset + 1;buffer.append(getNormalPart("下页", next, limit));}return buffer.toString();}// 一般按钮private static StringBuffer getNormalPart(String content, int offset, int limit) {StringBuffer buffer = new StringBuffer();buffer.append("<a href='javascript:void(0);' ").append("onclick='pageClick(").append(offset).append(",").append(limit).append(");'").append("'>").append(content).append("</a>");return buffer;}// 拼接中间部分private static StringBuffer getPart(int begin, int end, int offset, int limit) {StringBuffer buffer = new StringBuffer();for (int i = begin; i <= end; i++) {if (offset == i)buffer.append(getSelectedPart(String.valueOf(i), i));elsebuffer.append(getNormalPart(String.valueOf(i), i, limit));}return buffer;}// 选中按钮private static StringBuffer getSelectedPart(String content, Integer value) {StringBuffer buffer = new StringBuffer();buffer.append("<a href='javascript:void(0);'").append("' class='current'>").append(content).append("</a>");return buffer;}// 省略部分private static StringBuffer getEllipsis(String content) {StringBuffer buffer = new StringBuffer();buffer.append("<a href='javascript:void(0);'>").append(content).append("</a>");return buffer;}

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

/** * 分页参数对象 *  * @author shadow * @email 124010356@qq.com * @create 2012.04.28 */@SuppressWarnings("serial")public class Page<T> implements Serializable {private Integer offset = 1; // 索引值private Integer limit = 10; // 显示数private Integer total = 0; // 记录数private Integer index;private String navigation; // 分页导航private List<T> list; // 分页数据public Integer getLimit() {return limit;}public void setLimit(Integer limit) {this.limit = limit;}public Integer getCount() {return total % limit == 0 ? total / limit : total / limit + 1;}public Integer getTotal() {return total;}public void setTotal(Integer total) {this.total = total;}public Integer getOffset() {int count = getCount();if (offset > count)offset = count;if (offset < 1)offset = 1;return offset;}public void setIndex(Integer index) {this.index = index;}public Integer getIndex() {if (offset < 1)offset = 1;index = (offset - 1) * limit;return index;}public void setOffset(Integer offset) {this.offset = offset;}public String getNavigation() {return navigation;}public void setNavigation(String navigation) {this.navigation = navigation;}public List<T> getList() {return list;}public void setList(List<T> list) {this.list = list;}}
@Overridepublic Result page(AdminUserVo vo) {Result result = getInstance();try {vo.setTotal(dao.page4total(vo));List<AdminUser> models = dao.page4list(vo);vo.setList(models);vo.setNavigation(NavUtils.build(vo.getOffset(), vo.getLimit(), vo.getCount()));result.setSuccess(true).setValue(vo);} catch (Exception e) {result.setException(new MVCException(e));}return result;}

世上没有绝望的处境,只有对处境绝望的人。

java分页导航生成工具

相关文章:

你感兴趣的文章:

标签云: