Fork me on GitHub

servlet+jsp实现分页功能

1.分页万精油jsp页面,只需要传入pagebean对象

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8" %>
<div class="dataTables_paginate paging_simple_numbers"
     id="DataTables_Table_0_paginate">
    <a class="paginate_button previous disabled"
       aria-controls="DataTables_Table_0" data-dt-idx="0" tabindex="0"
       id="DataTables_Table_0_previous" href="<c:url value="${page.url}&currentPage=1"/>">首页</a>
    <c:if test="${page.currentPage > 1 }">
        <a class="paginate_button previous disabled"
           aria-controls="DataTables_Table_0" data-dt-idx="0" tabindex="0"
           id="DataTables_Table_0_previous"
           href="<c:url value='${page.url}&currentPage=${page.currentPage - 1 }'/>">上一页</a>
    </c:if>

    <c:choose>
        <c:when test="${page.totalPage <= 10 }">
            <c:set var="begin" value="1"/>
            <c:set var="end" value="${page.totalPage }"/>
        </c:when>
        <c:otherwise>
            <c:set var="begin" value="${page.currentPage-4 }"/>
            <c:set var="end" value="${page.currentPage+5 }"/>
            <c:choose>
                <c:when test="${begin < 1 }">
                    <c:set var="begin" value="1"/>
                    <c:set var="end" value="10"/>
                </c:when>
                <c:when test="${end > page.totalPage }">
                    <c:set var="begin" value="${page.totalPage-9 }"/>
                    <c:set var="end" value="${page.totalPage }"/>
                </c:when>
            </c:choose>
        </c:otherwise>
    </c:choose>


    <c:forEach begin="${begin}" end="${end}" var="i">
        <c:choose>

            <c:when test="${i == page.currentPage}"><span style="color: red">${i}</span></c:when>

            <c:otherwise>
                    <span><a
                            class="paginate_button current" aria-controls="DataTables_Table_0"
                            data-dt-idx="1" tabindex="0"
                            href="<c:url value='${page.url}&currentPage=${i}'/>"
                    > ${i}</a> </span>

            </c:otherwise>
        </c:choose>
    </c:forEach>


    <c:if test="${page.currentPage < page.totalPage }">
        <a class="paginate_button next disabled"
           aria-controls="DataTables_Table_0" data-dt-idx="2" tabindex="0"
           id="DataTables_Table_0_next"
           href="<c:url value='${page.url}&currentPage=${page.currentPage + 1 }'/>"
        >下一页</a>
    </c:if>
    <a class="paginate_button next disabled"
       aria-controls="DataTables_Table_0" data-dt-idx="2" tabindex="0"
       id="DataTables_Table_0_next"
       href="<c:url value='${page.url}&currentPage=${page.totalPage}'/>"
    >尾页</a>

</div>

 

2.PagaBean类,储存分页需要的数据

/**
 * 有些没必要设置get/set,为了节省时间,不搞了
 *
 * @param <T>
 */
public class PageBean<T> {
    private int currentPage; //当前页
    private int pageSize; //每页记录数
    private int totalPage;//总页数
    private List<T> pageBean;//当前页对象
    private String url;//用来设置Servlet访问路径及method参数
    private int totleCount;//总数据数

    public int getCurrentPage() {
        return currentPage;
    }

    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    public int getTotalPage() {
        return totalPage;
    }

    public void setTotalPage(int totalPage) {
        this.totalPage = totalPage;
    }

    public List<T> getPageBean() {
        return pageBean;
    }

    public void setPageBean(List<T> pageBean) {
        this.pageBean = pageBean;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url,HttpServletRequest request) {
        String uri = request.getRequestURI();    //返回“项目名/请求Servlet名”的字符串
        String quString = request.getQueryString(); //获取请求的参数部分
         url = uri + "?" + quString;   //拼串,请求地址:项目名/servlet名?参数
     if (url.contains("&currentPage")) {
            //截串,将没有用的参数截去,只留下用于反射的method参数
         url = url.substring(0, url.indexOf("&currentPage"));
        }
      this.url=url;
    }

