java初级简单分页功能实现

来源:互联网 发布:分销系统源码 编辑:程序博客网 时间:2024/05/29 04:36

业务逻辑

  • 实现原理:用户每次点击检索或者分页按钮查询时,提交表单,并将当前页码传入到servlet。
    • 点击检索图书时,传入当前页码为1
    • 点击分页按钮页码时:传入对应页码值
    • 每次提交表单,根据当前传入的页码数,查询指定位置的图书信息
      • 例如:设置每页显示的数量为pageRowNumber点击第2页,查询的索引则为((pageRowNumber*2)-(pageRowNumber-1))到(pageRowNumber*2)。查询结束转发至当前页面,并将查询结果保存到request范围对象中,动态获取
  • 方法准备

    • 需要获取到数据库总的图书信息数量
    • 需要设置每页显示的图书信息
    • 根据当前页面计算出查询的起始和结束索引
  • 定义方法获取总数selectRowCount()

/**     * 获得BOOK_MESSAGE表中的图书信息的总行数     *      * @param conn     *            数据库连接对象     * @return 返回Integer类型,返回总行数     * @throws SQLException     */    public abstract Integer selectRowCount(Connection conn) throws SQLException;
  • 定义根据索引查询指定的图书信息
/**     * 查询BOOK_MESSAGE表中指定位置的图书信息     *      * @param conn     *            数据库连接对象     * @param entity     *            封装查询信息的实体类     * @param startRow     *            查询起始     * @param endRow     *            查询结束     * @return 返回java.util.List类型,每一条数据是一个封装数据的实体类     * @throws SQLException     */    public abstract List<BookMessageEntity> selectBookMessage(Connection conn, Integer startRow, Integer endRow) throws SQLException;
  • 实现接口中的方法
@Override    public List<BookMessageEntity> selectBookMessage(Connection conn,Integer startRow ,Integer endRow) throws SQLException {        String sql = "SELECT R,BOOK_ID,BOOK_NAME,BOOK_AUTHOR,BOOK_PRICE,BOOK_ISBN FROM ("                + "SELECT ROW_NUMBER() OVER(ORDER BY BOOK_ID) AS R,BOOK_ID,BOOK_NAME,BOOK_AUTHOR,"                + "BOOK_PRICE,BOOK_ISBN FROM BOOK_MESSAGE) WHERE R BETWEEN ? AND ? ";        PreparedStatement ps = conn.prepareStatement(sql);        ps.setInt(1, startRow);        ps.setInt(2, endRow);        ResultSet rs = ps.executeQuery();        List<BookMessageEntity> list = new ArrayList<BookMessageEntity>();        BookMessageEntity entity = null;        while(rs.next()){            entity = new BookMessageEntity();            entity.setRowNumber(rs.getInt("R"));            entity.setBookId(rs.getInt("BOOK_ID"));            entity.setBookName(rs.getString("BOOK_NAME"));            entity.setBookAuthor(rs.getString("BOOK_AUTHOR"));            entity.setBookPrice(rs.getDouble("BOOK_PRICE"));            entity.setBookISBN(rs.getString("BOOK_ISBN"));            list.add(entity);        }        return list;    }    @Override    public Integer selectRowCount(Connection conn) throws SQLException {        String sql = "SELECT COUNT(*) FROM BOOK_MESSAGE";        PreparedStatement ps = conn.prepareStatement(sql);        ResultSet rs = ps.executeQuery();        if(rs.next()){            return rs.getInt(1);        }        return 0;    }
  • 定义模型层
/**     * 获得指定位置的图书信息     *      * @param startRow     *            查询的起始     * @param endRow     *            查询的结束     * @return 返回java.util.List类型,每一项封装一行图书信息     * @throws ClassNotFoundException     * @throws SQLException     */    public List<BookMessageEntity> selectBookMessage(Integer startRow, Integer endRow)            throws ClassNotFoundException, SQLException {        Connection conn = DbUtils.getConnection();        try {            BookMessageDAO dao = new BookMessageDAOImpl();            List<BookMessageEntity> list = dao.selectBookMessage(conn, startRow, endRow);            if (list != null) {                return list;            }        } finally {            conn.close();        }        return null;    }    /**     * 获得图书表中图书的总数     *      * @return 返回Integer类型,图书的总数     * @throws ClassNotFoundException     * @throws SQLException     */    public Integer selectRowCount() throws ClassNotFoundException, SQLException {        Connection conn = DbUtils.getConnection();        try {            BookMessageDAO dao = new BookMessageDAOImpl();            Integer rowCount = dao.selectRowCount(conn);            return rowCount;        } finally {        }    }
  • 定义控制层
