当前位置: 代码迷 >> Java Web开发 >> MVC分页问题
  详细解决方案

MVC分页问题

热度:175   发布时间:2006-04-24 15:07:00.0
MVC分页问题

各位哥哥姐姐
请教下高效简单的分页java源代码
感激不尽!!!

搜索更多相关主题的帖子: MVC  源代码  java  感激不尽  

----------------解决方案--------------------------------------------------------

public class PageBean {

int currentPage=1;//当前页数

public int totalPages=0;//总页数

int pageRecorders=5;//每页显示数

int totalRows=0;//总数据数

int pageStartRow=0;//每页的起始数

int pageEndRow;//每页的终止数

boolean hasNextPage=false;//是否有下一页

boolean hasPreviousPage=false;//是否有前一页

ArrayList arrayList;

Iterator it;

public PageBean(ArrayList arrayList) {

this.arrayList=arrayList;

totalRows=arrayList.size();

it=arrayList.iterator();

hasPreviousPage=false;

currentPage=1;

if((totalRows%pageRecorders)==0) {

totalPages=totalRows/pageRecorders;

}

else {

totalPages=totalRows/pageRecorders+1;

}

if(currentPage>=totalPages) {

hasNextPage=false;

}

else {

hasNextPage=true;

}

if(totalRows<pageRecorders) {

this.pageStartRow=0;

this.pageEndRow=totalRows;

}

else {

this.pageStartRow=0;

this.pageEndRow=pageRecorders;

}

}

public void setCurrentPage(int currentPage) {

this.currentPage=currentPage;

}

public void setPageRecorders(int pageRecorders) {

this.pageRecorders=pageRecorders;

}

public void setHasNextPage(boolean hasNextPage) {

this.hasNextPage=hasNextPage;

}

public void setHasPreviosPage(boolean hasPreviosPage) {

this.hasPreviousPage=hasPreviousPage;

}

public String getCurrentPage() {

return this.toString(currentPage);

}

public String getTotalPages() {

return this.toString(totalPages);

}

public String getTotalRow() {

return this.toString(totalRows);

}

public int getPageRecorders() {

return pageRecorders;

}

public int getPageEndRow() {

return pageEndRow;

}

public int getPageStartRow() {

return pageStartRow;

}

public boolean isHasNextPage() {

return hasNextPage;

}

public boolean isHasPreviousPage() {

return hasPreviousPage;

}

public Book[] getNextPage() {

currentPage=currentPage+1;

if((currentPage-1)>0) {

hasPreviousPage=true;

}

else {

hasPreviousPage=false;

}

if(currentPage>=totalPages) {

hasNextPage=false;

}

else {

hasNextPage=true;

}

Book[] books=getBooks();

return books;

}

public Book[] getPreviousPage() {

currentPage=currentPage-1;

if(currentPage==0) {

currentPage=1;

}

if(currentPage>=totalPages) {

hasNextPage=false;

}

else {

hasNextPage=true;

}

if((currentPage-1)>0) {

hasPreviousPage=true;

}

else {

hasPreviousPage=false;

}

Book[] books=getBooks();

return books;

}

public Book[] getBooks() {

if(currentPage*pageRecorders<totalRows) {

pageEndRow=currentPage*pageRecorders;

pageStartRow=pageEndRow-pageRecorders;

}

else {

pageEndRow=totalRows;

pageStartRow=pageRecorders*(totalPages-1);

}

Book[] books=new Book[pageEndRow-pageStartRow+1];

int j=0;

for(int i=pageStartRow;i<pageEndRow;i++) {

Book book=(Book)arrayList.get(i);

books[j++]=book;

}

return books;

}

public String toString(int temp) {

String str=Integer.toString(temp);

return str;

}

}

其中ArrayList是从数据库中读出来,返回的一个想对应的集合对象!主要部分可以给你点启示~~~~


----------------解决方案--------------------------------------------------------
楼上的好热心啊
----------------解决方案--------------------------------------------------------
回复:(zhangheng)public class PageBean { in...

请问二楼的大哥,Book 是一个javabean吗?


----------------解决方案--------------------------------------------------------
楼主 你有什么用什么框架啊???
分页有很多种``要根据你的具体情况来定`
如果你是的Hibernate做数据层``那么有另外的方法```
2楼的分页是可以的
----------------解决方案--------------------------------------------------------
  相关解决方案