display标签在Webwork中实现数据库分页

首先有一个PageInfo类,在里面放一些页面的属性,如页数什么的:

public class PageInfo ... {
privatelongallCount;//总条数
privatelongpageCount;//每页条数
privatelongpage;//第几页
privatelongpages;//一共几页

//再加上getset方法

}

写一个DisplayPageHelper类实现PaginatedList接口,用来包装要显示的数据




import java.util.List;

import org.displaytag.pagination.PaginatedList;
import org.displaytag.properties.SortOrderEnum;

public class DisplayPageHelper implements PaginatedList ... {
privateListlist;
privateintpageNumber=1;
privateintobjectsPerPage=10;
privateintfullListSize=0;
privateStringsortCriterion;
privateSortOrderEnumsortDirection;
privateStringsearchId;

publicListgetList()...{
returnlist;
}


publicvoidsetList(Listlist)...{
this.list=list;
}


publicintgetPageNumber()...{
returnpageNumber;
}


publicvoidsetPageNumber(intpageNumber)...{
this.pageNumber=pageNumber;
}


publicintgetObjectsPerPage()...{
returnobjectsPerPage;
}


publicvoidsetObjectsPerPage(intobjectsPerPage)...{
this.objectsPerPage=objectsPerPage;
}


publicintgetFullListSize()...{
returnfullListSize;
}


publicvoidsetFullListSize(intfullListSize)...{
this.fullListSize=fullListSize;
}


publicStringgetSortCriterion()...{
returnsortCriterion;
}


publicvoidsetSortCriterion(StringsortCriterion)...{
this.sortCriterion=sortCriterion;
}


publicSortOrderEnumgetSortDirection()...{
returnsortDirection;
}


publicvoidsetSortDirection(SortOrderEnumsortDirection)...{
this.sortDirection=sortDirection;
}


publicStringgetSearchId()...{
returnsearchId;
}


publicvoidsetSearchId(StringsearchId)...{
this.searchId=searchId;
}

}

在Action中得到Display标签的页数:

page = (String)ServletActionContext.getRequest().getParameter( " page " );

然后把参数都设到PageInfo类实例中,把pageInfo传到Dao层,再在Action中把返回的数据用DisplayPageHelper的实例包装一下就可以了

private DisplayPageHelper display;

public String execute() {
resultSize = bookStoreManage.findBookCount();
page = (String)ServletActionContext.getRequest().getParameter("page");
if(page == null){
page = "1";
}
PageInfo pageInfo = new PageInfo();
pageInfo.setAllCount(resultSize);
pageInfo.setPageCount(20);
pageInfo.setPage(new Long(page).longValue());
display = new DisplayPageHelper();
display.setFullListSize(resultSize);
display.setPageNumber((new Integer(page)).intValue());
display.setObjectsPerPage(20);

  bookPicList = bookStoreManage.findBookPic(null, pageInfo);
display.setList(bookPicList);

}

再加上display的get 和set方法页面就可以得到了,页面要显示的List是Display


你可能感兴趣的:(Webwork)