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>
  • 相关阅读:
    对用户控件(ascx)属性(property)赋值
    The Controls collection cannot be modified because the control contains code blocks (i.e. <% ... %>).
    图片淡入淡出切换效果
    在用户控件(ASCX)创建用户控件(ASCX)
    Login failed for user 'xxx'
    一些较好的书
    儿子购买的书
    怀念以前做网管的日子
    Linux下selinux简单梳理
    Rsync同步时删除多余文件 [附:删除大量文件方法的效率对比]
  • 原文地址:https://www.cnblogs.com/hehaiyang/p/4190295.html
Copyright © 2011-2022 走看看