ITeye博客

SSH、JavaWeb的分页代码,封装好、直接调用,简单实用、分页API【关于之前的分页代码完善的更改】

Etwo · 2012-07-12 17:21 · JavaEE技术 · 编程语言 · Markdown

关于之前实现的

SSH、JavaWeb的分页代码,封装好、直接调用,简单实用、分页API

分页代码的一个完善,当分页出现0或者负数或者大于计算出来的页数的时候,可能出现数据不能正常显示的问题。

action中仅用以下代码就能调用,servlet中可效仿此调用

		this.paging = new Paging();

		//设置当前页码
		this.paging.setCurPage(this.cur_page);
		
		//把得到总共显示的数据记录设置在分页总纪录中
		this.paging.setRowCount(userMagr.count());
		
		//设置每页显示的数据记录
		this.paging.setPageRowCount(3);
		
		//保存分页工具条
		request.setAttribute("toolBar", this.paging.getToolBar("user_list.do"));
		
		//得到每页显示数据
		this.users = userMagr.list(paging);

 

在此,特提出完善后的代码

package net.etwo.util;

public class Paging {

	/**
	 * 分页
	 * @说明:本分页采用直接获取每一页的数据
	 * @author Etwo
	 */
	
	/*
	 * 分页要素:
	 * 1、首页				first
	 * 2、最后一页			last
	 * 3、页总数				pageCount
	 * 4、总共显示的数据记录	rowCount
	 * 5、每页显示的数据记录	pageRowCount
	 * 6、当前页				curPage	
	 * 7、定义每一页的开始	start
	 * 8、定义每一页的结束	end
	 */
	
	/**
	 * 1、首页				first
	 */
	private int first;
	
	/**
	 * 2、最后一页			last
	 */
	private int last;
	
	/**
	 * 3、页总数				pageCount
	 */
	private int pageCount;
	
	/**
	 * 4、总共显示的数据记录	rowCount
	 */
	private long rowCount;
	
	/**
	 * 5、每页显示的数据记录	pageRowCount
	 */
	private int pageRowCount;
	
	/**
	 * 6、当前页				curPage	
	 */
	private int curPage;
	
	/**
	 * 7、定义每一页的开始	start
	 */
	private int start;
	
	/**
	 * 8、定义每一页的结束	end
	 */
	private int end;


	/**
	 * 计算并得出页总数
	 * @return
	 */
	public int getPageCount() {
		this.pageCount = (int)Math.ceil((double)this.getRowCount() / (double)this.getPageRowCount());
		return pageCount;
	}

	/**
	 * 得到总共显示的数据记录
	 * @return
	 */
	public long getRowCount() {
		return rowCount;
	}

	/**
	 * 设置总共显示的数据记录
	 * @param rowCount
	 */
	public void setRowCount(long rowCount) {
		this.rowCount = rowCount;
	}

	/**
	 * 得到每页显示的数据记录
	 * @return
	 */
	public int getPageRowCount() {
		return pageRowCount;
	}

	/**
	 * 设置每页显示的数据记录
	 * @param pageRowCount
	 */
	public void setPageRowCount(int pageRowCount) {
		this.pageRowCount = pageRowCount;
	}

	/**
	 * 得到当前页
	 * @return
	 */
	public int getCurPage() {
		if(curPage <= 0 ) {
			curPage = 1;
		} else if(curPage >= this.getPageCount()) {
			curPage = this.getPageCount();
		}
		return curPage;
	}

	/**
	 * 设置当前页
	 * @param curPage
	 */
	public void setCurPage(int curPage) {
		this.curPage = curPage;
	}

	/**
	 * 得到每页的开始记录数
	 * @return
	 */
	public int getStart() {
		this.start = (this.getCurPage() - 1) * this.getPageRowCount();
		return start;
	}

	/**
	 * 得到每页的结束记录数
	 * @return
	 */
	public int getEnd() {
		this.end = this.getPageRowCount();
		return end;
	}

	/**
	 * 得到首页
	 * @return
	 */
	public int getFirst() {
		this.first = 1;
		return this.first;
	}
	
	/**
	 * 得到最后一页
	 * @return
	 */
	public int getLast() {
		this.last = (int)this.getPageCount();
		return last;
	}
	
	/**
	 * 上一页
	 * @return
	 */
	public int previous() {
		return (this.getCurPage() > 1) ? this.getCurPage() - 1 : 1;
	}
	
	/**
	 * 下一页
	 * @return
	 */
	public int next() {
		return (this.getCurPage() < (int)this.getPageCount()) ? this.getCurPage() + 1 : (int)this.getPageCount();
	}
	
	/**
	 * 判断是否是首页
	 * @return
	 */
	public boolean isFirst() {
		return (this.getCurPage() == 1) ? true : false;
	}
	
	/**
	 * 判断是否是最后一页
	 * @return
	 */
	public boolean isLast() {
		return (this.getCurPage() == this.getPageCount()) ? true : false;
	}
	
	/**
	 * 客户端显示的工具条
	 */
	public String getToolBar(String url) {
        String str, temp;
        
        //用于判断url中是否存在?
        if(url.indexOf("?") == -1) {
            temp = "?";
        } else {
            temp = "&";
        }
        
        str = "<form method='post' name='frmPage' action='" + url + "'> ";
        str += "<p align='right' id='page'>";
        str += "<span style='font-size: 12px; font-weight: bold;'>";
        //判断是否是首页
        if(isFirst()) {
        	str += "首页 上一页&nbsp;";
        } else {
            str += "<a href='" + url + temp + "cur_page=" + this.getFirst() + "'>首页</a>&nbsp;";
            str += "<a href='" + url + temp + "cur_page=" + this.previous() + "'>上一页</a>&nbsp;";
        }
        //判断是否是最后一页
        if(isLast()) {
        	str += "下一页 尾页&nbsp;";
        } else {
            str += "<a href='" + url + temp + "cur_page=" + this.next() + "'>下一页</a>&nbsp;";
            str += "<a href='" + url + temp + "cur_page=" + this.getLast() + "'>尾页</a>&nbsp;";
        }
        str += "&nbsp;共<b>" + this.rowCount + "</b>条记录&nbsp;";
        str += "&nbsp;转到<select name='page' onChange=\"location='" + url + temp + "cur_page='+this.options[this.selectedIndex].value\">";
        
        for(int i = 1; i <= this.getPageCount(); i++) {
        	//判断是否是当前页,若是,则默认为选中当前页
            if(i == this.getCurPage())
                str += "<option value='" + i + "' selected>第" + i + "页</option>";
            else
                str += "<option value='" + i + "'>第" + i + "页</option>";
        }
        str += "</select></span></p></form>";
        return str;
	}
}

评论(1)

  1. #1 楼 · zhaoljin · 2014-06-13 19:05

    :D 非常感谢

评论功能维护中,当前仅展示历史评论。