1.index.jsp
<h2><a href="${pageContext.request.contextPath }/showProductsByPage?currPage=1">分页展示商品</a></h2>
2.Servlet
public class ShowProductsByPageServlet extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request,response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//接收第几页
int currPage=Integer.parseInt(request.getParameter("currPage"));
//设置每页显示的条数
int pageSize=4;
//调用service
ProductService productServcie=new ProductServiceImpl();
PageBean<Product>bean=null;
try {
bean=productServcie.showProductByPage(currPage,pageSize);
} catch (Exception e) {
e.printStackTrace();
}
//将当前页的数据放入域中
request.setAttribute("pb", bean);
//转发到分页的页面
request.getRequestDispatcher("/product_page.jsp").forward(request, response);
}
}
3.Service
public PageBean<Product> showProductByPage(int currPage, int pageSize) throws SQLException {
//展示当前页的数据 =limit (currPage-1)*pageSize,pageSize
ProductDao productDao=new ProductDao();
List<Product>list=productDao.findByCurrPage(currPage,pageSize);
//查询总条数
int totalCount=productDao.getTotalCount();
return new PageBean<>(list, currPage, pageSize, totalCount);
}
4.Dao
/**
* 获取当前页的数据
* @param currPage
* @param pageSize
* @return
* @throws SQLException
*/
public List<Product> findByCurrPage(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);
}
/**
* 获取总页数
* @return
* @throws SQLException
*/
public int getTotalCount() throws SQLException {
QueryRunner qr=new QueryRunner(DataSourceUtils.getDataSource());
String sql="select count(*) from product";
return ((Long)qr.query(sql, new ScalarHandler())).intValue();
}
5.page.jsp
<body>
<table border="1" align="center" width="88%">
<tr>
<th>pid</th>
<th>商品名称</th>
<th>商品价格</th>
<th>商品描述</th>
</tr>
<c:forEach items="${pb.list }" var="p">
<tr>
<td width='8%'>${p.id }</td>
<td width='8%'>${p.pname }</td>
<td width='8%'>${p.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>
<!-- 将所有的页码展示出来 -->
<c:forEach begin="1" end="${pb.totalPage }" var="n">
<!-- 若是当前页 不可点 -->
<c:if test="${pb.currPage == n }">
${n }
</c:if>
<!-- 若不是当前页 可点 -->
<c:if test="${pb.currPage != n }">
<a href="${pageContext.request.contextPath}/showProductsByPage?currPage=${n}">${n }</a>
</c:if>
</c:forEach>
<!-- 若是最后一页 尾页和下一页不展示 -->
<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>
6、PageBean
package cn.itcast.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;
}
/**
**
* 获取总页数
* @return 总页数
*/
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;
}
}
web分页实现
猜你喜欢
转载自blog.csdn.net/qq1424035130/article/details/82870939
今日推荐
周排行