翻页的实现
Posted afangfang
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了翻页的实现相关的知识,希望对你有一定的参考价值。
Page.java
package com.aff.bookstore.web; import java.util.List; public class Page<T> { // 当前第几页 private int pageNo; // 当前页的List private List<T> list; // 每页显示多少条记录 private int pageSize = 3; // 共有多少条记录 private long totalItemNumber; public Page() { super(); // TODO Auto-generated constructor stub } // 构造器中需要对pageNo初始化 public Page(int pageNo) { super(); this.pageNo = pageNo; } //得到当前第几页,需要校验一下,不一定是合法的 public int getPageNo() { if (pageNo<0) pageNo=1; if (pageNo>getTotalPageNumber()) { pageNo=getTotalPageNumber(); } return pageNo; } public void setTotalItemNumber(long totalItemNumber) { this.totalItemNumber = totalItemNumber; } public int getPageSize() { return pageSize; } public void setList(List<T> list) { this.list = list; } public List<T> getList() { return list; } // 获取总页数 public int getTotalPageNumber() { int totalPageNumber = (int) (totalItemNumber / pageSize); if (totalItemNumber % pageSize != 0) { totalPageNumber++; } return totalPageNumber; } public boolean isHasNext(){ if (getPageNo()<getTotalPageNumber()) { return true; } return false; } public boolean isHasPrev(){ if (getPageNo()>1) { return true; } return false; } public int getPrevPage(){ if (isHasPrev()) { return getPageNo() - 1; } return getPageNo(); } public int getNextPage(){ if (isHasNext()) { return getPageNo() +1; } return getPageNo(); } }
CriteriaBook.java
package com.aff.bookstore.web; public class CriteriaBook { private float minPrice = 0; private float maxPrice = Integer.MAX_VALUE;//2^31 - 1 private int pageNo; public float getMinPrice() { return minPrice; } public void setMinPrice(float minPrice) { this.minPrice = minPrice; } public float getMaxPrice() { return maxPrice; } public void setMaxPrice(float maxPrice) { this.maxPrice = maxPrice; } public int getPageNo() { return pageNo; } public void setPageNo(int pageNo) { this.pageNo = pageNo; } public CriteriaBook() { super(); // TODO Auto-generated constructor stub } public CriteriaBook(float minPrice, float maxPrice, int pageNo) { super(); this.minPrice = minPrice; this.maxPrice = maxPrice; this.pageNo = pageNo; } @Override public String toString() { return "CriteriaBook [minPrice=" + minPrice + ", maxPrice=" + maxPrice + ", pageNo=" + pageNo + "]"; } }
BookServlet.java
package com.aff.bookstore.servlet; import java.io.IOException; import java.lang.reflect.Method; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.aff.bookstore.domain.Book; import com.aff.bookstore.service.BookService; import com.aff.bookstore.web.CriteriaBook; import com.aff.bookstore.web.Page; @WebServlet("/bookServlet") public class BookServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } private BookService bookService = new BookService(); protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String methodName = request.getParameter("method"); try { Method method = getClass().getDeclaredMethod(methodName, HttpServletRequest.class, HttpServletResponse.class); method.setAccessible(true); method.invoke(this, request, response); } catch (Exception e) { e.printStackTrace(); } } protected void getBook(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String idStr = request.getParameter("id"); int id = -1; Book book = null; try { id = Integer.parseInt(idStr); } catch (NumberFormatException e) {} if (id>0) { book = bookService.getBook(id); if (book == null) { response.sendRedirect(request.getContextPath() + "/errror-1.jsp"); return; } } request.setAttribute("book", book); request.getRequestDispatcher("/WEB-INF/pages/book.jsp").forward(request, response); } protected void getBooks(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String pageNoStr = request.getParameter("pageNo"); String minPriceStr = request.getParameter("minPrice"); String maxPriceStr = request.getParameter("maxPrice"); int pageNo = 1; int minPrice = 0; int maxPrice = Integer.MAX_VALUE; try { pageNo = Integer.parseInt(pageNoStr); } catch (Exception e) { } try { minPrice = Integer.parseInt(minPriceStr); } catch (Exception e) { } try { maxPrice = Integer.parseInt(maxPriceStr); } catch (Exception e) { } CriteriaBook criteriaBook = new CriteriaBook(minPrice, maxPrice, pageNo); Page<Book> page = bookService.getPage(criteriaBook); request.setAttribute("bookpage", page); request.getRequestDispatcher("/WEB-INF/pages/books.jsp").forward(request, response); } }
books.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> <script type="text/javascript" src="script/jquery-1.12.3.js"></script> <script type="text/javascript"> $(function(){ $("a").click(function(){ var serializeVal = $(":hidden").serialize(); var href = this.href+"&"+serializeVal; window.location.href = href; return false; }); $("#pageNo").change(function(){ var val = $(this).val(); val = $.trim(val);//去除空格 //1.校验 val 是否为数字 var reg = /^d+$/g; if(!reg.test(val)){ alert("输入的不是合法的页码") $(this).val("") return ; } //2.检验 val 在一个合法的范围内, 1-totalPageNumber var pageNo = parseInt(val); if(pageNo<1 || pageNo>parseInt("${bookpage.totalPageNumber}")){ alert("输入的不是合法的页码"); $(this).val(""); return; } //3.页面跳转,保证把之前的条件带过去 var href = "bookServlet?method=getBooks&pageNo="+pageNo+"&"+$(":hidden").serialize(); window.location.href =href ; return; }); }); </script> </head> <body> <input type="hidden" name="minPrice" value="${param.minPrice}"/> <input type="hidden" name="maxPrice" value="${param.maxPrice}"/> <center> <br> <br> <form action="bookServlet?method=getBooks" method="post"> Price: <input type="text" size="1" name="minPrice"/> <input type="text" size="1" name="maxPrice"/> <input type="submit" value="Submit"/> </form> <br> <br> <table cellpadding="10"> <c:forEach items="${bookpage.list }" var="book"> <tr> <td> <a href="bookServlet?method=getBook&pageNo=${bookpage.pageNo }&id=${book.id}">${book.title }</a> <br> ${book.author } </td> <td>${book.price }</td> <td><a href="">加入购物车</a></td> </tr> </c:forEach> </table> <br><br> 共 ${bookpage.totalPageNumber }页 当前第${bookpage.pageNo }页 <c:if test="${bookpage.hasPrev }"> <a href="bookServlet?method=getBooks&pageNo=1">首页</a> <a href="bookServlet?method=getBooks&pageNo=${bookpage.prevPage }">上一页</a> </c:if> <c:if test="${bookpage.hasNext }"> <a href="bookServlet?method=getBooks&pageNo=${bookpage.nextPage }">下一页</a> <a href="bookServlet?method=getBooks&pageNo=${bookpage.totalPageNumber}">末页</a> </c:if> 转到<input type="text" size="1" id="pageNo"/>页 </center> </body> </html>
queryCondition.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <script type="text/javascript"> $(function(){ $("a").click(function(){ var serializeVal = $(":hidden").serialize(); var href = this.href+"&"+serializeVal; window.location.href = href; return false; }); }); </script> <input type="hidden" name="minPrice" value="${param.minPrice}"/> <input type="hidden" name="maxPrice" value="${param.maxPrice}"/>
目录
运行效果如下
以上是关于翻页的实现的主要内容,如果未能解决你的问题,请参考以下文章
实现类似微信表情包横向滚动翻页的功能,运用UICollectionView,自定义UICollectionViewFlowLayout,cell左右排版 ,支持多组Cell实现。