jsp分页的问题
dwxq 2003-10-14 03:55:30 从书上抄个jsp分页的例子运行,第一页的数据不能正常显示,而第二页的数据能够正常显示,不知原因,
jsp代码如下:
<jsp:useBean id="aaaBean" scope="page" class="firm.firm"/>
<%
//定义变量初始化
int pageLine=10; //指导定行数
int totalRec=0; //总记录数
int intPage=1; //当前页数
int i; //循环次数
if (request.getParameter("page")!=null)
intPage=Integer.parseInt(request.getParameter("page"));
//把从页面跳转表单中获取的数值类型(字符型)转换为整数型
try {
ResultSet countrs=null;
//取得总记录数
countrs=aaaBean.executeQuery("select count(*) as cnt from firm");
if (countrs.next())
totalRec=countrs.getInt("cnt");
countrs.close();
aaaBean.closeStmt();
}
catch(Exception e){//异常处理
e.printStackTrace();
}
//取得总页数
int intPageCount=0;
intPageCount=(totalRec+pageLine-1)/pageLine;
%>
<%
ResultSet rs=aaaBean.executeQuery("select * from firm");
//定义变量
String name;
String userid;
%>
<table border="1" width="650" align="center" cellspacing="0" bgcolor="#d7e3b9" bordercolorlight="#green" bordercolordark="ecf5ff">
<tr bgcolor="c8cc98" align="center">
<td width="20%">编号</td>
<td width="60%">姓名</td>
<td width="20%">删除</td>
</tr>
<%
if (intPageCount>0) //如果总页数大于0,则执行下面的语句
{
for (i=1;i<=(intPage-1)*pageLine;i++){ //每页的记录循环
rs.next();
for (i=1;i<=pageLine;i++)
if (rs.next())
{
name=rs.getString("name");
userid=rs.getString("id");
%>
<tr>
<td width="20%" align="center"><%=userid%></td>
<td width="60%" align="center"><%=name%></td>
<td width="20%" align="center"><a href="delete.jsp?id=<%=userid%>" onclick="return newwin(this.href)">删除</a></td>
</tr>
<%
}
}
rs.close();
}
%>
<%
//以下用于分页显示
out.print("<table border='0' align='center'><tr><td>");
if(intPageCount*pageLine<totalRec)
intPageCount++;
//如果总页数*记录行数总记录数,即页数加1
if (intPage>intPageCount)
intPage=intPageCount;
//如果当前页数大于总页数,则当前等于总页数
if (intPage<1)
intPage=1; //如果当前页数小于1,则把它设置为1
out.print("<form method='post' name='fpagenum' action='show.jsp' align='center'>");
out.print("<input type='hidden' name='page' value="+intPage+">");
//若使用跳转页数,则执行此句
out.print("<p align='left'>分页 ");
if (intPage<2) //如果当前页数小于2,则首页和上一页无连接
out.print("<font color='999966'>首页 【上一页】</font> ");
else{
out.print("<a href='show.jsp?page="+1+"'>【首页】</a> ");
out.print("<a href='show.jsp?page="+(intPage-1)+"'>【上一页】</a> ");
}
if (intPage-intPageCount>=0)//如果当前页数小于总页数,则下一页和尾页有连接
out.print("<font color='999966'>【下一页】 【尾页】</font>");
else{
out.print("<a href='show.jsp?page="+(intPage+1)+"'>【下一页】</a> ");
out.print("<a href='show.jsp?page="+(intPageCount)+"'>【尾页】</a>");
}
out.print(" 页次:<font color='red'><strong>"+intPage+"</strong></font>/<font color='red'><strong>"+intPageCount+"</strong></font>页");
out.print(" 共<font color='red'><b>"+totalRec+"</b></font>条记录<b> <font color='red'>"+pageLine+"</font></b> 条/页 ");
out.print("转到第 <input type='text' name='page' size='2' maxlength='10' class='smallinput' value="+intPage+">");//跳转页
out.print(" 页 <input class='buttongace' type='submit' value='转到' size='5' name='cndok'></span></p></form>");
out.print("</td>");
out.print("</tr>");
out.print("</table>");
%>
</table>