首页 > 代码库 > 黑马day13 分页思路&实现

黑马day13 分页思路&实现

分页的总体思想:

分页包含什么:

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.数据库中使用limitkeyword进行查询

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="http://www.mamicode.com/${param.name }"/>
  	性别:<input type="radio" name="gender" value="http://www.mamicode.com/男"
  		<c:if test="${param.gender==‘男‘}">checked="checked"</c:if>
  	/>男
    <input type="radio" name="gender" value="http://www.mamicode.com/女"
    	<c:if test="${param.gender==‘女‘}">checked="checked"</c:if>
    />女
            客户类型:
    <select name="type">
    	<option value=""/>
		<option value="http://www.mamicode.com/钻石用户"
			<c:if test="${param.type==‘钻石用户‘}">selected="selected"</c:if>
		>钻石用户</option>
		<option  value="http://www.mamicode.com/黄金用户"
			<c:if test="${param.type==‘黄金用户‘}">selected="selected"</c:if>
		>黄金用户</option>
		<option  value="http://www.mamicode.com/普通用户"
			<c:if test="${param.type==‘普通用户‘}">selected="selected"</c:if>
		>普通用户</option>
    </select>
    <input type="submit" value="http://www.mamicode.com/条件查询">
  </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="http://www.mamicode.com/${li.id}"/></td>
    			<td><c:out value="http://www.mamicode.com/${li.name}"></c:out></td>
    			<td><c:out value="http://www.mamicode.com/${li.gender}"></c:out></td>
    			<td><c:out value="http://www.mamicode.com/${li.birthday }"></c:out></td>
    			<td><c:out value="http://www.mamicode.com/${li.cellphone }"></c:out></td>
    			<td><c:out value="http://www.mamicode.com/${li.email }"></c:out></td>
    			<td><c:out value="http://www.mamicode.com/${li.preference }"></c:out></td>
    			<td><c:out value="http://www.mamicode.com/${li.type }"></c:out></td>
    			<td><c:out value="http://www.mamicode.com/${li.description }"></c:out></td>
    			<td><a href="http://www.mamicode.com/${pageContext.request.contextPath }/servlet/CustomerInfoServlet?id=${li.id}">改动</a></td>
    			<td><a href="http://www.mamicode.com/${pageContext.request.contextPath }/servlet/DeleteCustomerServlet?id=${li.id}">删除</a></td>
    	</tr>
    	</c:forEach>
    </table>
    <input type="submit" value="http://www.mamicode.com/批量删除"/>
    </form>
    </div>
     ${requestScope.page.countrow }条数据
     共${requestScope.page.countpage} 页
    
    <a href="http://www.mamicode.com/${pageContext.request.contextPath }/servlet/PageCustomerServlet?thispage=${page.firstpage}">首页</a>
     <a href="http://www.mamicode.com/${pageContext.request.contextPath }/servlet/PageCustomerServlet?thispage=${page.prepage}">上一页</a>
     <!-- 分页逻辑 -->
     <c:if test="${page.countpage<=5}">
  		<c:set var="begin" value="http://www.mamicode.com/1" scope="page"></c:set>
  		<c:set var="end" value="http://www.mamicode.com/${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="http://www.mamicode.com/1" scope="page"></c:set>
  			<c:set var="end" value="http://www.mamicode.com/5" scope="page"></c:set>
  		</c:when>
  		<c:when test="${page.thispage>=page.countpage-2}">
  			<c:set var="begin" value="http://www.mamicode.com/${page.countpage-4}" scope="page"></c:set>
  			<c:set var="end" value="http://www.mamicode.com/${page.countpage}" scope="page"></c:set>
  		</c:when>
  		<c:otherwise>
  			<c:set var="begin" value="http://www.mamicode.com/${page.thispage-2}" scope="page"></c:set>
  			<c:set var="end" value="http://www.mamicode.com/${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="http://www.mamicode.com/${pageContext.request.contextPath }/servlet/PageCustomerServlet?

thispage=${i}"><font color="red">${i}</font></a> </c:if> </c:forEach> <!-- 分页逻辑结束 --> <a href="http://www.mamicode.com/${pageContext.request.contextPath }/servlet/PageCustomerServlet?thispage=${page.nextpage}">下一页</a> <a href="http://www.mamicode.com/${pageContext.request.contextPath }/servlet/PageCustomerServlet?thispage=${page.lastpage}">尾页</a> </body><br> <a href="http://www.mamicode.com/${pageContext.request.contextPath }/">返回到主页</a> </html>


分页伪代码:

技术分享
6.分页展示:

技术分享



黑马day13 分页思路&amp;实现