jsp+Javabean+servlet实现分页查询
来源:互联网 发布:淘宝下载手机版 编辑:程序博客网 时间:2024/06/08 20:17
今天小编那写了个分页请大家参考
dao:对数据库进行操作public List<Product> findProduct(int start, int pageSize)throws SQLException {//用数据库连接池初始化runner对象QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());//sql进行分页查询String sql = "select * from product limit ?,?";return qr.query(sql, new BeanListHandler<Product>(Product.class),start, pageSize);}// 查询商品的总数public int findTotalCount() throws SQLException {QueryRunner qr = new QueryRunner(DataSourceUtils.getDataSource());String sql = "select count(*) from product";return ((Long) qr.query(sql, new ScalarHandler())).intValue();}service:进行逻辑判断private ProductDao pd = new ProductDao();public List<Product> findProduct(int currentPage, int pageSize) {try {//调用dao层方法查出当前页码号即(currentPage - 1) * pageSizereturn pd.findProduct((currentPage - 1) * pageSize, pageSize);} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return null;}//用pagesize做参数计算出最大页码号public int getMaxPage(int pageSize) {int totalCount;try {totalCount = pd.findTotalCount();//运用三目运算 如果总数/页数有余就让页码号加一否则不加return totalCount % pageSize > 0 ? totalCount / pageSize + 1: totalCount / pageSize;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();}return 0;}servlet:与前台进行交互private ProductService ps = new ProductService();private final int pageSize = 10;public void list(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {int currentPage = 1;if (request.getParameter("currentPage") != null) {currentPage = Integer.parseInt(request.getParameter("currentPage"));}int maxPage = ps.getMaxPage(pageSize);List<Product> list = ps.findProduct(currentPage, pageSize);request.setAttribute("list", list);request.setAttribute("currentPage", currentPage);request.setAttribute("maxPage", maxPage);request.getRequestDispatcher("/admin/product/list.jsp").forward(request, response);}前台遍历数据:<c:forEach items="${list}" var="product"><tr onmouseover="this.style.backgroundColor = 'white'"onmouseout="this.style.backgroundColor = '#F5FAFE';"><td style="CURSOR: hand; HEIGHT: 22px" align="center"width="18%">${product.pid}</td><td style="CURSOR: hand; HEIGHT: 22px" align="center"width="17%"><img width="40" height="45" src="${pageContext.request.contextPath}/${product.pimage}"></td><td style="CURSOR: hand; HEIGHT: 22px" align="center"width="17%">${product.pname}</td><td style="CURSOR: hand; HEIGHT: 22px" align="center"width="17%">${product.shop_price}</td><td style="CURSOR: hand; HEIGHT: 22px" align="center"width="17%">${product.is_hot}</td><td align="center" style="HEIGHT: 22px"><ahref="${ pageContext.request.contextPath }/product?method=toUpdate&&pid=${product.pid}"><imgsrc="${pageContext.request.contextPath}/images/i_edit.gif"border="0" style="CURSOR: hand"></a></td><td align="center" style="HEIGHT: 22px"><a href="${pageContext.request.contextPath}/product?method=delete&&pid=${product.pid}"> <imgsrc="${pageContext.request.contextPath}/images/i_del.gif"width="16" height="16" border="0" style="CURSOR: hand"></a></td></tr></c:forEach>Thank you for reading
阅读全文
1 0
- jsp+Javabean+servlet实现分页查询
- JSP+JavaBean+Servlet实现分页
- JSP+servlet+javabean实现分页
- jsp+servlet+javaBean实现分页
- JSP+Servlet+JavaBean实现分页
- jsp+servlet+javaBean怎样实现分页
- javaBean+servlet+jsp实现分页效果
- 使用jsp-javabean-servlet实现分页功能
- jsp+servlet+javabean实现数据分页
- JavaBean+Servlet+jsp真分页实现
- jsp+javabean实现分页
- 很好的jsp+servlet+javaBean实现数据分页代码例子
- MySQL数据库+jsp+servlet实现分页查询
- servlet+jsp实现分页查询+导出
- jsp之javaBean分页查询
- JSP+Servlet+JavaBean+JDBC实现用户登录,及成绩查询
- (jsp 和 servlet功能篇) jsp+servlet+javaBean 实现分页代码(mysql 数据库)
- JavaBean+Jsp 实现页面分页
- Java java.lang.Math数学包
- [算法分析与设计] leetcode 每周一题: 335. Self Crossing
- 2017.10.2 探寻本质
- String 常量池的使用
- HDU
- jsp+Javabean+servlet实现分页查询
- MyBatis3.x和Spring3.x的整合
- NGUI
- 【转】Rancher 2.0:K8s之上的Rancher魔法
- Java EE开发第十二章:2.Servlet之Request请求(请求行、头、体、转发,转发和重定向区别,中文乱码)
- “低配版”小米MIX 2发布 网友:雷军看了想打人
- 十一送iPhoneX、大疆无人机!还能免费修手机!够给力吧!
- html大吐槽 1结构解析
- [BZOJ4827][HNOI2017]礼物(FFT)