Java实现页面显示中的信息分页

来源:互联网 发布:软件系统性能指标要求 编辑:程序博客网 时间:2024/06/05 05:45

首先,我们一般在页面中经常会看到这样的页面:上一页,下一页,跳转到**页等等这样的分页信息,大概的表格信息如下图:


所以,我们通过Java代码去大概实现后端-》前端的大概流程


1.     看到这个分页的界面,首先应该将分页的相关信息进行封装JavaBean:代码如下

package com.yangguang.domain;import java.util.List;public class Page {private List<Book> records;private int pageSize = 3;//每页显示的条数private int totalPage;//总页码数private int currentPageNum;//当前页码private int prePageNum;//上一页private int nextPageNum;//下一页private int startIndex;//每页开始记录的索引private int totalRecords;//总记录条数private String url;public Page(int currentPageNum,int totalRecords) {this.currentPageNum = currentPageNum;this.totalRecords = totalRecords;totalPage = totalRecords % pageSize == 0?totalRecords/pageSize:totalRecords/pageSize+1;startIndex = (currentPageNum-1)*pageSize;}public List<Book> getRecords() {return records;}public void setRecords(List<Book> records) {this.records = records;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getTotalPage() {return totalPage;}public void setTotalPage(int totalPage) {this.totalPage = totalPage;}public int getCurrentPageNum() {return currentPageNum;}public void setCurrentPageNum(int currentPageNum) {this.currentPageNum = currentPageNum;}public int getPrePageNum() {prePageNum = currentPageNum -1;if(prePageNum < 1) {prePageNum = 1;}return prePageNum;}public void setPrePageNum(int prePageNum) {this.prePageNum = prePageNum;}public int getNextPageNum() {nextPageNum = currentPageNum + 1;if(nextPageNum > totalPage) {nextPageNum = 1;}return nextPageNum;}public void setNextPageNum(int nextPageNum) {this.nextPageNum = nextPageNum;}public int getStartIndex() {return startIndex;}public void setStartIndex(int startIndex) {this.startIndex = startIndex;}public int getTotalRecords() {return totalRecords;}public void setTotalRecords(int totalRecords) {this.totalRecords = totalRecords;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}}

封装好页面信息之后,我们需要去设计接口


2.第二步就是设计借口了。在图书管理的DAO层接口中,增加了3个函数:


接口实现如下:

public Book findById(String bookId) {try { return qRunner.query("select * from books where id=?", new BeanHandler<Book>(Book.class), bookId);} catch (SQLException e) {throw new RuntimeException(e);}}public int findAllBooksNumber() {try {Object object = qRunner.query("select count(*) from books", new ScalarHandler(1));Long num = (Long)object;return num.intValue();} catch (SQLException e) {throw new RuntimeException(e);}}public List<Book> findPageBooks(int startIndex, int offset) {try {return qRunner.query("select * from books limit ?,?", new BeanListHandler<Book>(Book.class),startIndex,offset);} catch (SQLException e) {throw new RuntimeException(e);}}

3.    在service层,设计接口及实现:



接口实现代码:

public Page findAllBookPageRecords(String pagenum) {int currentPageNum = 1;if (pagenum != null) {currentPageNum = Integer.parseInt(pagenum);}int totalRecords = bookDao.findAllBooksNumber();Page page = new Page(currentPageNum, totalRecords);page.setRecords(bookDao.findPageBooks(page.getStartIndex(), page.getPageSize()));return page;}

4.前端页面设计:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@ include file="/manager/header.jsp" %><br/><br/><table border="1" width="638" align="center">  <tr>    <th>选择</th>    <th>书名</th>    <th>作者</th>    <th>单价</th>    <th>描述</th>    <th>所属分类</th>    <th>图片</th>    <th>操作</th>  </tr>  <c:forEach  items="${page.records}" var="book" varStatus="vs">  <tr class="${vs.index%2==0?'odd':'even'}">    <td>    <input type="checkbox" name="ids" value="${book.id}"/>    </td>    <td>${book.name}</td>    <td>${book.author}</td>    <td>${book.price}</td>    <td>${book.description}</td>    <td>${book.categoryId}</td>    <td>    <img src="E:/学习测试程序/JavaTest/Project1_BookStore/WebContent/images${book.path}/${book.photoFileName}"/>    </td>    <td>    <a href="javascript:alert('已修改')">修改</a>    <a href="javascript:alert('已删除')">删除</a>    </td>  </tr></c:forEach><tr></tr></table><br/>第${page.currentPageNum}页/共${page.totalPage}页  <a href="${pageContext.request.contextPath}/servlet/ControlServlet?op=showAllBooks&num=${page.prePageNum}">上一页</a> <a href="${pageContext.request.contextPath}/servlet/ControlServlet?op=showAllBooks&num=${page.nextPageNum}">下一页</a>  </body></html>

项目代码课参考:https://github.com/yangguang010/Java_Project.git

原创粉丝点击