分页显示 ----使用subList方法

1.pager类

package po;

import java.util.List;

public class Pager {

    private int pageSize;
    private int currentPage;
    private int totalRecord;
    private int totalPage;
    private List dataList;

    public Pager(int pageNum,int pageSize,List sourceList) {

        if(sourceList == null || sourceList.isEmpty()){
            return;
        }

        // 总记录条数
        this.totalRecord = sourceList.size();

        // 每页显示多少条记录
        this.pageSize = pageSize;

        //获取总页数
        this.totalPage = this.totalRecord / this.pageSize;
        if(this.totalRecord % this.pageSize !=0){
            this.totalPage = this.totalPage + 1;
        }

        // 当前第几页数据
        this.currentPage = this.totalPage < pageNum ?  this.totalPage : pageNum;

        // 起始索引
        int fromIndex    = this.pageSize * (this.currentPage -1);

        // 结束索引
        int toIndex  = this.pageSize * this.currentPage > this.totalRecord ? this.totalRecord : this.pageSize * this.currentPage;

        this.dataList = sourceList.subList(fromIndex, toIndex);
    }
    public int getPageSize() {
        return pageSize;
    }

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

    public int getCurrentPage() {
        return currentPage;
    }

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

    public int getTotalRecord() {
        return totalRecord;
    }

    public void setTotalRecord(int totalRecord) {
        this.totalRecord = totalRecord;
    }

    public int getTotalPage() {
        return totalPage;
    }

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

    public List getDataList() {
        return dataList;
    }

    public void setDataList(List dataList) {
        this.dataList = dataList;
    }
}

2.service

    public Pager findCommodityOrder( List allDatebaseList, int pageNum, int pageSize) throws SQLException {
        DatebaseDAO dao = new DatebaseDAO();
        Pager pager = new Pager(pageNum, pageSize,
                allDatebaseList);
        return pager;
    }

3.servlet

            String pageNumStr = request.getParameter("pageNum");
            int pageNum = 1; //显示第几页数据
            if(pageNumStr!=null && !"".equals(pageNumStr.trim())){
                pageNum = Integer.parseInt(pageNumStr);
            }
            int pageSize = 5;  // 每页显示多少条记录
            String pageSizeStr = request.getParameter("pageSize");
            if(pageSizeStr!=null && !"".equals(pageSizeStr.trim())){
                pageSize = Integer.parseInt(pageSizeStr);
            }
            Pager result;
            try {
                result =  service.findCommodityOrder(service.selectall(), pageNum, pageSize);
                HttpSession session=request.getSession(false);
                if(session == null) {
                    session=request.getSession(true);
                }
                session.setAttribute("result", result);

                response.sendRedirect("5.jsp");
            } catch (SQLException e) {
                e.printStackTrace();
            }

4.jsp



获取数据  ${result.dataList }

  
共${result.totalRecord }条记录共${result.totalPage }页  当前第${result.currentPage }页 &nbsp; "#" onclick="firstPage();">首页 &nbsp; "#" onclick="nextPage();">下一页 &nbsp; "#" onclick="previousPage();">上一页 &nbsp; "#" onclick="lastPage();">尾页

 

你可能感兴趣的:(分页显示 ----使用subList方法)