@Override    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {        //当前页数        Integer pageNumber = null;            if ("".equals(req.getParameter("pageNumber"))) {            pageNumber = 1;        } else {            pageNumber = Integer.valueOf(req.getParameter("pageNumber"));        }        //每页显示的数量        ServletContext application = req.getServletContext();        Integer pageRowCount = Integer.valueOf(application.getInitParameter("pageRowCount"));        //每页结束的索引        Integer endRow = pageNumber * pageRowCount;        //每页开始的索引        Integer startRow = endRow - (pageRowCount-1);        //保存分页的页码按钮        int[] array = null;        //保存总的页数        Double pageCount = null;        BookMessageService service = new BookMessageService();        try {            //图书信息的总数            Integer rowCount = service.selectRowCount();            pageCount = Math.ceil(rowCount/Double.valueOf(pageRowCount));            array = new int[pageCount.intValue()] ;            for(int  i = 0 ; i< pageCount.intValue();i++){                array[i] = i+1;            }            List<BookMessageEntity> list = service.selectBookMessage(startRow, endRow);            req.setAttribute("pageButtonArray", array); //保存分页按钮的信息            req.setAttribute("bookMessageList", list);  //保存获得图书的信息            req.setAttribute("pageNumber", pageNumber); //保存当前页码数            req.setAttribute("pageCount", pageCount.intValue()); //保存当前页码总数            req.getRequestDispatcher("select.jsp").forward(req, resp);        } catch (ClassNotFoundException | SQLException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }
  • 视图层
    • 采用EI表达式将控制层传入视图层的数动态显示
      • 获取查询到的所有图书信息
      • 获取根据总的图书信息数得到的分页页码数
    • 为每一个分页按钮设置点击事件通过title属性保存对应的页码值,当点击a标签时,为form表单设置action属性值为selectServlet?pageNumber=当前的页码值,然后提交表单
<!-- 判断通过request范围对象传入的bookMessageList的长度是否为0,不为零进入循环 -->                <c:if test="${fn:length(requestScope.bookMessageList) > 0 }">                    <!-- 每一项bookMessageList即为一个封装一行图书信息的实体类 -->                    <c:forEach items = "${requestScope.bookMessageList }" var = "bookList">                        <tr>                            <td>${bookList.rowNumber }</td>                            <td>${bookList.bookName }</td>                            <td>${bookList.bookAuthor }</td>                            <td>                                <!-- 将价格格式化为货币 -->                                <fmt:formatNumber value="${bookList.bookPrice }" type="currency" var="money"/>                                ${money}                            </td>                            <td>${bookList.bookISBN }</td>                            <td>编辑</td>                            <td>删除</td>                        </tr>                    </c:forEach>                </c:if>
<!-- 判断如果当前页为1,则去除a标签的点击事件,并设置title为1 -->                <c:if test="${requestScope.pageNumber == 1 }">                    <li><a href = "#" title = "1">上一页</a></li>                </c:if>                <!-- 判断如果当前页不为1,则给a标签的添加点击事件,并设置title为当前页减去1 -->                <c:if test="${requestScope.pageNumber != 1 }">                    <li><a href = "#" class = "pageHref" title = "${pageNumber - 1 }">上一页</a></li>                </c:if>                <!-- 判断保存分页按钮的数组长度是否为0,不为0进入循环 -->                <c:if test="${fn:length(requestScope.pageButtonArray) > 0 }">                    <!-- 遍历数组,数组保存的即为页码数 -->                    <c:forEach items = "${requestScope.pageButtonArray }" var = "buttonArray">                        <li><a class = "pageHref" title = "${buttonArray}">${buttonArray}</a></li>                    </c:forEach>                </c:if>                <!-- 判断当前页是否为最后一页,若是,去掉a标签的点击事件,并设置title为最后页码数 -->                <c:if test="${requestScope.pageNumber == requestScope.pageCount }">                    <li><a href = "#" title = "${ requestScope.pageCount}">下一页</a></li>                </c:if>                <!-- 判断当前页是否为最后一页,若不是,给a标签的添加点击事件,并设置title为当前页码数减去1 -->                <c:if test="${requestScope.pageNumber != requestScope.pageCount }">                    <li><a href = "#" class = "pageHref" title = "${pageNumber + 1 }">下一页</a></li>                </c:if>
$(function(){        $(".pageHref").click(function(){            var pageNumber = $(this).prop("title");            $("form").prop("action","selectServlet?pageNumber="+pageNumber);            $("form").submit();            return false;        });    });
  • END
原创粉丝点击