您的位置:首页 > 其它

经典仿谷歌分页实例

2011-03-02 20:59 211 查看
经典仿谷歌分页,此算式是我自己独有想出,如有雷同纯属偶然,
首先,我的Page类:
public class page {
private int nowpage;//当前页
private int countpage;//总页数
private int countnum;//总记录数
private static final int PAGESIZE=5;//每页的记录数
private int start;//页面显示页数开始
private int end;//页面显示页数结尾
private List nowpagelist;//每页记录
public int getNowpage() {
return nowpage;
}
public void setNowpage(int nowpage) {
this.nowpage = nowpage;
}
public int getCountpage() {
return countpage;
}
public void setCountpage(int countpage) {
this.countpage = countpage;
}
public int getCountnum() {
return countnum;
}
public void setCountnum(int countnum) {
this.countnum = countnum;
}
public int getStart() {
return start;
}
public void setStart(int start) {
this.start = start;
}
public int getEnd() {
return end;
}
public void setEnd(int end) {
this.end = end;
}
public List getNowpagelist() {
return nowpagelist;
}
public void setNowpagelist(List nowpagelist) {
this.nowpagelist = nowpagelist;
}
//仿谷歌分页的算式
public static page getpage(page p){
int start=(p.getNowpage()-5)>1?(p.getNowpage()-5):1;
int end=p.getNowpage()+4;
if((p.getNowpage()-5)>1){
start=p.getNowpage()-5;
end=p.getNowpage()+4;
}
if((p.getNowpage()+4)>=p.getCountpage()){
end=p.getCountpage();
start=p.getCountpage()-10;
}
p.setStart(start);
p.setEnd(end);
return p;
}
}

介绍第一步:
页面代码:
<div>
<c:if test="${page.nowpage-3>1}">
<span><a href="./servlet/showstudent?nowpage=1">首页</a></span>
</c:if>
<c:if test="${page.nowpage!=1}">
<span><a href="./servlet/showstudent?nowpage=${page.nowpage==1?1:page.nowpage-1}">上一页</a></span>
</c:if>
<c:forEach begin="${page.start}" end="${page.end}" var="ss" >
<a href="./servlet/showstudent?nowpage=${ss}">${ss }</a>

</c:forEach>
<c:if test="${page.nowpage!=page.countpage}">
<span><a href="./servlet/showstudent?nowpage=${page.nowpage==page.countpage?page.countpage:page.nowpage+1}">下一页</a></span>
</c:if>
<c:if test="${page.nowpage+4<page.countpage}">
<span><a href="./servlet/showstudent?nowpage=${page.countpage}">最后一页</a></span>
</c:if>
<span>
<input type="text" id="nowpage" style="width: 25px;height: 25px;" name="nowpage">
<input type="button" value="GO" onclick="goPage()">
</span>
</div>

页面中有段javascript的代码为:
<script type="text/javascript">
function goPage(){
var nowpage=document.getElementById("nowpage").value;
window.location.href="./showstudent?nowpage="+nowpage;
}

</script>

从页面中得到nowpage(当前页)
把nowpage交给后台程序,下列代码在类 :Studentdaoimpl后台程序代码为:
public List getList(int nowpage) {
// TODO Auto-generated method stub
List list =new ArrayList();
con=Dbcon.getcon();
try {
pr=con.prepareStatement("select id,name,age,adress from student limit ?,?");
pr.setInt(1, (nowpage-1)*5);
pr.setInt(2, 5);
re=pr.executeQuery();
while(re.next()){
Student s=new Student();
s.setId(re.getInt("id"));
s.setName(re.getString("name"));
s.setAge(re.getInt("age"));;
s.setAdress(re.getString("adress"));
list.add(s);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return list;
}

public int getcountjilu() {
// TODO Auto-generated method stub
int i=0;
con=Dbcon.getcon();
try {
pr=con.prepareStatement("select count(*) from student");
re=pr.executeQuery();
while(re.next()){
i=re.getInt(1);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return i;
}

public int getcountpage() {
// TODO Auto-generated method stub
int i=0;
int j=this.getcountjilu();
i=j%5==0?j/5:j/5+1;
return i;
}
//**********************************************
Studentdaoimpl类创建一个实体即为s;
public page getcountpage(page p) {
// TODO Auto-generated method stub
p.setCountnum(s.getcountjilu());
p.setCountpage(s.getcountpage());
p.setNowpagelist(s.getList(p.getNowpage()));
p=p.getpage(p);
return p;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: