javabean+jsp+DAO實現分頁

來源:互聯網
上載者:User
PageBean.java

package com.servlet;
import java.util.*;
public class PageBean {
  private List arrayList;//存放總資料
  // 分頁資訊定義
  private int totalRows = 0; // 總資料數
  private int pageRecorders = 20;// 每頁顯示記錄數
  private int totalPages = 0; // 總頁數
  private int pageStartRow = 0;// 每頁的起始數
  private int pageEndRow = 0; // 每頁顯示資料的終止數
  private int currentPage = 1;// 當前頁
  private boolean hasNextPage = false; // 是否有下一頁
  private boolean hasPreviousPage = false; // 是否有前一頁
  public int getCurrentPage() {
   return currentPage;
  }
  public void setCurrentPage(int currentPage) {
   this.currentPage = currentPage;
  }
  public boolean isHasNextPage() {
   return hasNextPage;
  }
  public void setHasNextPage(boolean hasNextPage) {
   this.hasNextPage = hasNextPage;
  }
  public boolean isHasPreviousPage() {
   return hasPreviousPage;
  }
  public void setHasPreviousPage(boolean hasPreviousPage) {
   this.hasPreviousPage = hasPreviousPage;
  }
  public int getPageEndRow() {
   return pageEndRow;
  }
  public void setPageEndRow(int pageEndRow) {
   this.pageEndRow = pageEndRow;
  }
  public int getPageRecorders() {
   return pageRecorders;
  }
  public void setPageRecorders(int pageRecorders) {
   this.pageRecorders = pageRecorders;
  }
  public int getPageStartRow() {
   return pageStartRow;
  }
  public void setPageStartRow(int pageStartRow) {
   this.pageStartRow = pageStartRow;
  }
  public int getTotalPages() {
   return totalPages;
  }
  public void setTotalPages(int totalPages) {
   this.totalPages = totalPages;
  }
  public int getTotalRows() {
   return totalRows;
  }
  public void setTotalRows(int totalRows) {
   this.totalRows = totalRows;
  }
  
  public PageBean(List arrayList, int pageRecorders) {
   this.arrayList = arrayList;
   this.pageRecorders = pageRecorders;
   totalRows = arrayList.size();// 獲得總資料數
   // 獲得總頁數
   if ((totalRows % pageRecorders) == 0) {
    totalPages = totalRows / pageRecorders;
   } else {
    totalPages = totalRows / pageRecorders + 1;
   }
   totalPages = (totalPages == 0) ? 1 : totalPages;
   // 設定第一頁開始位置和結束位置
   if (totalRows < pageRecorders) {
    this.pageStartRow = 0;
    this.pageEndRow = totalRows;
   } else {
    this.pageStartRow = 0;
    this.pageEndRow = pageRecorders;
   }
   // 是否有下一頁
   if (currentPage >= totalPages) {
    hasNextPage = false;
   } else {
    hasNextPage = true;
   }
  }
  // 轉到頁面
  public List getPageList(int currentPage) {
   //當前頁小於1
   if (currentPage <= 0) {
    currentPage = 1;
   }
   //當前頁大於總頁數
   if (currentPage >= totalPages) {
    currentPage = totalPages;
    hasNextPage = false;
   } else {
    hasNextPage = true;
   }
   //當前頁大於1
   if (currentPage > 1) {
    hasPreviousPage = true;
   } else {
    hasPreviousPage = false;
   }
   //判斷是否為最後一頁
   if (currentPage * pageRecorders < totalRows) {
    pageEndRow = currentPage * pageRecorders;
    pageStartRow = pageEndRow - pageRecorders;
   } else {
    pageEndRow = totalRows;
    pageStartRow = pageRecorders * (totalPages - 1);
   }
   List<Object> pagelist = new ArrayList<Object>();
   for (int i = pageStartRow; i < pageEndRow; i++) {
    pagelist.add(arrayList.get(i));
   }
   return pagelist;
  }
}

MyJsp.jsp

<%@ page c%>
<%@ page import="java.util.*" %>
<%@ page import="com.vo.*"%>
<%@ page import="com.servlet.PageBean"%>
<jsp:useBean id="right" scope="request" class="com.factory.Factory"/>
<html>
  <head>
  <title>熊貓眼工作室</title>
  <link rel="stylesheet" href="css/shop.css" type="text/css">
</head>
<body>
<%!
int currentPage=1;
int Recorders= 4;
%>
<%
  if(request.getParameter("page")==null||request.getParameter("page")=="")
  {
  currentPage=1;
  }
  else
  {
  currentPage=Integer.parseInt(request.getParameter("page"));
  }
  List shoplist=right.getShopInstance().queryAll();//資料庫查詢的資料
  PageBean pages=new PageBean(shoplist,Recorders);//傳入要分頁的資料和每頁的記錄數
  pages.setCurrentPage(currentPage);//設定當前頁
  List all=pages.getPageList(currentPage);//調用當前頁的資料
  Iterator cart=all.iterator();//迭代
  while(cart.hasNext())//輸出資料
      {
          ShopVo bv = (ShopVo)cart.next() ;
       String name=bv.getGoodsname();
%>
<%=name %>
<%}%>
<p>
<!-- 分頁導航條 -->
<form name="form1" method="post" action="MyJsp.jsp">
共<%=pages.getTotalRows() %>條 <%=Recorders %>條/頁   第<%=pages.getCurrentPage() %>頁/共<%=pages.getTotalPages() %>頁
<%if(pages.getCurrentPage()<=1){ %>
首頁
<%}else{ %>
<a href="MyJsp.jsp?page=1">首頁</a>
<%} %>
<%if(pages.isHasPreviousPage()){ %>
<a href="MyJsp.jsp?page=<%=currentPage-1 %>">上一頁</a>
<%}else{ %>
上一頁
<%}%>
<%if(pages.isHasNextPage()) {%>
<a href="MyJsp.jsp?page=<%=currentPage+1 %>">下一頁</a>
<%}else{ %>
下一頁
<%}%>
<%if(pages.getCurrentPage()<pages.getTotalPages()){ %>
<a href="MyJsp.jsp?page=<%=pages.getTotalPages() %>">尾頁</a>
<% }else{%>
尾頁
<%} %>
<select name="page">
  <%for(int i=1;i<=pages.getTotalPages();i++)
  {%>
  <option value="<%=i%>" <%if(i==pages.getCurrentPage()){ out.println("selected");} %>><%=i%></option>
<%}%>
</select>
<input type="submit" name="Submit" value="跳到">
</form>
</p>
</body>
</html>

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.