分页代码

来源:互联网 发布:淘宝低价卖正货 编辑:程序博客网 时间:2024/05/16 15:31
<%!
int totalPages;         //总页数 变量
int countPerPage=5;    //计数 每页显示文章数 变量
int totalCount;        //信息总计数 变量
int currentPage=1;      //当前第几页面,赋值为1 变量
int currentStart;
%>
<%
ArrayList vt=memoQuery.dbQueryMemos();  //通过DatabaseBean类中的dbQueryMemos()方法搜索查询数据库
totalCount=vt.size();   //数据库中存储信息的数量(size属性)
totalPages=totalCount/countPerPage;  // 总页数=存储信息总数量/每页显示文章数
if(totalCount%countPerPage>0)totalPages=totalPages+1;  // 余数大于0(或不等于0),则totalPages+1才是总页数
if(request.getParameter("page")==null)  
{
currentPage=1;
}
else
{
currentPage=Integer.parseInt(request.getParameter("page"));

if(currentPage>totalPages)currentPage=totalPages; 

if(currentPage<1)currentPage=1;
}
currentStart=totalCount-1-countPerPage * (currentPage-1);
%>

<script language="JavaScript" >
function page_check()
{
if(document.pageForm.page.value=="")
{alert("请输入页数");
document.pageForm.page.focus();
return false;
}
return true;
}</script>

<center>
   //数据的分页显示
              <%
    
    for(int i=0;i<countPerPage&&currentStart-i>=0;i++)
    {
     memoInfo.setId(((MemoBean)vt.get(currentStart-i)).getId());
     memoInfo.setTitle(((MemoBean)vt.get(currentStart-i)).getTitle());     
     memoInfo.setContent(((MemoBean)vt.get(currentStart-i)).getContent());
     memoInfo.setUdate(((MemoBean)vt.get(currentStart-i)).getUdate());
     
     
   out.print("<table width='100%' border='1' ><tr><td width='18%'>"); 
   out.print(memoInfo.getTitle()); 
   out.print("</td><td height='30' colspan=''>");
   out.print(memoInfo.getContent());
   out.print("</td><td width='15%'>");
   out.print(memoInfo.getUdate());  
   out.print("</td></tr></table> ");
   out.print("<br>");
     
    }
  
   %>


   <form name="pageForm" method="post"  onSubmit="return page_check()" action="liuyan.jsp">
   <table style="width:100%; border:1; bordercolor:#0099CC">                              
                <tr> 
                    <td width="40%"></td>
                  <td width="4%">页码:<%out.print(currentPage);%>/<%out.print(totalPages);%></td>
                  <td width="5%">共<%out.print(totalCount);%>个留言</td>
                    <td width="12%">
                        <input type="button" value="首页" onclick="javascript:window.location.href ='liuyan.jsp?page=1';">
                        <input type="button" value="上一页" onclick="javascript:window.location.href ='liuyan.jsp?page=<%=currentPage-1%>';">
                        
                        <input type="button" value="下一页" onclick="javascript:window.location.href ='liuyan.jsp?page=<%=currentPage+1%>';">
                        <input type="button" value="尾页" onclick="javascript:window.location.href ='liuyan.jsp?page=<%=totalPages%>';">    
                    </td>
                    <td width="10%">
                        <input type="submit" name="Submit" value="G0">                    
                        <input name="page" type="text" size="10"> 页
                      </td>
                </tr>
              </table></form>
                  
                   </center>     
                                                                                               
           <table width="100%" border="0">
          <tr> 
            <td height="12" align="center">Copyright @ HomeLee. All rights reserved. 
            </td>
          </tr>
          <tr> 
            <td height="12" align="center" bgcolor="#FFFFFF">&nbsp;</td>
          </tr>
        </table>