zoukankan      html  css  js  c++  java
  • SpringMVC框架下实现分页功能

    1、创建实体类Page.java

    @Entity
    public class Page {
        private int totalRecord;// 表示查询后一共得到多少条结果记录
        private int pageSize; // 表示页面一次要显示多少条记录
        private int totalPage;// 表示将所有的记录进行分页后,一共有多少页
        private int startIndex;// 表示从所有的结果记录中的哪一个编号开始分页查询
        private int currentPage; // 表示用户想看的页数
    
        @SuppressWarnings("unchecked")
        private List list =null;// list集合是用来装载一个页面中的所有记录的
    
        public Page(int pageNum, int totalRecord) {
            this.currentPage = pageNum;
            this.totalRecord = totalRecord;
    
            this.pageSize = 5;// 设置一页默认显示10条查询记录
            this.startIndex = (this.currentPage - 1) * this.pageSize;// 至于为什么this.page要减1,
            // 是因为mysql数据库对于分页查询时,得到的所有的查询记录,第一条记录的编号是从0开始。
            if (this.totalRecord % this.pageSize == 0) {
                this.totalPage = this.totalRecord / this.pageSize;
            } else {
                this.totalPage = this.totalRecord / this.pageSize + 1;
            }
    
        }
    
       //****此处省略了set和get方法****//
    }

    2、创建Dao层实现类PageDaoImpl.java

    @Repository
    public class PageDaoImpl implements PageDao {
    
        @Autowired
        private JdbcTemplate jdbcTemplate;
    
        /* 
         * 获得总记录数
         */
        @SuppressWarnings("deprecation")
        public int getTotalRecord(String sql, Object... arrayParameters) {
            int totalRecord = jdbcTemplate.queryForInt(sql, arrayParameters);
            return totalRecord;
        }
    
        /* 
         * 获取当前页数据
         */
        @SuppressWarnings("unchecked")
        public Page getPage(int pageNum, Class clazz, String sql, int totalRecord, Object... parameters) {
            Page page = new Page(pageNum, totalRecord);
            sql = sql+" limit "+page.getStartIndex()+","+page.getPageSize();
            List list=jdbcTemplate.query(sql, parameters, ParameterizedBeanPropertyRowMapper.newInstance(clazz));    
            page.setList(list);
            return page;
        }
    }

    3、在服务层实现类中添加代码

        public Page getClassifyPage(int pageNum) {
            String sql = "select count(*) from t_classify";
            int totalRecord = pageDao.getTotalRecord(sql);
            sql = "select * from t_classify";
            Page page = pageDao.getPage(pageNum, Classify.class, sql, totalRecord);
            return page;
        }

    4、在控制层中添加代码

        @RequestMapping("/list")
        public String list(HttpServletRequest request) {
            String pageNum=request.getParameter("p")==null?"1":request.getParameter("p");//获取页码,默认1
            request.setAttribute("page", classifyService.getClassifyPage(Integer.valueOf(pageNum)));return "admin/classify/list";
        }

    5、在jsp页面中布局

    内容部分:

    <c:forEach var="classify" items="${page.list}" varStatus="s">
      <tr class="column_${s.count}">
           <td class="list-text color999">${classify.name}</td>
           <td class="list-text color999">${classify.id}</td>
    </tr> </c:forEach>

    分页按钮部分:

           页次:${page.currentPage}/${page.totalPage}&nbsp;每页${page.pageSize}&nbsp;总数${page.totalRecord}&nbsp;&nbsp;&nbsp;&nbsp;
      <a href="<c:url value='/admin/user/list.htm?p=1'/>">首页</a>
        <c:choose>  
               <c:when test="${page.currentPage>1}">
                   <a href="<c:url value='/admin/classify/list.htm?p=${page.currentPage-1}'/>">上一页</a>
               </c:when>  
               <c:otherwise>
                       <a href="#">上一页</a>
               </c:otherwise>  
        </c:choose>
        <%--
        &nbsp;&nbsp;
        <c:forEach var="i" begin="1" end="${page.totalPage}">
           <a href="<c:url value='/classify.htm?c=${page.list[0].classifyid}&p=${i}'/>">${i}</a>
        </c:forEach>
        --%> <c:choose> <c:when test="${page.currentPage<page.totalPage}"> <a href="<c:url value='/admin/classify/list.htm?p=${page.currentPage+1}'/>">下一页</a> </c:when> <c:otherwise> <a href="#">下一页</a> </c:otherwise> </c:choose>
  • 相关阅读:
    BZOJ 3205 [Apio2013]机器人 ——斯坦纳树
    BZOJ 3782 上学路线 ——动态规划 Lucas定理 中国剩余定理
    HDU 1423 Greatest Common Increasing Subsequence ——动态规划
    BZOJ 3309 DZY Loves Math ——莫比乌斯反演
    POJ 1038 Bugs Integrated, Inc. ——状压DP
    POJ 3693 Maximum repetition substring ——后缀数组
    POJ 2699 The Maximum Number of Strong Kings ——网络流
    POJ 2396 Budget ——有上下界的网络流
    BZOJ 4650 [Noi2016]优秀的拆分 ——后缀数组
    源码安装python
  • 原文地址:https://www.cnblogs.com/hehaiyang/p/4190295.html
Copyright © 2011-2022 走看看