分页测试

第一次做分页,刚刚开始想从网上找一段现成的分页代码拿来直接使用,看了很多头有点晕,但在这种飘飘然的晕中貌似找到点了写分页的灵感,最后决定自己写一段分页代码。


先摆出我写的分页,比较简单较容易懂,后面会上一段标准的分页代码,供参考
原理很简单:用page对象来控制页面信息、改变页面内容
1、Struts 2 下运行
核心方法:根据 LIST<> 中的 subList(startIndex,endIndex);方法

// Page

public class Page {
private int totalCount; //总条目
private int totalPage; //总页面
private int currentCount; //当前条目
private int currentPage; //当前页面

public Page(){

}

public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurrentCount() {
return currentCount;
}
public void setCurrentCount(int currentCount) {
this.currentCount = currentCount;
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}

@Override
public String toString() {
// TODO Auto-generated method stub
return "totalCount"+totalCount+"\ttotalPage"+totalPage+"\tcurrentCount"+currentCount+"\tcurrentPage"+currentPage;
}

}




//Action 中代码 在Action中分页
public String execute() throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
ISubjectService subjectSer = new SubjectServiceImpl();
//得到考卷
ArrayList arrSubject = subjectSer.querySubject(); //将要分段的内容放置到容器
//****************************************************
//分页
int showCount = 15; //展示页面
int totalCount = arrSubject.size(); //总条目
int totalPage = arrSubject.size()/showCount; //总页数
Page page = (Page)session.getAttribute("page"); //得到页面

if(pageCount<0){
session.setAttribute("outOfPage", -1); //首页了
System.out.println("首页了");
return SUCCESS;
}
if(pageCount>totalPage){
session.setAttribute("outOfPage", 1); //尾页了
System.out.println("尾页了");
return SUCCESS;
}
if(page==null){
page = new Page();
page.setCurrentCount(0);
page.setCurrentPage(0);
page.setTotalCount(totalCount); //设置总条目
page.setTotalPage(totalPage); //设置总页数
}
//page.setTotalCount(totalCount);
//page.setTotalPage(totalPage);
page.setCurrentPage(pageCount); //设置当前条目
int currentPage = page.getCurrentPage(); //当前页面
int currentCount = page.getCurrentCount(); //当前条目
int startCount = currentPage*showCount;
int endCount = currentPage*showCount + showCount;
List listSubject = null ;
if(totalPage==pageCount){
listSubject = arrSubject.subList(startCount, totalCount);

}else{
listSubject = arrSubject.subList(startCount, endCount);
}
session.setAttribute("page", page); //将页面对象传递到页面
//****************************************************
session.setAttribute("arrSubject", listSubject); //将分页得到的结果放入session
return super.execute();
}









style="color:gray;text-decoration:none " > 上一页
|
style="color:gray;text-decoration:none " > 下一页
 
当前页面:${session.page.currentPage +1 } /${session.page.totalPage +1 }




2、标准分页 Struts2 + hibernate

//Page
public class Page {
private int everyPage; //每页显示记录数
private int totalCount; //总记录数
private int totalPage; //总页数
private int currentPage; //当前页
private int beginIndex; //查询起始点
private boolean hasPrePage; //是否有上一页
private boolean hasNextPage; //是否有下一页
public Page(int everyPage, int totalCount, int totalPage,
int currentPage,int beginIndex, boolean hasPrePage,
boolean hasNextPage) { //自定义构造方法
this.everyPage = everyPage;
this.totalCount = totalCount;
this.totalPage = totalPage;
this.currentPage = currentPage;
this.beginIndex = beginIndex;
this.hasPrePage = hasPrePage;
this.hasNextPage = hasNextPage;
}
public Page(){} //默认构造函数
public int getEveryPage() { //获得每页显示记录数
return everyPage;
}
public void setEveryPage(int everyPage) {//设置每页显示记录数
this.everyPage = everyPage;
}
public int getTotalCount() {//获得总记录数
return totalCount;
}
public void setTotalCount(int totalCount) {//设置总记录数
this.totalCount = totalCount;
}
public int getTotalPage() {//获得总页数
return totalPage;
}
public void setTotalPage(int totalPage) {//设置总页数
this.totalPage = totalPage;
}
public int getCurrentPage() {//获得当前页
return currentPage;
}
public void setCurrentPage(int currentPage) {//设置当前页
this.currentPage = currentPage;
}
public int getBeginIndex() {//获得查询起始点
return beginIndex;
}
public void setBeginIndex(int beginIndex) {//设置查询起始点
this.beginIndex = beginIndex;
}
public boolean isHasPrePage() {//获得是否有上一页
return hasPrePage;
}
public void setHasPrePage(boolean hasPrePage) {//设置是否有上一页
this.hasPrePage = hasPrePage;
}
public boolean isHasNextPage() {//获得是否有下一页
return hasNextPage;
}
public void setHasNextPage(boolean hasNextPage) {//设置是否有下一页
this.hasNextPage = hasNextPage;
}
}




//页面信息
public class PageResult {
private Page page; //分页信息
private List list; //记录信息
public PageResult(Page page, List list) {
this.page = page;
this.list = list;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
}




/*
* 分页信息辅助类
*/
public class PageUtil {
public static Page createPage(int everyPage,int totalCount,int currentPage) {//创建分页信息对象
everyPage = getEveryPage(everyPage);
currentPage = getCurrentPage(currentPage);
int totalPage = getTotalPage(everyPage, totalCount);
int beginIndex = getBeginIndex(everyPage, currentPage);
boolean hasPrePage = getHasPrePage(currentPage);
boolean hasNextPage = getHasNextPage(totalPage, currentPage);
return new Page(everyPage, totalCount, totalPage, currentPage,
beginIndex, hasPrePage, hasNextPage);
}
public static int getEveryPage(int everyPage) { //获得每页显示记录数
return everyPage == 0 ? 10 : everyPage;
}
public static int getCurrentPage(int currentPage) { //获得当前页
return currentPage == 0 ? 1 : currentPage;
}
public static int getTotalPage(int everyPage,int totalCount) {//获得总页数
int totalPage = 0;
if(totalCount != 0 &&totalCount % everyPage == 0) {
totalPage = totalCount / everyPage;
} else {
totalPage = totalCount / everyPage + 1;
}
return totalPage;
}
public static int getBeginIndex(int everyPage,int currentPage) {//获得起始位置
return (currentPage - 1) * everyPage;
}
public static boolean getHasPrePage(int currentPage) {//获得是否有上一页
return currentPage == 1 ? false : true;
}
public static boolean getHasNextPage(int totalPage, int currentPage) { //获得是否有上一页
return currentPage == totalPage || totalPage == 0 ? false : true;
}
}

你可能感兴趣的:(技术笔记)