    public int getTotleCount() {
        return totleCount;
    }

    public void setTotleCount(int totleCount) {
        this.totleCount = totleCount;
    }
}

 

3.dao层,主要分为两个查询方法,一个是总记录数,一个是单页的对象集合,工具类用的是dbutils,数据连接池用的是c3p0

   //查询全部获得总记录数
    @Override
    public Integer totleCount() {
        String sql = "SELECT COUNT(*) FROM `grade` AS g  LEFT JOIN `class`  c ON g.`gradeId`=c.`gradeId`";
        Integer totleCount = 0;
        try {
            totleCount=((Long)queryRunner.query(sql, new ScalarHandler<>())).intValue();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return totleCount;
    }
    //查询全部获取当前页对象集合
    @Override
    public List<GradeAndClass> getRoleList(int pagesize, int begin) {
        String sql="SELECT g.`gradeId`,g.`gradeName`,c.`classId`,c.`className` ,c.`orderID` FROM `grade` AS g  LEFT JOIN `class`  c ON g.`gradeId`=c.`gradeId` LIMIT ?,?";
        List<GradeAndClass> gas=null;
        try {
            gas = queryRunner.query(sql, new BeanListHandler<GradeAndClass>(GradeAndClass.class),new Object[]{begin,pagesize});
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return gas;
    }

 

4.service层,调用dao层查询总记录数和当前页对象集合的方法,用于配置pagebean各个属性值,返回pageBean对象

  //分页+查询全部
    @Override
    public PageBean<GradeAndClass> page(Integer pagesize, Integer currentPage,HttpServletRequest request) {
        PageBean<GradeAndClass> pageBean = new PageBean<>();
        //页面大小
        pageBean.setPageSize(pagesize);
        //从数据库得到总记录数,将其放在pageBean里面
        pageBean.setTotleCount(gcDao.totleCount());
        //总页码
        pageBean.setTotalPage(pageBean.getTotleCount() % pageBean.getPageSize() == 0
                ? pageBean.getTotleCount() / pageBean.getPageSize()
                : pageBean.getTotleCount() / pageBean.getPageSize() + 1);
        //当前页
        pageBean.setCurrentPage(currentPage);
        //设置url
        pageBean.setUrl("/main/gc?action=getPage",request);
        //从数据库获取当前页面数据集合,传值给pageBean对象
        pageBean.setPageBean(gcDao.getRoleList(pageBean.getPageSize(), (pageBean.getCurrentPage() - 1) * (pageBean.getPageSize())));
        return pageBean;
    }

 

4.servlet层,获取前端传的页码,返回给前端List集合(用于展示分页的内容),pagebean对象(pagebean对象用于分页jsp页面)

 //查询全部并分页
    public String getPage(HttpServletRequest request, HttpServletResponse response){
        //获取前端传来的当前页码
        Integer currentPage = Integer.parseInt(request.getParameter("currentPage"));
        //获取PageBean对象
        PageBean<GradeAndClass> page = gcService.page(5, currentPage,request);
        List<GradeAndClass> pageBean = page.getPageBean();
        request.setAttribute("page",page);
        request.setAttribute("gc",pageBean);
        return "/WEB-INF/views/main/allGCPage.jsp";
    }

 

5.引用分页jsp页面,放在需要分页的jsp页面适当位置,引用的是分页页面

<%@include file="pageModel.jsp"%>

 

6.触发分页,传参当前页

<li><a data-href="/main/gcc?action=getPage&currentPage=1" data-title="课程管理" href="javascript:void(0)">全部课程</a></li>

 

posted @ 2020-06-29 20:00  亲爸爸  阅读(1680)  评论(0编辑  收藏  举报