以下是java的pageBean通用分页工具类,适用于在自定义的分页插件中使用,通常在ssm或ssh框架中,一般使用例如layPage等分页插件来做分页的,这里使用pageBean分页工具类,是想让各位知道js分页插件的计算逻辑,它实际上就是将在pagebean中的计算让js来实现了,这样我们只需要告诉js分页插件3个参数就可以实现分页了,例如总条数,当前页,以及每页显示的条数。
1:pageBean分页工具类的实现代码,了解分页的计算逻辑。
import java.util.List;
/**
* 分页对象。用于封装分页相关的数据
* @author APPle
*
*/
public class PageBean<T>{
private List<T> data; //当前页数据(查询数据库得到)
private int firstPage; //首页
private int prePage; // 上一页
private int nextPage;// 下一页
private int totalPage; // 末页/总页数
private int curPage; //当前页
private int totalCount; //总记录数(查询数据库得到)
private int pageSize = 5;//每页记录数
public List<T> getData() {
return data;
}
public void setData(List<T> data) {
this.data = data;
}
//首页
public int getFirstPage() {
return 1;
}
public void setFirstPage(int firstPage) {
this.firstPage = firstPage;
}
//上一页: 算法:如果当前页是首页,则为1,否则为当前页-1
public int getPrePage() {
return this.getCurPage()==this.getFirstPage()
? 1
: this.getCurPage()-1;
}
public void setPrePage(int prePage) {
this.prePage = prePage;
}
//下一页:算法:如果当前页是末页,则为末页,否则当前页+1
public int getNextPage() {
return this.getCurPage()==this.getTotalPage()
?this.getTotalPage()
: this.getCurPage()+1;
}
public void setNextPage(int nextPage) {
this.nextPage = nextPage;
}
//总页数、末页: 算法:如果总记录数%每页记录数==0 ? 总记录数/每页记录数 :总记录数/每页记录数+1
public int getTotalPage() {
return this.getTotalCount()%this.getPageSize()==0
? this.getTotalCount()/this.getPageSize()
: this.getTotalCount()/this.getPageSize()+1;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public int getCurPage() {
return curPage;
}
public void setCurPage(int curPage) {
this.curPage = curPage;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}
//每页记录数
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
}2:以查询Employee员工数据来说,EmpDao类是从数据库中分页查询数据的,代码如下。
import com.baidu.entity.Employee;
import com.baidu.util.C3P0Util;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
/**
* 员工dao
* @author APPle
*
*/
public class EmpDao {
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
/**
* 查询当前页数据
* @param curPage 当前页数
* @param pageSize 每页条数
* @return
*/
public List<Employee> queryData(int curPage,int pageSize){
try {
String sql = "SELECT * FROM employee LIMIT ?,?";
return qr.query(sql, new BeanListHandler(Employee.class),(curPage-1)*pageSize,pageSize);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
/**
* 查询总记录数
* @return
*/
public int queryCount(){
try {
String sql = "select count(*) from employee";
Long count = (Long)qr.query(sql, new ScalarHandler());
return count.intValue();
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException(e);
}
}
}3:EmpService是service层,可以从servlet中获取三个必要的参数(总记录数)totalCount,(当前页)currenPage,(每页显示条数)pagesize,代码如下。
import com.baidu.dao.EmpDao;
import com.baidu.entity.Employee;
import com.baidu.entity.PageBean;
import java.util.List;
//员工业务
public class EmpService {
EmpDao empDao = new EmpDao();
/**
* 为了封装一个分页对象
* @param curPage
* @return
*/
public PageBean findPageBean(int curPage,int pageSize) {
// 1.创建PageBean对象
PageBean<Employee> pageBean = new PageBean<Employee>();
// 2.设置当前页数据
pageBean.setCurPage(curPage);
pageBean.setPageSize(pageSize);
// 3. 获取总记录数
pageBean.setTotalCount(empDao.queryCount());
// 4.获取当前页数据
List<Employee> emps = empDao.queryData(pageBean.getCurPage(),
pageBean.getPageSize());
pageBean.setData(emps);
return pageBean;
}
}4:servlet中接收jsp页面获取来的数据。
import com.baidu.entity.PageBean;
import com.baidu.service.EmpService;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
//分页显示员工
public class PageEmpServlet extends HttpServlet {
EmpService empService = new EmpService();
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 1 ===接收外部页面参数 : 当前页 (从用户页面接收参数: curPage参数)
String curPage = request.getParameter("curPage");
// 如果页面没有传递当前页,则默认为1
if (curPage == null || curPage.equals("")) {
curPage = "1";
}
String pageSize = request.getParameter("pageSize");
//如果没有传递每页条数,则默认为5
if (pageSize == null || pageSize.equals("")) {
pageSize = "5";
}
// 2 ===调用业务逻辑,获取封装好的PageBean对象
PageBean pageBean = empService.findPageBean(Integer.parseInt(curPage),Integer.parseInt(pageSize));
// 3 === 通过域对象共享到jsp
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/pageEmp.jsp").forward(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}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>
<title>分页显示员工</title>
</head>
<body>
<h3>分页显示</h3>
<table border="1" width="700px">
<tr>
<th>编号</th>
<th>姓名</th>
<th>性别</th>
<th>职位</th>
<th>邮箱</th>
<th>薪水</th>
</tr>
<c:forEach items="${pageBean.data}" var="emp">
<tr>
<td>${emp.id }</td>
<td>${emp.name }</td>
<td>${emp.gender }</td>
<td>${emp.title }</td>
<td>${emp.email }</td>
<td>${emp.salary }</td>
</tr>
</c:forEach>
<tr>
<td colspan="6">
<%--
如果当前页是首页,不显示 首页 上一页的链接
如果当前页是末页,不显示 下一页 末页的链接
--%>
<c:choose>
<c:when test="${pageBean.curPage==1 }">
首页 上一页
</c:when>
<c:otherwise>
<a href="${pageContext.request.contextPath }/PageEmpServlet?curPage=${pageBean.firstPage }">首页</a>
<a href="${pageContext.request.contextPath }/PageEmpServlet?curPage=${pageBean.prePage }">上一页</a>
</c:otherwise>
</c:choose>
<c:choose>
<c:when test="${pageBean.curPage==pageBean.totalPage }">
下一页 末页
</c:when>
<c:otherwise>
<a href="${pageContext.request.contextPath }/PageEmpServlet?curPage=${pageBean.nextPage }">下一页</a>
<a href="${pageContext.request.contextPath }/PageEmpServlet?curPage=${pageBean.totalPage }">末页</a>
</c:otherwise>
</c:choose>
当前第${pageBean.curPage }页/共${pageBean.totalPage }页,
共${pageBean.totalCount }条记录
每页<input type="text" id="pageSizeNo" size="2" name="pageSize" value="${pageBean.pageSize }" onblur="_chageSize()"/>条
跳转到第<input type="text" id="pageNo" name="pageNo" size="2" onblur="_chagePage()" value="${pageBean.curPage }"/>页
</td>
</tr>
</table>
<script type="text/javascript">
//切换页码
function _chagePage(){
//1.获取填入的页码
var pageSizeNo = document.getElementById("pageNo").value;
//校验输入: 只能是数字,且不能大于末页
var reg = /^[1-9][0-9]?$/;
if(!reg.test(pageSizeNo)){
alert("只能输入两位数字!");
document.getElementById("pageNo").value="1";
return;
}
//得到总页数
var totalPage = "${pageBean.totalPage}";
//判断
if(pageSizeNo>totalPage){
alert("不能大于总页数!");
document.getElementById("pageNo").value="1";
return;
}
//2.把页码发送到PageEmpServlet后台
window.location.href="${pageContext.request.contextPath }/PageEmpServlet?curPage="+pageSizeNo;
}
//改变每页显示条数
function _chageSize(){
var pageSizeNo = document.getElementById("pageSizeNo").value;
window.location.href="${pageContext.request.contextPath }/PageEmpServlet?pageSize="+pageSizeNo;
}
</script>
</body>
</html>