1.action
public class DataMangAction extends ActionSupport implements ServletRequestAware, ModelDriven<DataMangForm>
{
private String str;
private HttpServletRequest request;
private int pageSize = 2;
private DataMangForm model = new DataMangForm();
public String execute() throws Exception
{
System.out.println("ok");
str="execute";
return str;
}
public String init() throws Exception
{
DataMangForm dataMangForm = getModel();
ArrayList baseInfoList;
// 获取当前页码
int pageNum = dataMangForm.getPageNum();
DataMangOpertion dataMangOpertion=new DataMangOpertion();
baseInfoList = dataMangOpertion.getAllBaseInfo(pageSize, pageNum);
if (baseInfoList != null)
{
dataMangForm.setBaseInfoList(baseInfoList);
Pagination pagination = dataMangOpertion.getPageInAction();
pagination.setShowCurrentState(true);
pagination.setShowPageNumber("dataMangAction!init.action", "", 10, 5);
dataMangForm.setPagination(pagination);
request.setAttribute("pagination", pagination);
}
str="init";
return str;
}
public String toAdd() throws Exception
{
str="toAdd";
return str;
}
public DataMangForm getModel()
{
return model;
}
public void setServletRequest(HttpServletRequest request)
{
this.request = request;
}
}
2.pagination
public class Pagination
{
/*
** pageSize 每页记录数
** recordCount 总记录数
** pageNum 当前页
** pageCount 总页数
*/
private int pageSize;
private int recordCount;
private int pageNum;
private int pageCount;
private String showPageNumber="";
private String showCurrentState;
public Pagination()
{
}
public Pagination(int pageNum,int pageSize,int recordCount,int pageCount)
{
this.setPageNum(pageNum);
this.setPageSize(pageSize);
this.setRecordCount(recordCount);
this.setPageCount(pageCount);
}
public void setPageSize(int pageSize)
{
this.pageSize=pageSize;
}
public int getPageSize()
{
return pageSize;
}
public void setRecordCount(int recordCount)
{
this.recordCount=recordCount;
}
public int getRecordCount()
{
return recordCount;
}
public void setPageNum(int pageNum)
{
this.pageNum=pageNum;
}
public int getPageNum()
{
return pageNum;
}
public void setPageCount(int pageCount)
{
this.pageCount=pageCount;
}
public int getPageCount()
{
return pageCount;
}
//显示分页的当前状态信息
public void setShowCurrentState(boolean bool)
{
if(bool)
{
this.showCurrentState = "共有" + recordCount + "条记录数,每页" + pageSize + "条,第<font color=\"red\">"+ pageNum + "</font>页,"+"共" +pageCount + "页." ;
}
else
{
this.showCurrentState="";
}
}
public String getShowCurrentState()
{
return showCurrentState;
}
//显示分页码
public void setShowPageNumber(String path,String parameters,String first,String previous,String next,String last)
{
if(pageNum>1)
{
showPageNumber="<a href=\"" +path+ "?pageNum=1" +parameters+ "\">" +first+ "</a> ";
showPageNumber=showPageNumber+"<a href=\"" +path+ "?pageNum=" +(pageNum-1)+parameters+ "\">" +previous+ "</a> ";
}
if(pageNum<pageCount)
{
showPageNumber=showPageNumber+"<a href=\"" +path+ "?pageNum=" +(pageNum+1)+parameters+ "\">" +next+ "</a> ";
showPageNumber=showPageNumber+"<a href=\"" +path+ "?pageNum=" +pageCount+parameters+ "\">" +last+ "</a> ";
}
}
public void setShowPageNumber(String path,String parameters,int middleSize,int kipSize)
{
int start=(pageNum/kipSize)*kipSize+1;
int end=start-1+middleSize;
if(end>=pageCount)
{
end=pageCount;
}
if(start>kipSize)
{
showPageNumber="<a href=" +path+ "?pageNum=" +(start-2)+parameters+ ">" +"<<<"+ "</a> ";
}
while(start<=end)
{
showPageNumber=showPageNumber+"<a href=" +path+ "?pageNum=" +start+parameters+ ">[" +start+ "]</a> ";
start++;
}
if(end<pageCount)
{
showPageNumber=showPageNumber+"<a href=" +path+ "?pageNum=" +(end+1)+parameters+ ">" +">>>"+ "</a> ";
}
}
public String getShowPageNumber()
{
return showPageNumber;
}
}
3.在JSP页面显示
${pagination.showPageNumber} ,${pagination.showCurrentState}