分页显⽰——(⼀)
1.思路介绍:
  先定义⼀个封装分页数据的泛型⼯具类,控制层中通过前台传的页数每页的数量向service要page对象,service层调⽤dao返回总数并调⽤dao取出数据(limit关键字),dao层从数据库中取出service层所需要的数据,并提交给service层。这样就达到分页的效果。数据封装在pageBean的list中
2.PageBean⼯具类(泛型类)
package utils;
/**
* 分页⼯具类
*/
import java.util.ArrayList;
import java.util.List;
public class PageBean<T> {
//当前页
private int currentPage;
//当前页显⽰的条数
private int currentCount;
//总条数
private int totalCount;
//总页数
private int totalPage;
//每页显⽰的数据
private List<T> productList = new ArrayList<T>();
public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public int getCurrentCount() {
return currentCount;
}
public void setCurrentCount(int currentCount) {
this.currentCount = currentCount;
}
public int getTotalCount() {
return totalCount;
}
public void setTotalCount(int totalCount) {
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
}
public List<T> getProductList() {
return productList;
}
public void setProductList(List<T> productList) {
this.productList = productList;
}
}
3 . 控制层中通过前台传的页数与每页的数量向service要page对象package web;
import java.io.IOException;
分页预览
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import daomain.User;
import service.UserService;
import utils.PageBean;
@WebServlet("/userfenye")
public class Userfenye extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
UserService service = new UserService();
//模拟当前是第⼀页,如果刚进来显⽰第⼀页数据
String currentPageStr = Parameter("currentPage");
if(currentPageStr==null) currentPageStr="1";
int currentPage = Integer.parseInt(currentPageStr);
//认为每页显⽰12条,后期可以根据⽤户输⼊设置每页显⽰的页数
int currentCount = 8;
PageBean<User> pageBean = null;
try {
pageBean = service.findPageBean(currentPage,currentCount);
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("pageBean", pageBean);
System.out.ProductList());
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
4.  service层调⽤dao返回总数并调⽤dao取出数据(limit关键字) package service;
import java.sql.SQLException;
import java.util.List;
import daomain.User;
import utils.PageBean;
public class UserService {
private UserDao userDao;
//谁调⽤服务谁创建dao
public UserService() {
userDao = new UserDao();
}
public PageBean<User> findPageBean(int currentPage, int currentCount) throws SQLException { //⽬的:就是想办法封装⼀个PageBean 并返回
PageBean<User> pageBean = new PageBean();
//1、当前页private int currentPage;
pageBean.setCurrentPage(currentPage);
//2、当前页显⽰的条数private int currentCount;
pageBean.setCurrentCount(currentCount);
//3、总条数private int totalCount;
int totalCount = TotalCount();
pageBean.setTotalCount(totalCount);
//4、总页数private int totalPage;
/*
* 总条数当前页显⽰的条数总页数
* 10        4                3
* 11        4                3
* 12        4                3
* 13        4                4
*
* 公式:总页数=il(总条数/当前显⽰的条数)
*
*/
int totalPage = (int) il(1.0*totalCount/currentCount);
pageBean.setTotalPage(totalPage);
//5、每页显⽰的数据private List<T> productList = new ArrayList<T>();
/
*
* 页数与limit起始索引的关系
* 例如每页显⽰4条
* 页数其实索引每页显⽰条数
* 1        0            4
* 2        4            4
* 3        8            4
* 4        12            4
*
* 索引index = (当前页数-1)*每页显⽰的条数
*
*/
int index = (currentPage-1)*currentCount;
List<User> userList = userDao.findUserListForPageBean(index,currentCount);
pageBean.setProductList(userList);
System.out.println(userList);
return pageBean;
}
}
5.dao层从数据库中取出service层所需要的数据,并提交给service层
(如果是最后⼀页的话,limit超出后只会取剩下的)
package dao;
import java.sql.SQLException;
import org.apachemons.dbutils.QueryRunner;
import org.apachemons.dbutils.handlers.BeanHandler;
import org.apachemons.dbutils.handlers.BeanListHandler;
import org.apachemons.dbutils.handlers.ScalarHandler;
import daomain.User;
import utils.DataSourceUtils;
/**
*
* @author: qlq
* @date :  2017年7⽉6⽇上午11:57:56
* @description:chuli yonghu xinxi
*/
public class UserDao {
//获得全部的⽤户条数,返回⼀个整数
public int getTotalCount() throws SQLException {
QueryRunner runner = new DataSource());
String sql = "select count(*) from user";
Long query = (Long) runner.query(sql, new ScalarHandler());
return query.intValue();
}
public List<User> findUserListForPageBean(int index, int currentCount) throws SQLException {        QueryRunner runner = new DataSource());
String sql = "select * from user limit ?,?";
return runner.query(sql, new BeanListHandler<User>(User.class), index,currentCount);
}
}
6.使⽤分页插件在页⾯进⾏显⽰
1.  bootstrap分页插件使⽤简单,超过⼗个以上的页码显⽰是个问题
1.1  使⽤规则:
⾸先需要引⼊bootstrap的css和Jquery等三个⽂件
1.2  页⾯中使⽤显⽰分页标签
<%--items⽤于迭代的变量,var代表每次迭代之后存放名字 --%>
<table border="1px" cellspacing="0px" align="center" cellpadding="0px">
<tr>
<th>id</th>
<th>姓名:</th>
<th>图⽚</th>
<th>密码</th>
</tr>
<c:forEach items="${pageBean.productList }" var="user">
<tr>
<th>${user.id}</th>
<th>${user.name}</th>
<th><img alt=""
src="${tPath }/${user.picture}"
></th>
<th>${user.password}</th>
</tr>
</table>
<!--分页 -->
<div >
<ul class="pagination" >
<!-- 上⼀页 -->
<!-- 判断当前页是否是第⼀页 -->
<c:if test="${pageBean.currentPage==1 }">
<li class="disabled"><a href="javascript:void(0);"
aria-label="Previous"><span aria-hidden="true">«</span>
</a></li>
</c:if>
<c:if test="${pageBean.currentPage!=1 }">
<li><a
href="${tPath }/userfenye?currentPage=${pageBean.currentPage-1}"                        aria-label="Previous"><span aria-hidden="true">«</span>
</a></li>
</c:if>
<c:forEach begin="1" end="${alPage }" var="page">
<!-- 判断当前页 -->
<c:if test="${pageBean.currentPage==page }">
<li class="active"><a href="javascript:void(0);">${page}</a></li>
</c:if>
<c:if test="${pageBean.currentPage!=page }">
<li><a
href="${tPath }/userfenye?currentPage=${page}">${page}</a></li> </c:if>
</c:forEach>
<!-- 判断当前页是否是最后⼀页 -->
<c:if test="${pageBean.currentPage==alPage }">
<li class="disabled"><a href="javascript:void(0);"
aria-label="Next"><span aria-hidden="true">»</span>
</a></li>
</c:if>
<c:if test="${pageBean.currentPage!=alPage }">
<li><a
href="${tPath }/userfenye?currentPage=${pageBean.currentPage+1}"                        aria-label="Next"><span aria-hidden="true">»</span>
</a></li>
</c:if>
</ul>
</div>
<!-- 分页结束 -->
1.3  效果