黑马day13 分页思路&实现

Posted liguangsunls

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了黑马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="${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.分页展示:

技术分享









以上是关于黑马day13 分页思路&amp;实现的主要内容,如果未能解决你的问题,请参考以下文章

黑马day16 jquery&amp;层次选择器

黑马day15 文件上传&amp;apche的工具包

黑马旅游网:用户登录,导航条,线路分页——Java就业班day47

13th day

操作系统-3.6-内存(虚拟内存&&请求分页管理详解)超详解

Day456.排序与分页&多表查询 -mysql