笔记:分页
核心工具类:PageBean
PageBean核心三件套,index:索引页;size:每一页的大小;totalCount:总共多少条记录。
package com.bjsxt.util;
import java.util.List;
/**
* 分页的三个基本属性
* 1.每页几条记录size 可以有默认值5
* 2.当前页号 index 可以有默认值1
* 3.记录总数totalCount:不可能有默认值,需要查询数据库获取真正的记录总数
*
* 4.一共多少页 :totalPageCount=totalCount/size+1 23/5
* 5 30 31 32 33 34 35
* 5.上一页 index-1 当前页1,上一页1
* 6.下一页 index+1 当前页是最后一页 下一页:还是最后一页
*
* 扩展
* 分页Bean还可以放要查询的数据 protected List<T> list;
* 分页Bean还可以放页码列表 [1] 2 3 4 5 private int[] numbers;
*
* @author Administrator
*
* @param <T>
*/
public class PageBean<T> {
private int size = 5;//每页显示记录 //
private int index = 1;// 当前页号
private int totalCount = 0;// 记录总数 ok
private int totalPageCount = 1;// 总页数 ok
private int[] numbers;//展示页数集合 //ok
protected List<T> list;//要显示到页面的数据集
/**
* 得到开始记录
* @return
*/
public int getStartRow() {
return (index - 1) * size;
}
/**
* 得到结束记录
* @return
*/
public int getEndRow() {
return index * size;
}
/**
* @return Returns the size.
*/
public int getSize() {
return size;
}
/**
* @param size
* The size to set.
*/
public void setSize(int size) {
if (size > 0) {
this.size = size;
}
}
/**
* @return Returns the currentPageNo.
*/
public int getIndex() {
if (totalPageCount == 0) {
return 0;
}
return index;
}
/**
* @param currentPageNo
* The currentPageNo to set.
*/
public void setIndex(int index) {
if (index > 0) {
this.index = index;
}
}
/**
* @return Returns the totalCount.
*/
public int getTotalCount() {
return totalCount;
}
/**
* @param totalCount
* The totalCount to set.
*/
public void setTotalCount(int totalCount) {
if (totalCount >= 0) {
this.totalCount = totalCount;
setTotalPageCountByRs();//根据总记录数计算总页�?
}
}
public int getTotalPageCount() {
return this.totalPageCount;
}
/**
* 根据总记录数计算总页�?
* 5
* 20 4
* 23 5
*/
private void setTotalPageCountByRs() {
if (this.size > 0 && this.totalCount > 0 && this.totalCount % this.size == 0) {
this.totalPageCount = this.totalCount / this.size;
} else if (this.size > 0 && this.totalCount > 0 && this.totalCount % this.size > 0) {
this.totalPageCount = (this.totalCount / this.size) + 1;
} else {
this.totalPageCount = 0;
}
setNumbers(totalPageCount);//获取展示页数集合
}
public int[] getNumbers() {
return numbers;
}
/**
* 设置显示页数集合
*
* 默认显示10个页码
* 41 42 43 44 [45 ] 46 47 48 49 50
*
*
* [1] 2 3 4 5 6 7 8 9 10
*
* 41 42 43 44 45 46 47 [48] 49 50
* @param totalPageCount
*/
public void setNumbers(int totalPageCount) {
if(totalPageCount>0){
//!.当前数组的长度
int[] numbers = new int[totalPageCount>10?10:totalPageCount];//页面要显示的页数集合
int k =0;
//
//1.总页数长度<10 1 2 3 4 .... 7
//2.总页数长度>=10
// 当前页<=6 1 2 3 4 10
// 当前页>=总页数-5 ......12 13 14 15
// 其他 5 6 7 8 9 当前页(10) 10 11 12 13
for(int i = 0;i < totalPageCount;i++){
//保证当前页为集合的中�?
if((i>=index- (numbers.length/2+1) || i >= totalPageCount-numbers.length) && k<numbers.length){
numbers[k] = i+1;
k++;
}else if(k>=numbers.length){
break;
}
}
this.numbers = numbers;
}
}
public void setNumbers(int[] numbers) {
this.numbers = numbers;
}
public List<T> getList() {
return list;
}
public void setList(List<T> list) {
this.list = list;
}
/*
public static int getTotalPageCount(int iTotalRecordCount, int iPageSize) {
if (iPageSize == 0) {
return 0;
} else {
return (iTotalRecordCount % iPageSize) == 0 ? (iTotalRecordCount / iPageSize) : (iTotalRecordCount / iPageSize) + 1;
}
}*/
}
这里是一段JAVA代码分页的代码
package com.bjsxt.servlet;
import com.bjsxt.entity.Student;
import com.bjsxt.service.StudentService;
import com.bjsxt.service.impl.StudentServiceImpl;
import com.bjsxt.util.PageBean;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
public class ShowAllServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//解决post表单的中文乱码问题
request.setCharacterEncoding("utf-8");
//1.接收来自页面的请求页码、每页记录数、查询条件
String sindex = request.getParameter("index");//null "aa"
int index = 1;
try {
index = Integer.parseInt(sindex);
} catch (NumberFormatException e) {
e.printStackTrace();
}
String ssize = request.getParameter("size");//null "aa"
int size = 5;
try {
size = Integer.parseInt(ssize);
} catch (NumberFormatException e) {
e.printStackTrace();
}
String name = request.getParameter("name");
String sminScore = request.getParameter("minScore");
double minScore = 0;
try{
if(sminScore != null){
minScore = Double.parseDouble(sminScore);
}
}catch (NumberFormatException e){
e.printStackTrace();
}
//2
PageBean<Student> pageBean = new PageBean<Student>();
pageBean.setIndex(index);
pageBean.setSize(size);
StudentService studentService = new StudentServiceImpl();
// List <Student> stuList =studentService.findAll();
studentService.find(pageBean,name,minScore);
request.setAttribute("pageBean", pageBean);//!!!!!!!
request.setAttribute("name",name);
request.setAttribute("minScore",sminScore);//
//3com.bjsxt
request.getRequestDispatcher("/jsp/showAll.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
this.doGet(request, response);
}
}
接下来的一段HTML代码。
<%--
Created by IntelliJ IDEA.
User: Administrator
Date: 2019/05/29
Time: 10:19
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>Title</title>
<base href="http://localhost:8080/stumgr/">
<script type="text/javascript">
function changeSize(size){
change(1,size);
}
function change (index,size) {
document.form1.index.value=index;
document.form1.size.value=size;
document.form1.submit();
// location.href="servlet/ShowAllServlet?index="+index+"&size="+size;
}
</script>
</head>
<body>
<hr>
<form action="servlet/ShowAllServlet" method="post" id="form1" name="form1">
<table align="center">
<tr>
<td>姓名</td>
<td>
<input type="text" name="name" value="${name}">
<input type="hidden" name="index" value="">
<input type="hidden" name="size" value="">
</td>
<td>分数>=</td>
<td><input type="text" name="minScore" value="${minScore}"></td>
<td><input type="submit" value="提交"></td>
</tr>
</table>
</form>
<hr>
<!-- 显示所有学生 /stumanager/ -->
<table align="center" border="1" width="60%">
<tr>
<th>学生 编号</th>
<th>学生姓名</th>
<th>学生年龄</th>
<th>学生成绩</th>
<th>vs.index</th>
<th>更新操作</th>
<th>删除操作</th>
</tr>
<c:forEach items="${studentPageBean.list}" var="stu" varStatus="vs">
<tr>
<td>${stu.id }</td>
<td>${stu.name }</td>
<td>${stu.age }</td>
<td>${stu.score }</td>
<td>${vs.index }</td>
<td><a href="/stumanager/servlet/StudentServlet?operate=preupdate&sid=${stu.id}">更新</a></td>
<td><a href="/stumanager/servlet/StudentServlet?operate=delete&sid=${stu.id}">删除</a></td>
</tr>
</c:forEach>
<tr>
<td colspan="11">
<c:if test="${studentPageBean.index==1}">
上一页
</c:if>
<c:if test="${studentPageBean.index!=1}">
<%--<a href="servlet/ShowAllServlet?index=${studentPageBean.index-1}">上一页</a>--%>
<a href="javascript:change(${studentPageBean.index-1},${studentPageBean.size})">上一页</a>
</c:if>
<c:if test="${studentPageBean.index==studentPageBean.totalPageCount}">
下一页
</c:if>
<c:if test="${studentPageBean.index!=studentPageBean.totalPageCount}">
<%--<a href="servlet/ShowAllServlet?index=${studentPageBean.index+1}">下一页</a>--%>
<a href="javascript:change(${studentPageBean.index+1},${studentPageBean.size})">下一页</a>
</c:if>
<c:forEach items="${studentPageBean.numbers}" var="i">
<c:if test="${studentPageBean.index==i}">
<%--[<a href="servlet/ShowAllServlet?index=${i}">${i}</a>]--%>
[<a href="javascript:change(${i},${studentPageBean.size})">${i}</a>]
</c:if>
<c:if test="${studentPageBean.index!=i}">
<a href="javascript:change(${i},${studentPageBean.size})">${i}</a>
</c:if>
</c:forEach>
<select onchange="changeSize(this.value)">
<c:forEach begin="5" end="25" step="5" var="i">
<c:if test="${studentPageBean.size==i}">
<option value="${i}" selected>${i}</option>
</c:if>
<c:if test="${studentPageBean.size!=i}">
<option value="${i}">${i}</option>
</c:if>
</c:forEach>
</select>
一共有${studentPageBean.totalPageCount}页
共有${studentPageBean.totalCount}条记录
</td>
</tr>
</table>
</body>
</html>