例如我做的书城,其中的分页方式 import java.util.List; public class PageResult { } 这是我的domain类 import java.sql.SQLException; import javax.naming.ldap.PagedResultsControl; import dao.ProductDao; public class ProductService { } 其中在设置总页数总使用了Math.ceil方法 Math.ceil() 函数返回大于或等于一个给定数字的最小整数。 servlet import java.io.IOException; import javax.servlet.ServletException; import domain.PageResult; @WebServlet(“/showProductByPage”) }
首先需要一个domain类,用来记录总条数,每页规定显示数,总页数,当前页
package domain;
List list;
long totalCount;//总条数
int pageSize = 4;//每页显示条数
int totalPage;//总页数
int currentPage;//当前页
public List getList() {
return list;
}
public void setList(List list) {
this.list = list;
}
public long getTotalCount() {
return totalCount;
}
public void setTotalCount(long totalCount) {
this.totalCount = totalCount;
}
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 getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
@Override
public String toString() {
return “PageResult [totalCount=” + totalCount + “, pageSize=” + pageSize + “, totalPage=” + totalPage
+ “, currentPage=” + currentPage + “]”;
}
创建一个service类
在service类中定义总页数的规则,及设置当前页数
package service;
import java.util.ArrayList;
import java.util.List;
import domain.PageResult;
import domain.Product;ProductDao productDao=new ProductDao(); public PageResult<Product> findBooks(String category,int page){ try { //创建模型 PageResult<Product> pr=new PageResult<Product>(); //设置总记录数 long totalCount=new ProductDao().count(category); pr.setTotalCount(totalCount); System.out.println(category); System.out.println(totalCount); //设置总页数 int totalPage=(int) Math.ceil(totalCount*1.0/pr.getPageSize()); pr.setTotalPage(totalPage); //设置当前页数 pr.setCurrentPage(page); //设置数据list List<Product> al=productDao.findBooks(category, page, pr.getPageSize()); pr.setList(al); return pr; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return null; } }
totalCount*1.0/pr.getPageSize()
我们定义总页数的规则,totalCount是总条数,在PageResult类中我们设置了pageSize为4,也就是一页显示四条信息,用总条数,除去4,例如现在有17条数据,17除去4还余了1,Math.ceil()方法的作用就体现出来了
package web.servlet;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import domain.Product;
import service.ProductService;
public class ShowProductByPageServlet extends HttpServlet{
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException { String category =req.getParameter("category"); byte[] buf = category.getBytes("iso8859-1"); //第二步: 使用utf-8码表进行解码 category = new String(buf,"utf-8"); System.out.println(category); String pageStr=req.getParameter("page"); int page=1; if(pageStr!=null && !"".equals(pageStr)){ page=Integer.parseInt(pageStr); } //调用service ProductService ps=new ProductService(); PageResult<Product> pageResult=ps.findBooks(category, page); //存入request req.setAttribute("pageResult", pageResult); req.setAttribute("category", category); //跳转页面 req.getRequestDispatcher("/product_list.jsp").forward(req, resp); }
在页面中,使用c:forEach来显示页数,及当前页数
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算