1. 程式人生 > >分頁思路實現

分頁思路實現

分頁的整體思想:

分頁包括什麼:

1.當前頁,每頁顯示的記錄數,總的記錄數,總的頁碼,集合List存放的是JavaBean,首頁, 尾頁,上一頁,下一頁

傳遞的引數:當前頁,每頁顯示的記錄數.這兩個本來就是有的。

資料庫中查詢的:總的記錄數,集合List存放的是JavaBean。這兩個是從資料庫中查詢的。

通過已知的屬性計算:首頁, 尾頁,上一頁,下一頁,總的頁碼 這些是通過計算得到了。

因此javaBean Page可以寫成:

package com.itheima.domain;

import java.util.List;

public class Page {
	private int thispage;//當前頁
	private int rowperpage;//每頁顯示的記錄數
	private int countrow;//總的記錄數
	private int countpage;//總的頁數
	private int firstpage;//首頁
	private int lastpage;//尾頁
	private int prepage;//上一頁
	private int nextpage;//下一頁
	private List<Customer> list;
	public int getThispage() {
		return thispage;
	}
	public void setThispage(int thispage) {
		this.thispage = thispage;
	}
	public int getRowperpage() {
		return rowperpage;
	}
	public void setRowperpage(int rowperpage) {
		this.rowperpage = rowperpage;
	}
	public int getCountrow() {
		return countrow;
	}
	public void setCountrow(int countrow) {
		this.countrow = countrow;
	}
	public int getCountpage() {
		return countpage;
	}
	public void setCountpage(int countpage) {
		this.countpage = countpage;
	}
	public int getFirstpage() {
		return firstpage;
	}
	public void setFirstpage(int firstpage) {
		this.firstpage = firstpage;
	}
	public int getLastpage() {
		return lastpage;
	}
	public void setLastpage(int lastpage) {
		this.lastpage = lastpage;
	}
	public int getPrepage() {
		return prepage;
	}
	public void setPrepage(int prepage) {
		this.prepage = prepage;
	}
	public int getNextpage() {
		return nextpage;
	}
	public void setNextpage(int nextpage) {
		this.nextpage = nextpage;
	}
	public List<Customer> getList() {
		return list;
	}
	public void setList(List<Customer> list) {
		this.list = list;
	}
}
2.首先要進行分頁需要在cn.itheima.web包中寫一個分頁的Servlet,PageCustomerServlet

這個類的主要步驟是:

(1).得到傳遞的引數預設是首頁

(2).每頁顯示的記錄數,這裡預設的是5

(3).呼叫service中分頁的函式返回Page

(4).將Page放到request域中

(5).重定向到要實現分頁的jsp進行顯示分頁

package com.itheima.web;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itheima.domain.Customer;
import com.itheima.domain.Page;
import com.itheima.factory.BasicFactory;
import com.itheima.service.CustomerService;

