写的一个分页工具类,大家来多多指点

V 发布于 2010/09/01 11:16
阅读 577
收藏 2

import java.util.List;

public class SplitPage {
	
	private List totalResult = null; //所有的数据
	private List pageResult = null; //每页的数据
	private int pageSize = 10; // 每页显示的行数


	private int totalRow; //所有数据的行数
	private int totalPage; //总共的页数
	private int thisPage; //当前页面
	
	public SplitPage(List list) {
		super();
		this.totalResult = list;
		this.totalRow=list.size();
		this.thisPage=1;
		int page=this.totalRow/this.pageSize;
		if(this.totalRow%this.pageSize!=0)
			this.totalPage=page+1;
		else
			this.totalPage=page;
	}

	public SplitPage(List list,int num) {
		super();
		this.pageSize = num;
		this.totalResult = list;
		this.totalRow=list.size();
		this.thisPage=1;
		int page=this.totalRow/this.pageSize;
		if(this.totalRow%this.pageSize!=0)
			this.totalPage=page+1;
		else
			this.totalPage=page;
	}
	
	public List getFirstPage(){
		thisPage = 1;
		if(totalRow < pageSize)
			pageResult = totalResult;
		else
			pageResult = totalResult.subList(0, pageSize);
		return pageResult;
	}
	
	public List getLastPage(){
		thisPage = totalPage;
		if(totalRow < pageSize)
			pageResult = totalResult;
		else
			pageResult = totalResult.subList((totalPage-1)*pageSize,totalRow);
		return pageResult;
	}
	
	public List gotoPage(int go){
		thisPage = go;
		if(go == 1)
			return getFirstPage();
		else if(go == totalPage)
			return getLastPage();
		else{
			pageResult = totalResult.subList((go-1)*pageSize, go*pageSize);
			return pageResult;
		}			
	}
	
	public List goNextPage(int current){
		current++;
		return gotoPage(current);
	}
	
	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	
	public List goPrePage(int current){
		current--;
		return gotoPage(current);
	}
	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getThisPage() {
		return thisPage;
	}

	public void setThisPage(int thisPage) {
		this.thisPage = thisPage;
	}
}
加载中
0
RainJ
RainJ

今天才看到这个帖子,发现有些问题,什么都不说,先上testCase,请自己测试!~~~

import static org.junit.Assert.assertNotNull;

import java.util.ArrayList;

import java.util.List;

import org.junit.Before;

import org.junit.Test;

public class TestSplitPage {

List<Integer> list = null;

SplitPage splitPage = null;

@Before

public void init() {

list = new ArrayList<Integer>();

for (int i = 0; i < 100; i++) {

list.add(i);

}

splitPage = new SplitPage(list);

}

@Test

@SuppressWarnings("unchecked")

public void testGotoPage(){

List<Integer> result = (List<Integer>)splitPage.gotoPage(20);

assertNotNull(result);

}

}

0
RainJ
RainJ

另外,弱弱的提一点点建议

工具类,  如果我考虑到话我一般会将其设计成单例的,这样也许比较节约资源

另外,也许你愿意加上一个getCurrentPage的方法

返回顶部
顶部