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


    1.pager类

    package po;
    
    import java.util.List;
    
    public class Pager<T> {
    
        private int pageSize;
        private int currentPage;
        private int totalRecord;
        private int totalPage;
        private List<T> dataList;
    
        public Pager(int pageNum,int pageSize,List<T> 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<T> getDataList() {
            return dataList;
        }
    
        public void setDataList(List<T> dataList) {
            this.dataList = dataList;
        }
    }

    2.service

        public Pager<Datebase> findCommodityOrder( List<Datebase> allDatebaseList, int pageNum, int pageSize) throws SQLException {
            DatebaseDAO dao = new DatebaseDAO();
            Pager<Datebase> pager = new Pager<Datebase>(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<Datebase> 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

    <script type="text/javascript">
        // 当前第几页数据
        var currentPage = ${result.currentPage};
    
        // 总页数
        var totalPage = ${result.totalPage};
    
        function submitForm(actionUrl){
            window.location.href=actionUrl;
        }
    
        // 第一页
        function firstPage(){
            if(currentPage == 1){
                alert("已经是第一页数据");
                return false;
            }else{
                submitForm("datebaseServlet?flag=5&pageNum=1");
                return true;
            }
        }
    
        // 下一页
        function nextPage(){
            if(currentPage == totalPage){
                alert("已经是最后一页数据");
                return false;
            }else{
                submitForm("datebaseServlet?flag=5&pageNum=" + (currentPage+1));
                return true;
            }
        }
    
        // 上一页
        function previousPage(){
            if(currentPage == 1){
                alert("已经是第一页数据");
                return false;
            }else{
                submitForm("datebaseServlet?flag=5&pageNum=" + (currentPage-1));
                return true;
            }
        }
    
        // 尾页
        function lastPage(){
            if(currentPage == totalPage){
                alert("已经是最后一页数据");
                return false;
            }else{
                submitForm("datebaseServlet?flag=5&pageNum="+totalPage);
                return true;
            }
        }
    </script>

    获取数据  ${result.dataList }

      <br> 共${result.totalRecord }条记录共${result.totalPage }页&nbsp;&nbsp;当前第${result.currentPage }页&nbsp;&nbsp;
                            <a href="#" onclick="firstPage();">首页</a>&nbsp;&nbsp;
                            <a href="#" onclick="nextPage();">下一页</a>&nbsp;&nbsp;
                            <a href="#" onclick="previousPage();">上一页</a>&nbsp;&nbsp;
                            <a href="#" onclick="lastPage();">尾页</a>
  • 相关阅读:
    如何获得RVA(相对虚地址)的值,从而得到一个程序的入口点
    Prism 问题总结一: 在模块中引用公用程序集出错
    Dapper 操作 ACCESS 数据库问题总结
    我国土地招拍挂制度
    [导入]棋味
    [导入]无语
    [导入]心灯
    [导入]寄托
    [导入]视频资源
    [导入]asp.net实现视频截图
  • 原文地址:https://www.cnblogs.com/When6/p/13283215.html
Copyright © 2020-2023  润新知