importjava.util.ArrayList;
importjava.util.List;publicclassPage{
privateinttotalPage=1;
privateintprePage=1;
privateintnextPage=1;
privateinttotalRec=0;
privatefinalintdefaultPageSize=10;
privateintpageSize=defaultPageSize;
privateintpageIndex=1;
privateint[]pageNumbers;publicintgetPageIndex(){
returnpageIndex;
}publicvoidsetPageIndex(intpageIndex){
this.pageIndex=pageIndex>0?pageIndex:1;
}publicintgetNextPage(){
returnnextPage;
}publicvoidsetNextPage(intnextPage){
this.nextPage=nextPage>this.totalPage?this.totalPage:nextPage;
}publicintgetPageSize(){
returnpageSize;
}publicvoidsetPageSize(intpageSize){
this.pageSize=pageSize>0?pageSize:10;
}publicintgetPrePage(){
returnprePage;
}publicvoidsetPrePage(intprePage){
this.prePage=prePage}publicintgetTotalPage(){
returntotalPage;
}publicvoidsetTotalPage(inttotalPage){
this.totalPage=totalPage>0?totalPage:1;
}publicintgetTotalRec(){
returntotalRec;
}publicvoidsetTotalRec(inttotalRec){
this.totalRec=totalRec>-1?totalRec:0;
}publicint[]getPageNumbers(){
returnpageNumbers;
}publicvoidsetPageNumbers(int[]pageNumbers){
this.pageNumbers=pageNumbers;
}
}
==============实体类有了,我们得在写一个公共的DAO方法(也就是每一个用到分页的地方都会调用这个)================publicListfindPageByQuery(finalStringqueryString,
finalObject[]parameters,finalPagepageInfo){
return(List)getHibernateTemplate().execute(newHibernateCallback()//这里使用了匿名内部类
{
publicObjectdoInHibernate(Sessionsession)//Spring进行事务维护省去每次创建session和关闭session
throwsHibernateException
{
Queryquery=session.createQuery(queryString);
if(parameters!=null)
{
for(inti=0;i
query.setParameter(i,parameters[i]);
}
}
ScrollableResultssr=query.scroll();
sr.last();
inttotalCount=sr.getRowNumber();
intstartIndex=(pageInfo.getPageIndex()-1)
*pageInfo.getPageSize();
query.setMaxResults(pageInfo.getPageSize());
query.setFirstResult(startIndex);
inttotalRec=totalCount+1;
pageInfo.setTotalRec(totalRec);
inttotalPage=(totalRec%pageInfo.getPageSize()==0)?(totalRec/pageInfo
.getPageSize())
:(totalRec/pageInfo.getPageSize())+1;
int[]pageNumbers=newint[totalPage];
for(inti=0;i
pageNumbers[i]=(i+1);
}
pageInfo.setPageNumbers(pageNumbers);
pageInfo.setTotalPage(totalPage);
pageInfo.setPageSize(pageInfo.getPageSize());
pageInfo.setPageIndex(pageInfo.getPageIndex());
pageInfo.setPrePage(pageInfo.getPageIndex()-1);
pageInfo.setNextPage(pageInfo.getPageIndex()+1);
returnquery.list();
}
},true);
}
=================至此我们的工作就完成了一半了,接下了就改写具体的方法了,来调用我们的公工分页方法publicListgetUU(intlexample,PagepageInfo)
{
//自己测试时候可以使用
Listlist=null;
if(pageInfo==null)
pageInfo=newPage();
try
{
hql="SELECTexaFROMExampleexaWHERE1=1ANDexa.LExample="+lexample;
list=this.getCommonDAO().findPageByQuery(hql,null,pageInfo);
}catch(Exceptionex)
{
System.out.println(ex);
}
returnlist;
}=============接下来就是action中调用了===============publicActionForwardgetMore(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse){
ExampleFormexampleForm=(ExampleForm)form;//TODOAuto-generatedmethodstub
Listlist=newArrayList();
try
{
//pageInfo=(PageInfo)request.getAttribute("pageInfo");
Stringtype=request.getParameter("type");
//类别编号
inttypeId=0;
//页码编号
Stringpage=request.getParameter("page");
if(page==null)
{page="1";
}
if(type==null){
//默认显示女的
type="2";
}
System.out.println("action...........................");
pageInfo.setPageIndex(Integer.parseInt(page));
pageInfo.setPageSize(20);
typeId=Integer.parseInt(type);
//pageInfo.setPageIndex(Integer.parseInt(request.getParameter("Index")));
list=this.exampleBiz.getUU(typeId,pageInfo);
if(list.size()!=0)
{
request.setAttribute("list",list);
request.setAttribute("pageInfo",pageInfo);
}
}catch
(Exceptionex)
{
ex.printStackTrace();
}
returnmapping.findForward("more");
}======下面是页面,我们的工作马上就完了。<%stringnowpage>%stringnowpage>Stringtype;
inttotal;
nowPage=request.getParameter("page");
if(nowPage==null)
{nowPage="1";
}
type=request.getParameter("type");//这个是我项目中的类别,没必要的
if(type==null){
type="2";
}
intcurPage=Integer.parseInt(nowPage);
inttypeId=Integer.parseInt(type);
System.out.println(typeId+"------------------------------------");
if((Page)request.getAttribute("pageInfo")==null){
total=1;
}else{
total=((Page)request.getAttribute("pageInfo")).getTotalPage();
if(curPage>((Page)request.getAttribute("pageInfo")).getTotalPage()){
curPage=total;
}
}
%>
明星指数:254179
当前第<%>页转到第
<%if>
selected
<%>
>
<%>
<%>%>}
%>
页
共<%>页${pageInfo.pageIndex}
OK!,现在我们的分页就行了,赶快试试吧!%>%>%>%if>