public class PageCustomerServlet extends HttpServlet {
	CustomerService service =BasicFactory.getFactory().getInstance(CustomerService.class);
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1.獲取當前頁每頁顯示的記錄
		int thispage=Integer.parseInt(request.getParameter("thispage"));
		int rowperpage=5;
		//2.呼叫service的方法
		Page page=service.findCustomerByPage(thispage,rowperpage);
		request.setAttribute("page", page);
		//3.請求轉發到pageList.jsp
		request.getRequestDispatcher("/pageList.jsp").forward(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
3.service實現類程式碼主要完成的功能:

計算Page中的屬性,設定到Page中

public Page findCustomerByPage(int thispage, int rowperpage) {
		Page page=new Page();
		//設定當前頁
		page.setThispage(thispage);
		//每頁顯示的記錄數 預設是5條
		page.setRowperpage(rowperpage);
		//查詢資料庫一共有多少行
		int countrow=dao.getCount();
		page.setCountrow(countrow);
		//一共有多少頁
		int countpage=(countrow-1)/rowperpage+1;
		page.setCountpage(countpage);
		//設定首頁
		page.setFirstpage(1);
		//設定尾頁
		page.setLastpage(countpage);
		//前一頁
		page.setPrepage(thispage==1?1:thispage-1);
		//下一頁
		page.setNextpage(thispage==countpage?countpage:thispage+1);
		//
		List<Customer> list=dao.getCustomerByPage((thispage-1)*rowperpage,rowperpage);
		page.setList(list);
		return page;
	}
4.資料庫中使用limit關鍵字進行查詢
public List<Customer> getCustomerByPage(int from, int count) {
		String sql="select * from customer limit ?,?";
		QueryRunner runner=new QueryRunner(DataSourceUtil.getSource());
		try {
			return runner.query(sql, new BeanListHandler<Customer>(Customer.class),from,count);
		} catch (SQLException e) {
			e.printStackTrace();
			throw new RuntimeException();
		}
	}
5.jsp頁面中顯示:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
  <script type="text/javascript">
  	function selectAll(all){
  		var del=document.getElementsByName("del");
  		for(var i=0;i<del.length;i++){
  		del[i].checked=all.checked;
  		}
  	}
  </script>
    <title></title>
    
	<meta http-equiv=" pragma" content="no-cache">
	<meta http-equiv="cache-control" content="no-cache">
	<meta http-equiv="expires" content="0">    
	
  </head>
  
 <body style="text-align: center">
  <h1>客戶管理系統_顯示列表</h1><hr>
  <div align="center">
  <form action="${pageContext.request.contextPath }/servlet/FindCustomerByConditionServlet" method="post">
  	姓名:<input type="text" name="name" value="${param.name }"/>
  	性別:<input type="radio" name="gender" value="男"
  		<c:if test="${param.gender=='男'}">checked="checked"</c:if>
  	/>男
    <input type="radio" name="gender" value="女"
    	<c:if test="${param.gender=='女'}">checked="checked"</c:if>
    />女
            客戶型別:
    <select name="type">
    	<option value=""/>
		<option value="鑽石使用者"
			<c:if test="${param.type=='鑽石使用者'}">selected="selected"</c:if>
		>鑽石使用者</option>
		<option  value="黃金使用者"
			<c:if test="${param.type=='黃金使用者'}">selected="selected"</c:if>
		>黃金使用者</option>
		<option  value="普通使用者"
			<c:if test="${param.type=='普通使用者'}">selected="selected"</c:if>
		>普通使用者</option>
    </select>
    <input type="submit" value="條件查詢">
  </form>
  <form action="${pageContext.request.contextPath}/servlet/BatchDeleteCustomer" method="post">
    <table border="1" width="100%" cellpadding="0" cellspacing="0" style="text-align: center">
    	<tr>
    		<th><input type="checkbox" onclick="selectAll(this)">全選</th>
    		<th>客戶姓名</th>
    		<th>客戶性別</th>
    		<th>客戶生日</th>
    		<th>客戶電話</th>
    		<th>電子郵箱</th>
    		<th>客戶愛好</th>
    		<th>客戶型別</th>
    		<th>詳細描述</th>
    		<th colspan="2">操作型別</th>
    	</tr>
    	<c:forEach items="${requestScope.page.list}" var="li">
    	<tr>
    			<td><input type="checkbox" name="del" value="${li.id}"/></td>
    			<td><c:out value="${li.name}"></c:out></td>
    			<td><c:out value="${li.gender}"></c:out></td>
    			<td><c:out value="${li.birthday }"></c:out></td>
    			<td><c:out value="${li.cellphone }"></c:out></td>
    			<td><c:out value="${li.email }"></c:out></td>
    			<td><c:out value="${li.preference }"></c:out></td>
    			<td><c:out value="${li.type }"></c:out></td>
    			<td><c:out value="${li.description }"></c:out></td>
    			<td><a href="${pageContext.request.contextPath }/servlet/CustomerInfoServlet?id=${li.id}">修改</a></td>
    			<td><a href="${pageContext.request.contextPath }/servlet/DeleteCustomerServlet?id=${li.id}">刪除</a></td>
    	</tr>
    	</c:forEach>
    </table>
    <input type="submit" value="批量刪除"/>
    </form>
    </div>
     ${requestScope.page.countrow }條資料
     共${requestScope.page.countpage} 頁
    
    <a href="${pageContext.request.contextPath }/servlet/PageCustomerServlet?thispage=${page.firstpage}">首頁</a>
     <a href="${pageContext.request.contextPath }/servlet/PageCustomerServlet?thispage=${page.prepage}">上一頁</a>
     <!-- 分頁邏輯 -->
     <c:if test="${page.countpage<=5}">
  		<c:set var="begin" value="1" scope="page"></c:set>
  		<c:set var="end" value="${page.countpage}" scope="page"></c:set>
  	</c:if>
  	 
  	 <c:if test="${page.countpage>5}">
  	 <c:choose>
  		<c:when test="${page.thispage<=3}">
  			<c:set var="begin" value="1" scope="page"></c:set>
  			<c:set var="end" value="5" scope="page"></c:set>
  		</c:when>
  		<c:when test="${page.thispage>=page.countpage-2}">
  			<c:set var="begin" value="${page.countpage-4}" scope="page"></c:set>
  			<c:set var="end" value="${page.countpage}" scope="page"></c:set>
  		</c:when>
  		<c:otherwise>
  			<c:set var="begin" value="${page.thispage-2}" scope="page"></c:set>
  			<c:set var="end" value="${page.thispage+2}" scope="page"></c:set>
  		</c:otherwise>
  		</c:choose>
  	</c:if>
  	
  	<c:forEach begin="${begin}" end="${end}" step="1" var="i">
  		<c:if test="${i==page.thispage}"><!-- 當前頁就不是超連結 -->
  			<font color="black">${i}</font>
  		</c:if>
  		<c:if test="${i!=page.thispage}"><!-- 非當前頁就是超連結 -->
  			<a href="${pageContext.request.contextPath }/servlet/PageCustomerServlet?thispage=${i}"><font color="red">${i}</font></a>
  		</c:if>
  	</c:forEach>
  	<!-- 分頁邏輯結束 -->
    <a href="${pageContext.request.contextPath }/servlet/PageCustomerServlet?thispage=${page.nextpage}">下一頁</a>
    <a href="${pageContext.request.contextPath }/servlet/PageCustomerServlet?thispage=${page.lastpage}">尾頁</a>
  </body><br>
   <a href="${pageContext.request.contextPath }/">返回到主頁</a>
</html>

分頁虛擬碼:


6.分頁展示: