一个分页BEAN

发表于:2007-07-01来源:作者:点击数: 标签:
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=objpag
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>&nbsp;<a href=\"?page="+p+"\" title=\"上页\"><strong><font face=webdings>7</font></strong></a>&nbsp;";
}
if(Tmpa>currentpage+stride){//大于当前页+跨度的页面
break;
}
willprint+="<a href=\"?page="+Tmpa+"\"><strong>"+Tmpa+"</strong></a>&nbsp;";

if(Tmpa==currentpage+stride){//刚好够跨度的页数
p=currentpage+stride+1;
willprint+="<a href=\"?page="+p+"\" title=\"下页\"><strong><font face=webdings>8</font></strong></a>&nbsp;<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>

原文转自:http://www.ltesting.net