商品信息的分页实现

该博客详细介绍了如何使用JAVA实现商品信息的分页展示。从VO类`PageBean`的定义,到Web层的Servlet处理请求,再到Service层的分页逻辑,以及DAO层的数据库查询,最后在JSP页面中展示分页结果。整个流程涵盖从后台到前台的完整分页实现。
摘要由CSDN通过智能技术生成

首先准备一个vo类(value object)

(声明:以下为主要所需代码)

package cn.jhome.domain;

import java.util.ArrayList;
import java.util.List;
public class PageBean<T> {
// 当前页码
private int currentPage;
// 每页显示的商品数
private int currentCount;
// 总商品数
private int totalCount;
// 总页码数
private int totalPage;
// 每页显示的商品信息
private List<T> productList = new ArrayList<T>();
public int getCurrentPage() {
return currentPage;
}

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}

public int getCurrentCount() {
return currentCount;
}


public void setCurrentCount(int currentCount) {
this.currentCount = currentCount;
}


public int getTotalCount() {
return totalCount;
}

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

public int getTotalPage() {
return totalPage;
}


public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}

public List<T> getProductList() {
return productList;
}

public void setProductList(List<T> productList) {
this.productList = productList;
}

}

-------------------------------------------------------------------------------------

web层代码

package cn.jhome.web;

import java.io.IOException;

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

import cn.jhome.domain.PageBean;
import cn.jhome.domain.Product;
import cn.jhome.service.ProductService;
import cn.jhome.service.impl.ProductServiceImpl;

public class FindProductsByPageServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//模拟当前页面为1
// int currentPage = 1;
String currentPageStr = request.getParameter("currentPage");
if(currentPageStr==null) currentPageStr = "1";
int currentPage = Integer.parseInt(currentPageStr);
//定义当前页显示商品信息的条数
int currentCount = 12;
//获取总商品数
ProductService productService = new ProductServiceImpl();
PageBean<Product> pageBean = productService.findPageBean(currentPage, currentCount);

//转发到product_list页面
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);

}

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

}

-----------------------------------------------------------------------------------------------------------------

service层主要代码

//分页操作
public PageBean<Product> findPageBean(int currentPage,int currentCount) {
//目的:就是想办法封装一个PageBean并返回
PageBean<Product> pageBean = new PageBean<Product>();
//当前页码
pageBean.setCurrentPage(currentPage);
//每页显示的商品数
pageBean.setCurrentCount(currentCount);
//总商品数
int totalCount = productDao.getTotalCount();
pageBean.setTotalCount(totalCount);
//总页码数
/*
* 总条数 当前页显示的条数 总页数
* 10 4 3
* 11 4 3
* 12 4 3
* 13 4 4

* 公式:总页数 = 总条数%当前显示的条数==0 ?总条数/当前显示条数 : 总条数/当前显示条数+1; 

*/
//取余为0 刚好为0  就直接取整, 若取余不为0 取整后+1
int totalPage = totalCount%currentCount == 0 ? totalCount/currentCount : ((totalCount/currentCount)+1);
pageBean.setTotalPage(totalPage);

//每页显示的商品信息
/*
* 页数与limit起始索引的关系
* 例如 每页显示4条
* 页数 其实索引 每页显示商品数
* 1 0 4
* 2 4 4
* 3 8 4
* 4 12 4

* 索引index = (当前页数-1)*每页显示的条数

*/
int index = (currentPage - 1)*currentCount;
List<Product> productList = productDao.findProductsByPage(index, currentCount);
pageBean.setProductList(productList);

return pageBean;

}

--------------------------------------------------------------------------------------------------------------

dao层主要代码

//获取总商品数

@Override
public int getTotalCount() {
String sql = "select count(*) from product";
Long query = null;
try {
query = (Long) queryRunner.query(sql, new ScalarHandler());
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("获取商品总数异常");
}
return query.intValue();
}


@Override
    public List<Product> findProductsByPage(int index, int currentCount) {
String sql = "select * from product limit ?,?";
List<Product> productList = null;
try {
productList = queryRunner.query(sql, new BeanListHandler<Product>(Product.class),             index,currentCount);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("分页查询异常");
}
return productList;

}

-------------------------------------------------------------------------------------------------

jsp代码解析

展示商品信息代码块:

//${pageBean.productList } 取出request域中存储的pageBean的productList元素即每页显示的商品信息

//${pageContext.request.contextPath }/${product.pimage }  当前项目名称/图片 ${product.pimage } 可以理解成 product.getPimage();<-----后面依次类推 

