在开发网站的过程中,做分页处理肯定是常常遇到的。这里介绍一个对整个应用都有效的分页方式。(WAP开发中,用不了JS,显得更有用了)
在JSP页面上,只需在需要显示分页信息的地方加入请求参数(参数值为分页代码)。例如,在引入了struts2标签的页面中加入代码:<s:property value="pageInfo.nav">。 在发送请求的servlet中,只需实例化pageInfo,并设置好需要跳转的url(请求地址名:httpServletRequest.getRequestURI()、请求参数:httpServletRequest.getQueryString())。
下面是用于分页处理的Page类:
public class Page {
private int numPerPage = 5; // 页大小
private int totalRows; // 总记录数
private int totalPages; // 总页数
private int currentPage; // 起始页
private int startIndex; // 起始行数
private int lastIndex; // 结束行数
private boolean hasNext; // 是否有下一页
private boolean hasPrev; // 是否有上一页
private int offset = 0; // 偏移量
private String baseUrl = ""; //转向的地址
public Page() {
}
public Page(int currentPage,int totalRows){
init(this.numPerPage,currentPage,totalRows,0);
}
public Page(int currentPage,int totalRows,int numPerPage)
{
init(numPerPage,currentPage,totalRows,0);
}
public Page(int currentPage,int totalRows,int numPerPage,int offset)
{
init(numPerPage,currentPage,totalRows,offset);
}
// 初始化Page
private void init(int numPerPage,int currentPage,int totalRows,int offset){
this.numPerPage=numPerPage;
this.currentPage=currentPage;
this.totalRows=totalRows;
this.offset=offset;
this.setTotalPages();
this.setHasNext();
this.setHasPrev();
this.setStartIndex();
this.setLastIndex();
}
//构造分页导航条 最后的字符串显示到页面上
public String getNav(){
String navStr="";
if(this.isHasPrev()){
navStr+="<a href=/""+this.getBaseUrl()+"&pageNum="+(this.getCurrentPage()-1)+"/">上一页"+"</a> ";
}
if(this.hasNext){
navStr+="<a href=/""+this.getBaseUrl()+"&pageNum="+(this.getCurrentPage()+1)+"/">下一页"+"</a> ";
}
if(this.getTotalPages()>0){
navStr+="第"+this.getCurrentPage()+"页/共"+this.getTotalPages()+"页";
}
return navStr;
}
public int getNumPerPage() {
return numPerPage;
}
public void setNumPerPage(int numPerPage) {
this.numPerPage = numPerPage;
}
public int getTotalRows() {
return totalRows;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public int getTotalPages() {
return totalPages;
}
public void setTotalPages() {
if(totalRows%numPerPage==0){ //正好n页
totalPages=totalRows/numPerPage;
}else{
totalPages=totalRows/numPerPage+1;
}
if(currentPage>totalPages){
if(totalPages>0){
currentPage=totalPages;
}else{
currentPage=1;
}
}
if(currentPage<1){
currentPage=1;
}
}
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getStartIndex() {
return startIndex;
}
public void setStartIndex() {
startIndex=(currentPage-1)*numPerPage+1;
}
public int getLastIndex() {
return lastIndex;
}
public void setLastIndex() {
lastIndex=currentPage*numPerPage;
//lastIndex=startIndex+numPerPage-1
if(lastIndex>totalRows){
lastIndex=totalRows;
}
}
public boolean isHasNext() {
return hasNext;
}
public void setHasNext() {
if(totalPages>currentPage){
hasNext=true;
}else{
hasNext=false;
}
}
public boolean isHasPrev() {
return hasPrev;
}
public void setHasPrev() {
if(currentPage>1){
hasPrev=true;
}else{
hasPrev=false;
}
}
public int getOffset() {
return offset;
}
public void setOffset(int offset) {
this.offset = offset;
}
public String getBaseUrl() {
return baseUrl;
}
//设置“上一页”“下一页”转向的地址。url:请求项目名,param:请求参数(名值对)
public void setBaseUrl(String url,String param) {
int index=param.indexOf("pageNum")-1;
if(index>0){
// 清除先前的页面参数对,这里只处理页面信息在参数串末尾的情况。
param=param.substring(0,index);
}
this.baseUrl = url+"?"+param;
}
}