您的位置:首页 > Web前端 > JavaScript

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

2017-08-25 09:40 1211 查看
前端JSP页面的代码:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
</head>
<body>
<table border="2" style="border-collapse: collapse" bordercolor="blue"
width="88%" align="center">
<tr>
<th>pid</th>
<th>商品图片</th>
<th>商品名称</th>
<th>市场价</th>
<th>商城价</th>
<th>商品描述</th>
</tr>
<c:forEach items="${pb.list }" var="p">
<tr>
<td width="8%">${p.pid}</td>
<td width="8%"><img width="80px"
src="${pageContext.request.contextPath}/${p.pimage}" /></td>
<td width="8%">${p.pname}</td>
<td width="8%">${p.market_price}</td>
<td width="8%">${p.shop_price}</td>
<td>${p.pdesc}</td>
</tr>
</c:forEach>
</table>
<center>
<c:if test="${pb.currPage!=1 }">
<a href="${pageContext.request.contextPath}/showProductsByPage?currPage=1">[首页]</a>
<a href="${pageContext.request.contextPath}/showProductsByPage?currPage=${pb.currPage-1}">[上一页]</a>
</c:if>

<!-- 1-10页码展示 -->
<!-- 如果总页数大于10页 -->
<c:if test="${pb.totalPage>10}">
<!-- 如果当前页面大于等于(1)&&小于等于(6) -->
<c:if test="${(pb.currPage>=1)&&(pb.currPage<=6 )}">
<c:forEach begin="1" end="10" var="n">
<a href="${pageContext.request.contextPath}/showProductsByPage?currPage=${n}">${n}</a>
</c:forEach>
</c:if>
<!-- 如果当前数大于等于(总页数-4)&&小于等于(总页数) -->
<c:if test="${(pb.currPage>=(pb.totalPage-4))&&(pb.currPage<=pb.totalPage) }">
<c:forEach begin="${ pb.totalPage-9}" end="${pb.totalPage }" var="n">
<a href="${pageContext.request.contextPath}/showProductsByPage?currPage=${n}">${n}</a>
</c:forEach>
</c:if>
<!-- 如果当前数大于(6)&&小于总页数-4) -->
<c:if test="${(pb.currPage>6)&&(pb.currPage<(pb.totalPage-4))}">
<c:forEach begin="${ pb.currPage-5}" end="${pb.currPage+4 }" var="n">
<a href="${pageContext.request.contextPath}/showProductsByPage?currPage=${n}">${n}</a>
</c:forEach>
</c:if>
</c:if>
<!-- 如果总页数小于10页 -->
<c:if test="${pb.totalPage<=10 }">
<c:forEach begin="1" end="${pb.totalPage }" var="n">
<a href="${pageContext.request.contextPath}/showProductsByPage?currPage=${n}">${n}</a>
</c:forEach>
</c:if>

<c:if test="${pb.currPage!=pb.totalPage }">
<a href="${pageContext.request.contextPath}/showProductsByPage?currPage=${pb.currPage+1}">[下一页]</a>
<a href="${pageContext.request.contextPath}/showProductsByPage?currPage=${pb.totalPage}">[尾页]</a>
</c:if>

  第${pb.currPage }页/共${pb.totalPage }页
</center>
</body>
</html>


商品详情显示的代码:



首页 上一页的代码



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



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



分页效果显示:



后端的代码:

PageBean的代码:

package com.itheima.domain;

import java.util.List;

public class PageBean<T> {

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

public List<T> getList() {
return list;
}

public void setList(List<T> 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<T> 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<Product> 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<Product> showProductsByPage(int currPage, int pageSize) throws SQLException {
ProductDao dao=new ProductDao();

List<Product> list=dao.findProductByPage(currPage,pageSize);

int totalCount=dao.getCount();

return new PageBean<>(list, currPage, pageSize, totalCount);
}


ProductDao的代码:

public List<Product> 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();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