<c:forEach items="${pageBean.productList }" var="product">
<div class="col-md-2" style="height:250px">
<a href="product_info.htm"> <img src="${pageContext.request.contextPath }/${product.pimage }"
width="170" height="170" style="display: inline-block;">
</a>
<p>
<a href="product_info.html" style='color: green'>${product.pname }</a>
</p>
<p>
<font color="#FF0000">商城价:&yen;${product.shop_price }</font>
</p>
</div>

</c:forEach>

分页代码块:

<!--分页 -->

//解析:

//pageBean.currentPage 取出request域中存储的pageBean的currentPage元素即当前页

//javascript:void(0); 设置为点不了,额 ,反正我是这么理解

/*<a href="${pageContext.request.contextPath }/findProductsByPage?currentPage=${pageBean.currentPage-1 }" aria-label="Previous"></a>

这个是重点***,重点字面意思 当前项目名称/servlet(分页查询的servlet)?当前页名=当前页-1;

解析:你点击这个a标签是,将执行当前项目下findProductsByPage这个servlet,currentPage实质上是传递当前页的参数过去,此servlet接收到参数之后,就会进行分页查询操作,最后显示对应页面的商品信息。

*/

<div style="width: 380px; margin: 0 auto; margin-top: 50px;">
<ul class="pagination" style="text-align: center; margin-top: 10px;">
<!-- 判断是否为第一页 -->
<c:if test="${pageBean.currentPage == 1 }">
<li class="disabled">
<a href="javascript:void(0);" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
</c:if>
<c:if test="${pageBean.currentPage != 1 }">
<li>
<a href="${pageContext.request.contextPath }/findProductsByPage?currentPage=${pageBean.currentPage-1 }" aria-label="Previous">
<span aria-hidden="true">&laquo;</span>
</a>
</li>
</c:if>

<c:forEach begin="1" end="${pageBean.totalPage }" var="page">
<!-- 判断是否为当前页 -->
<%-- <c:if test="${pageBean.currentPage = page }">
<li class="active"><a href="#">${page }</a></li>
</c:if>
<c:if test="${pageBean.currentPage != page }">
<li class="active"><a href="#">${page }</a></li>
</c:if> --%>
<li ${pageBean.currentPage == page? "class='active'":""} ><a href="${pageContext.request.contextPath }/findProductsByPage?currentPage=${page }">${page }</a></li>
</c:forEach>

<!-- 判断是否为最后一页 -->
<c:if test="${pageBean.currentPage == pageBean.totalPage }">
<li class="disable">
<a href="javascript:void(0);" aria-label="Next"> 
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</c:if>
<c:if test="${pageBean.currentPage != pageBean.totalPage }">
<li>
<a href="${pageContext.request.contextPath }/findProductsByPage?currentPage=${pageBean.currentPage+1 }" aria-label="Next"> 
<span aria-hidden="true">&raquo;</span>
</a>
</li>
</c:if>

</ul>
</div>

<!-- 分页结束 -->

特别提示:本jsp页面是用Bootstrap做的......上述代码为主要分页操作的代码。

《HTT电脑秘书软件》是一个小巧实用的工具,通过它可以方便地打开您常用的程序、文档资料、以及网站链接等。《HTT电脑秘书软件》 的最大好处,莫过于它的易用性。当然,它还有许多特点: 1、极其简单的打开/隐藏方式:鼠标移向屏幕右下角,《HTT电脑秘书软件》 主窗口马上打开;鼠标移向屏幕右边界下方,主窗口立即消失,移到右边界上方,主窗口立即显示。也可以通过单击右下角的本系统图标进行打开和隐藏。 2、允许对程序项进行分门别类,用户可建立任意树形目录结构,并提供两种目录显示方式。 3、可以对《HTT电脑秘书软件》中的任何程序文档等项目定义系统级热键,无论《HTT电脑秘书软件》是否在前台,均可捕获热键。 4、有自启动功能,每次进入Windows后,《HTT电脑秘书软件》 以小图标方式显示在任务条上的通知区中。 5、支持OLE拖放功能,您可将任意文件或IE/Netscape中的超链接拖入《HTT电脑秘书软件》中。 6、提供多种小巧实用的工具,如日程记事、HTT文档编辑器,HTT图片编辑器、Office文档编辑器等,并允许以插件方式加以扩充。 7、可以通过拖放方式,将你的系统中的所有文件添加到【文档库】节点(该节点对应本系统所在路径下的文件夹),将你系统的所有文件进行集中管理。 《HTT电脑秘书软件》 的插件编程接口: 如果你需要给本系统编写插件,请与我们公司联系。 E-mail: htt@huitetong.com WWW: http://www.huitetong.com Tel: 010-82896850 13801355031 作者:赵宝泽
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值