JSP翻页功能代码

<%
int intPageSize; //一页显示的记录数 
int intRowCount; //记录总数 
int intPageCount; //总页数 
int intPage; //待显示页码 
String strPage; 

//设置一页显示的记录数 
intPageSize = 25; 

//取得待显示页码 
strPage = request.getParameter("page"); 
if(strPage==null){//表明在QueryString中没有page这一个参数,此时显示第一页数据 
intPage = 1; 
}else{//将字符串转换成整型 
intPage = java.lang.Integer.parseInt(strPage); 
if(intPage< 1){ 
intPage = 1; 


Rs=connJB.executeQuery(sql); 
Rs.absolute(1); 


//获取记录总数 
Rs.last(); 
intRowCount = Rs.getRow(); 

//记算总页数 
intPageCount = (intRowCount+intPageSize-1) / intPageSize; 

//调整待显示的页码 
if(intPage >intPageCount) intPage = intPageCount; 
if(intPageCount >0){ 
//将记录指针定位到待显示页的第一条记录上 
Rs.absolute((intPage-1) * intPageSize+1); 
//显示数据 
i = 0; 
while(i< intPageSize && !Rs.isAfterLast()){ 
if(!className.equals("行业分类")){ 
//id=Rs.getInt("INFORMATION_ID"); 
strId=Rs.getString("INFORMATION_ID"); 
subjectName=Rs.getString("INFORMATION_NAME"); 
informationRank=Rs.getString("INFORMATION_RANK"); 


%> 
<tr> 
<td width="4%" height="25" valign="middle"><div align="center">◇</div></td> 
<td width="96%"><a href="index2.jsp?INFORMATION_ID=<%=strId%>&INFORMATION_RANK=<%=informationRank%>" class="t8"><%=subjectName%></a></td> 
</tr> 
<% }else{ 
strId=Rs.getString("SUB_CLASS_VALUE"); 
subjectName=Rs.getString("SUB_CLASS_VALUE"); 
%> 
<tr> 
<td width="4%" height="25" valign="middle"><div align="center">◇</div></td> 
<td width="96%"><a href="index_way.jsp?SMALL_CLASS=<%=strId%>" class="t8"><%=subjectName%></a></td> 
</tr> 
<%}%> 

<% 
Rs.next(); 
i++; 

}Rs.close(); 
i=0; 
%> 
</table> 
<table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#CCCCCC"> 
<tr> 
<td valign="top" bgcolor="#F2F2F2"> 
<div align="left"> 

</div></td> 
</tr> 
</table> 
<table width="100%" border="0" align="center" cellpadding="0" cellspacing="1" bgcolor="#8C8C8C"> 
<tr> 
<td height="22" align="center" bgcolor="#FFFFFF">第<%=intPage%>页 共<%//=intRowCount%>条记录/共<%=intPageCount%>页 
<% 
if(intPage<2){ 
out.print("首页 | 上一页"); 
}else{%> 
<a href="index.jsp?CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">首页 </a> | <a href="index.jsp?page=<%=intPage-1%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">上一页</a> 
<%}%> 

<% 
if((intPageCount-intPage)<1) 

out.print("下一页 | 尾页"); 
}else{%> 
<a href="index.jsp?page=<%=intPage+1%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">下一页</a> | <a href="index.jsp?page=<%=intPageCount%>&CLASS_NAME=<%=className%>&SUBJECT_NAME=<%=titleName%>">尾页</a> 
<%} 

else{ 
out.print("<center><table width=776 border=0 align=center cellpadding=0 cellspacing=1 bgcolor=#000000><tr><td bgcolor=#FFFFFF><center>目前还没有内容!</center></td></tr></table></centre>"); 
}%> 


其中sql为具体的SQL语句.
package com; public class Pager { private int totalRows = 0; // 记录总数 private int totalPages = 0; // 总页数 private int pageSize = 10; // 每页显示数据条数,默认为10条记录 private int currentPage = 1; // 当前页数 private boolean hasPrevious = false; // 是否有上一页 private boolean hasNext = false; // 是否有下一页 public int getSearchFrom() { return (currentPage - 1) * pageSize; } public Pager() { } public void init(int totalRows) { this.totalRows = totalRows; this.totalPages = ((totalRows + pageSize) - 1) / pageSize; refresh(); // 刷新当前页面信息 } /** * * @return Returns the currentPage. * */ public int getCurrentPage() { return currentPage; } /** * * @param currentPage * current page * */ public void setCurrentPage(int currentPage) { this.currentPage = currentPage; refresh(); } /** * * @return Returns the pageSize. * */ public int getPageSize() { return pageSize; } /** * * @param pageSize * The pageSize to set. * */ public void setPageSize(int pageSize) { this.pageSize = pageSize; refresh(); } /** * * @return Returns the totalPages. * */ public int getTotalPages() { return totalPages; } /** * * @param totalPages * The totalPages to set. * */ public void setTotalPages(int totalPages) { this.totalPages = totalPages; refresh(); } /** * * @return Returns the totalRows. * */ public int getTotalRows() { return totalRows; } /** * * @param totalRows * The totalRows to set. * */ public void setTotalRows(int totalRows) { this.totalRows = totalRows; refresh(); } // 跳到第一页 public void first() { currentPage = 1; this.setHasPrevious(false); refresh(); } // 取得上一页(重新设定当前页面即可) public void previous() { if (currentPage > 1) { currentPage--; } refresh(); } // 取得下一页 public void next() { //System.out.println("next: totalPages: "
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值