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


原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 淘宝店铺动态评分一直很低怎么办? 刚生下来的宝宝睡觉不吃奶怎么办 宝宝吐奶咳嗽了可能被呛到怎么办 床下面的液压支架支撑不了了怎么办 沙发的海绵垫坐着感觉太硬怎么办 修公路占地补偿盖房子房产证怎么办 哺乳期乳房有硬块挤不出来怎么办 老公投的公司倒闭了钱怎么办 小丑鱼老在缸上边游不下去怎么办 海缸养珊瑚又想养蝴蝶鱼怎么办 被几个月的小狗咬了怎么办 木本植物继代长出大量愈伤怎么办 钱兜树叶子发黄掉叶怎么办 手机qq群200人满了怎么办 支票被背书人的章盖浅了怎么办 没病装病的心理病患者怎么办 20多岁被骗40多万怎么办 一个学生上课爱捣乱班级课堂怎么办 军训的时候大姨妈来了怎么办 笔记本连接无线网说链接限制怎么办 12306密码和邮箱都忘了怎么办 烦恼都是自找的新书散页了怎么办 白色衣服被红色衣服染了怎么办 手机微信里面不能拍手功怎么办 肇事逃逸轻伤且对方不私了怎么办 数学差怎么办脑子不好啊数学学不老 手机号丢失微信密码丢失怎么办找回 悦借钱输入五次密码被锁怎么办 生源地助学贷款密码忘了怎么办 生源地助学贷款登录密码忘了怎么办 助学贷款支付宝密码忘了怎么办 微信密码忘记了手机号也换了怎么办 换手机号了微信密码忘记了怎么办 qq密码忘记了手机号也换了怎么办 一年只能修改一次昵称我能怎么办 华为手机的账号和密码丢失怎么办 华为手机的账号和密码丢失了怎么办 手机号被别人注册了微博怎么办 微博账号一天内多次解冻怎么办 露娜注册时邮箱填错了怎么办 苹果4s手机显示已停用怎么办