package xx;
import java.lang.*;
public class page {
private int totalpage,stride,currentpage;
//设置总页数
public void settotalpage(int objpage){
totalpage=objpage;
}
//设置当前页
public void setcurrentpage(int objpage){
currentpage=objpage;
}
//设置跨度
public void setstride(int objstride){
stride=objstride;
}
//获得总页数
public int gettotalpage(){
return totalpage;
}
//获得跨读
public int getstride(){
return stride;
}
//获取当前页
public int getcurrentpage(){
return currentpage;
}
//打印分页
public string show(){
string willprint=new string();
int p,tmpa;
for(tmpa=1;tmpa<=totalpage;tmpa++){
if(tmpa+stride<currentpage){//加了跨度还小于当前页的不显示
continue;
}
if(tmpa+stride==currentpage){//刚好够跨度的页数
p=currentpage-stride-1;
willprint+="<a href=\"?page=1\" title=\"首页\"><strong><font face=webdings>9</font></strong></a> <a href=\"?page="+p+"\" title=\"上页\"><strong><font face=webdings>7</font></strong></a> ";
}
if(tmpa>currentpage+stride){//大于当前页+跨度的页面
break;
}
willprint+="<a href=\"?page="+tmpa+"\"><strong>"+tmpa+"</strong></a> ";
if(tmpa==currentpage+stride){//刚好够跨度的页数
p=currentpage+stride+1;
willprint+="<a href=\"?page="+p+"\" title=\"下页\"><strong><font face=webdings>8</font></strong></a> <a href=\"?page="+totalpage+"\" title=\"尾页\"><strong><font face=webdings>:</font></strong></a>";
}
}
return willprint;
}
}
调用:
—–
<%@page import="java.util.*"%>
<%@page import="java.io.*"%>
<%@page contenttype="text/html;charset=gb2312"%>
<html>
<head>
<title>分页bean</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
</head>
<style type="text/css">
body,td,input {font-size:12px;color:red}
</style>
<body>
<hr size="1" />
<jsp:usebean id="cm" class="xx.page" />
<%
string cpage=request.getparameter("page");
string stcpath=new string();
int dpage;
if(cpage==null){
cpage="1";
}
dpage=integer.parseint(cpage);
cm.settotalpage(100);
cm.setcurrentpage(dpage);
cm.setstride(5);
stcpath=cm.show();
out.println(stcpath);
%>
</body>
</html>
