java分页

 <%@ page language="java" import="java.util.*" pageEncoding="GB18030"%>
<%@page import="bean.ArticleBean"%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
  </head>
 
  <jsp:useBean id="artBean" class="bean.ArticleBean" scope="page"/>
  <jsp:useBean id="artDao" class="dao.ArticleDao" scope="page"/>
 
  <!-- 定义变量赋值 -->
  <%!
 int currPage=1;//当前页
 int rowPage=5;//每页行数
 int totalPage;//总页数
 int totalRow;//总记录数
 int indexBegin;//开始下标
 int indexEnd;//结束下标
 Vector vData;  
  %>
 
 <!-- 得到分页数据,并存入session内 -->
  <%
 Object obj=session.getAttribute("page");
 if(obj==null)
 { 
    vData=artDao.list();
    session.setAttribute("page",vData);
   }else
   {
    vData=(Vector)obj;
   }
  %>
 
  <!-- 给变量赋值 -->
  <%
   String number=request.getParameter("pageNum");
   currPage=1;
   if(number!=null)
   {
    currPage=Integer.parseInt(number);
   }
   totalRow=vData.size();
   totalPage=totalRow/rowPage;
   if(totalRow%rowPage!=0)
   {
    totalPage++;
   }
   indexBegin=(currPage-1)*rowPage;
   indexEnd=currPage*rowPage-1;
   if(currPage==totalPage)
   {
      indexEnd=totalRow-1;
   }
  %>
 
  <body>
    <table border=1>
     <%
      for(int i=indexBegin;i<=indexEnd;i++)
      {
       artBean=(ArticleBean)vData.get(i);
  %>
  <tr>
  <td><%=artBean.getCaption() %></td>
  <td><%=artBean.getContent() %></td>
  <td><%=artBean.getUserName() %></td>
  </tr>
  <%       
      }
     %>
    </table>
    当前页:<%=currPage %> 总页数:<%=totalPage %>
    <%
     if(currPage==1)
     {
    %>
    <a style="border-bottom:1px solid #999">首页</a>
    <a style="border-bottom:1px solid #999">上一页</a>
    <%
     }
     else
     {
    %>
    <a href="index.jsp?pageNum=1">首页</a>
    <a href="index.jsp?pageNum=<%=currPage-1 %>">上一页</a>
    <% 
     }
    %>
    <%
     if(currPage==totalPage)
     {
    %>
    <a style="border-bottom:1px solid #999">下一页</a>
    <a style="border-bottom:1px solid #999">尾页</a>
    <%
     }
     else
     {
    %>
    <a href="index.jsp?pageNum=<%=currPage+1 %>">下一页</a>
    <a href="index.jsp?pageNum=<%=totalPage %>">尾页</a>
    <% 
     }
    %>
  </body>
</html>

 

 

 

 


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值