我们在工程中常用的JSP+MySql的分页功能,现在拿出来我大家分享,
缺点:1. 不灵活 2. 只能写在页面中
优点:肯定能用(总比不能用好啊)
<body>
<%
int intPageSize = 4; //每页显示的记录
int currPageBegin; //起始记录索引
int intPageCount; //总页数
int intPage; //待显示页码
String strPage; //用户请求页码
//读取数据内容开始
NewsAdd na = new NewsAdd();
Vector vector = new NewsAdd().queryNewsAll();
//读取数据内容结束
//取得待显示页码(第一个写死在分页处)
strPage = request.getParameter("pagenumber");
//显示第一页
if(strPage == null){
intPage = 1;
}else{
//获取当前页码
intPage = Integer.parseInt(strPage);
//如果当前页码小于1,则显示第一页
if(intPage < 1)
intPage = 1;
}
intPageCount = (vector.size()+intPageSize-1)/intPageSize;
//调整待显示的页码
if(intPage > intPageCount)intPage = intPageCount;
currPageBegin = intPage*intPageSize-intPageSize;
%>
<table width="100%" border="0" cellspacing="3" cellpadding="0">
<%
if(!vector.isEmpty()){
for(int i = currPageBegin;i<intPage*intPageSize && i < vector.size();i++){
//循环显示数据
//NewsForm nf = (NewsForm)vector.elementAt(i);
int number = i+1;
%>
<tr>
td>
<span class="youyan">共<span style="color:#FF6600"><%=intPageCount %></span>页</span>
<a href="<%=path%>/secondnews/morenews.jsp?pagenumber=1" class="youyan"><font color="gray">首页</font></a>
<% if(intPage > 1){%>
<span class="youyan"><< <a href="<%=path%>/secondnews/morenews.jsp?pagenumber=<%=intPage-1%>" class="youyan"><font color="gray">上一页</font></a></span>
<% }else{%><span class="youyan"><< <font color="gray">上一页</font></span><%} %>
<span class="youyan"><font color="#666666">第</font><font color="#FF6600"><%=intPage %></font><font color="#666666"> 页</font></span>
<% if(intPage < intPageCount){%>
<a href="<%=path%>/secondnews/morenews.jsp?pagenumber=<%=intPage+1%>" class="youyan"><font color="#666666">下一页</font>>> </a>
<a href="<%=path%>/secondnews/morenews.jsp?pagenumber=<%=intPageCount%>" class="youyan"><font color="#666666">尾页</font>>> </a>
<%}else{%>
<span class="youyan"><font color="#666666">下一页</font>>> </span>
<span class="youyan"><font color="#666666">尾页</font>>> </span>
<% }%>
</td></tr><%} %>
</table>
</body>