JSP+Servlet技术实现分页 首页 下一页 每一页显示10条页码 下一页 尾页 第页/共页 (利用PageBean实现)

前端JSP页面的代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>




Insert title here


	
			
pid 商品图片 商品名称 市场价 商城价 商品描述
${p.pid} ${p.pname} ${p.market_price} ${p.shop_price} ${p.pdesc}
[首页] [上一页] ${n} ${n} ${n} ${n} [下一页] [尾页]   第${pb.currPage }页/共${pb.totalPage }页

商品详情显示的代码:

JSP+Servlet技术实现分页 首页 下一页 每一页显示10条页码 下一页 尾页 第页/共页 (利用PageBean实现)_第1张图片


首页 上一页的代码



每一页显示10条页码的代码:

JSP+Servlet技术实现分页 首页 下一页 每一页显示10条页码 下一页 尾页 第页/共页 (利用PageBean实现)_第2张图片


下一页 尾页 第页/共页的代码:



分页效果显示:

JSP+Servlet技术实现分页 首页 下一页 每一页显示10条页码 下一页 尾页 第页/共页 (利用PageBean实现)_第3张图片


后端的代码:

PageBean的代码:

package com.itheima.domain;

import java.util.List;

public class PageBean {

	private List list; // 当前页内容 查询
	private int currPage; // 当前页码 传递
	private int pageSize; // 每页显示的条数 固定
	private int totalCount; // 总条数 查询
	private int totalPage; // 总页数 计算

	public List getList() {
		return list;
	}

	public void setList(List list) {
		this.list = list;
	}

	public int getCurrPage() {
		return currPage;
	}

	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public int getTotalPage() {
		return (int) Math.ceil(totalCount * 1.0 / pageSize);
	}

	public PageBean() {
	}

	public PageBean(List list, int currPage, int pageSize, int totalCount) {
		super();
		this.list = list;
		this.currPage = currPage;
		this.pageSize = pageSize;
		this.totalCount = totalCount;
	}

}

ShowProductsByPageServlet的代码:

package com.itheima.web.servlet;

import java.io.IOException;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.itheima.domain.PageBean;
import com.itheima.domain.Product;
import com.itheima.service.ProductService;

/**
 * 分页展示数据
 */
public class ShowProductsByPageServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		int currPage = Integer.parseInt(request.getParameter("currPage"));

		int pageSize = 3;

		PageBean bean = null;

		try {
			bean = new ProductService().showProductsByPage(currPage, pageSize);
		} catch (SQLException e) {
			e.printStackTrace();
		}

		request.setAttribute("pb", bean);

		request.getRequestDispatcher("/product_page.jsp").forward(request, response);

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}

ProductService的代码:

	/**
	 * 分页查询商品
	 * @param currPage 第几页
	 * @param pageSize 每页显示的条数
	 * @return
	 * @throws SQLException 
	 */
	public PageBean showProductsByPage(int currPage, int pageSize) throws SQLException {
		ProductDao dao=new ProductDao();
		
		List list=dao.findProductByPage(currPage,pageSize);
		
		int totalCount=dao.getCount();
		
		return new PageBean<>(list, currPage, pageSize, totalCount);
	}


ProductDao的代码:

	public List findProductByPage(int currPage, int pageSize) throws SQLException {
		QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
		String sql="select * from product limit ?,?";
		return qr.query(sql, new BeanListHandler<>(Product.class),(currPage-1)*pageSize,pageSize);
	}

	public int getCount() throws SQLException {
		QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
		String sql="select count(*) from product";
		return ((Long)qr.query(sql, new ScalarHandler<>())).intValue();
	}





你可能感兴趣的:(Java,Web)