看看JSP中怎样实现分页显示
的有关信息介绍如下:
分页依据: select 字段列表 from 表名 limit m,n; m: 表示起始记录,并且从0开始 n: 查询记录的个数,每页记录数分页信息 共多少页 有没有上一页 有没有下一页 当前页 注:分页信息类Page 注2:创建分页信息辅助类PageUtil public static Page createPage(int everyPage,int totalCount,int currentPage) everyPage: 程序员定 totalCount: 总记录数,查询数据库表记录 select count(*) from 表名 currentPage: 从默认第一页开始,下一页= 当前页+1 上一页 = 当前页-1分页数据集合List 依据查询语句获得集合: select 字段列表 from 表名 limit m,n; m: beginIndex n: everyPage具体实现:::UserBiz: //分页public int getCont();public List findByPage(Page page);123UserBizImpl:@Overridepublic int getCont() {String sql = "select count(*) as count from user";CountUtil count = (CountUtil) udao.get(sql, CountUtil.class);return count.getCount();}@Overridepublic List findByPage(Page page) {String sql = "select * from user limit "+page.getBeginIndex()+", "+page.getEveryPage();return udao.query(sql, User.class);}servlet::UserServletint everyPage = 5;//每页记录数int totalCount = ubiz.getCont();//获取总记录数//点击链接重新获取当前页String scurrentPage = request.getParameter("currentPage");int currentPage = 1; //当前页,默认1if(scurrentPage == null){currentPage = 1;//从第一页开始访问}else{currentPage = Integer.parseInt(scurrentPage);}//分页信息Page page = PageUtil.createPage(everyPage, totalCount, currentPage);//分页数据信息List list = ubiz.findByPage(page);request.setAttribute("page", page);request.setAttribute("list", list);//转发到userlist.jsprequest.getRequestDispatcher("/back/manager/userlist.jsp").forward(request, response);userlist.jsp中的分页表现
当前为第${page.currentPage}页,共${page.totalPage}页 | /user.do?method=list¤tPage=1">首页 |/user.do?method=list¤tPage=${page.currentPage -1 }">上一页首页 | 上一页/user.do?method=list¤tPage=${page.currentPage + 1 }">下一页 |/user.do?method=list¤tPage=${page.totalPage }">尾页下一页 | 尾页 |
附::::page类(分页信息实体类)public class Page {private int everyPage; //每页显示记录数private int totalCount; //总记录数private int totalPage; //总页数private int currentPage; //当前页private int beginIndex; //查询起始点private boolean hasPrePage; //是否有上一页private boolean hasNextPage; //是否有下一页public Page(int everyPage, int totalCount, int totalPage,int currentPage,int beginIndex, boolean hasPrePage,boolean hasNextPage) { //自定义构造方法this.everyPage = everyPage;this.totalCount = totalCount;this.totalPage = totalPage;this.currentPage = currentPage;this.beginIndex = beginIndex;this.hasPrePage = hasPrePage;this.hasNextPage = hasNextPage;}///////get,set方法略}123456789101112131415161718192021附:pageutil 分页信息辅助类public class PageUtil {//创建Page对象public static Page createPage(int everyPage,int totalCount,int currentPage) {//创建分页信息对象everyPage = getEveryPage(everyPage);currentPage = getCurrentPage(currentPage);int totalPage = getTotalPage(everyPage, totalCount);int beginIndex = getBeginIndex(everyPage, currentPage);boolean hasPrePage = getHasPrePage(currentPage);boolean hasNextPage = getHasNextPage(totalPage, currentPage);return new Page(everyPage, totalCount, totalPage, currentPage,beginIndex, hasPrePage, hasNextPage);}// 以下方法辅助创建Page对象public static int getEveryPage(int everyPage) { //获得每页显示记录数return everyPage == 0 ? 10 : everyPage;}public static int getCurrentPage(int currentPage) { //获得当前页return currentPage == 0 ? 1 : currentPage;}public static int getTotalPage(int everyPage,int totalCount) {//获得总页数int totalPage = 0;if(totalCount != 0 &&totalCount % everyPage == 0) {totalPage = totalCount / everyPage;} else {totalPage = totalCount / everyPage + 1;}return totalPage;}public static int getBeginIndex(int everyPage,int currentPage) {//获得起始位置return (currentPage - 1) * everyPage;}public static boolean getHasPrePage(int currentPage) {//获得是否有上一页return currentPage == 1 ? false : true;}public static boolean getHasNextPage(int totalPage, int currentPage) { //获得是否有上一页return currentPage == totalPage || totalPage == 0 ? false : true;}}