易买网是我为期2个星期的一个项目,下面简单介绍一下商品分页。
第一步:创建商品实体类
package entity;public class easybuy_PRoductn { private int ep_id ;//编号 private String ep_name ;//名字 private String ep_description ;//描述 private int ep_price;//价格 private int ep_stock ;//库存 private int epc_id ;//父类id private int epc_child_id ;//二级id private String ep_file_name ;//文件名 private int tiaoma ;//条码号 public int getEp_id() { return ep_id; } public void setEp_id(int epId) { ep_id = epId; } public String getEp_name() { return ep_name; } public void setEp_name(String epName) { ep_name = epName; } public String getEp_description() { return ep_description; } public void setEp_description(String epDescription) { ep_description = epDescription; } public int getEp_price() { return ep_price; } public void setEp_price(int epPrice) { ep_price = epPrice; } public int getEp_stock() { return ep_stock; } public void setEp_stock(int epStock) { ep_stock = epStock; } public int getEpc_id() { return epc_id; } public void setEpc_id(int epcId) { epc_id = epcId; } public int getEpc_child_id() { return epc_child_id; } public void setEpc_child_id(int epcChildId) { epc_child_id = epcChildId; } public String getEp_file_name() { return ep_file_name; } public void setEp_file_name(String epFileName) { ep_file_name = epFileName; } public int getTiaoma() { return tiaoma; } public void setTiaoma(int tiaoma) { this.tiaoma = tiaoma; }}
第二步:编写SQL语句
public List<easybuy_productn> findproductList() { List<easybuy_productn> listck = new ArrayList<easybuy_productn>(); con = super.getConnection(); try { st = con.prepareStatement("select * from easybuy_productn"); rs = st.executeQuery(); while(rs.next()){ easybuy_productn product = new easybuy_productn(); product.setEp_id(rs.getInt(1)); product.setEp_name(rs.getString(2)); product.setEp_description(rs.getString(3)); product.setEp_price(rs.getInt(4)); product.setEp_stock(rs.getInt(5)); product.setEpc_id(rs.getInt(6)); product.setEpc_child_id(rs.getInt(7)); product.setEp_file_name(rs.getString(8)); product.setTiaoma(rs.getInt(9)); listck.add(product); } } catch (SQLException e) { e.printStackTrace(); }finally { super.ShiFang(rs, st, con); } return listck; }
第三步:分页语句:
/* * 分页 */public List<easybuy_productn> fengye(int pageIndex,int pageSize){ con=super.getConnection(); String sql="select * from (select t.*,rownum r from "+"(select * from easybuy_productn order by ep_id desc)"+"t where rownum <= "+(pageIndex*pageSize)+") where r>"+(pageSize*(pageIndex-1));try {st=con.prepareStatement(sql);productlist = new ArrayList<easybuy_productn>();rs=st.executeQuery();while(rs.next()){ easybuy_productn product = new easybuy_productn(); product.setEp_id(rs.getInt(1)); product.setEp_name(rs.getString(2)); product.setEp_description(rs.getString(3)); product.setEp_price(rs.getInt(4)); product.setEp_stock(rs.getInt(5)); product.setEpc_id(rs.getInt(6)); product.setEpc_child_id(rs.getInt(7)); product.setEp_file_name(rs.getString(8)); product.setTiaoma(rs.getInt(9)); productlist.add(product);}return productlist;} catch (SQLException e) {// TODO Auto-generated catch blocke.printStackTrace();return null; } }//商品总数除于要分的条数public int getTotalPages(int count,int pageSize){ int totalpages=0; totalpages=(count%pageSize==0)?(count/pageSize):(count/pageSize+1); return totalpages;}//获取商品总条数public int getConut(){ int count=0; con=super.getConnection(); String sql="select * from easybuy_productn"; try { st=con.prepareStatement(sql); rs=st.executeQuery(); while(rs.next()){ count++; } return count; } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); return 0; } }
第四步:jsp页面
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%><%@page import="entity.easybuy_productn"%><%@page import="Biz.Impl.productnBizImpl"%><%@page import="Biz.productnBiz"%><%@page import="Dao.Impl.productnDaoImpl"%><%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%><%@page import="entity.easybuy_product"%><%@page import="Biz.Impl.productBizImpl"%><%@page import="Biz.productBiz"%><%@page import="Dao.Impl.productDaoImpl"%><%String path = request.getContextPath();String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";%> <% request.setCharacterEncoding("utf-8"); String name=(String)session.getAttribute("name"); %> <%//==============分页=============== //设置新闻显示条数 int pageSize=5;//实例化productnDaoImpl newxw=new productnDaoImpl(); //获取数据库有多少条数据int count=newxw.getConut();//获取页码String page1=request.getParameter("pageIndex");//得到具体要分的页int pag=newxw.getTotalPages(newxw.getConut(),pageSize);//得到当前页int pageIndex=0;//判断得到的值是否有值if(page1==null){ pageIndex=1; //查询 }else{ //把当前页赋值给pageIndex pageIndex=Integer.parseInt(page1); //判断当前页是否为最大页 if(pageIndex>pag){ pageIndex=pag; } }List<easybuy_productn> product=newxw.fengye(pageIndex,pageSize);request.setAttribute("list",product);%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html> <head> <base href="<%=basePath%>"><title>后台管理 - 易买网</title><link type="text/CSS" rel="stylesheet" href="css/style.css" /><script type="text/Javascript">function Delete(id){ if(confirm("确定要删除吗?")) { location.href = "delectproduct?id=" + id; }}</script><script type="text/javascript" src="../scripts/function-manage.js"></script></head><body><div id="header" class="wrap"> <div id="logo"><img src="images/logo.gif" /></div> <div class="help"><a href="index.jsp">返回前台页面</a></div> <div class="navbar"> <ul class="clearfix"> <li><a href="manage/index.jsp">首页</a></li> <li><a href="manage/user.jsp">用户</a></li> <li class="current"><a href="manage/product.jsp">商品</a></li> <li><a href="manage/order.jsp">订单</a></li> <li><a href="manage/guestbook.jsp">留言</a></li> <li><a href="manage/news.jsp">新闻</a></li> </ul> </div></div><div id="childNav"> <div class="welcome wrap"> 管理员:<%=name%> 您好,今天是2015-06-12,欢迎回到管理后台。 </div></div><div id="position" class="wrap"> 您现在的位置:<a href="manage/index.jsp">易买网</a> > 管理后台</div><div id="main" class="wrap"> <div id="menu-mng" class="lefter"> <div class="box"> <dl> <dt>用户管理</dt> <dd><em><a href="manage/user-add.jsp">新增</a></em><a href="manage/user.jsp">用户管理</a></dd> <dt>商品信息</dt> <dd><em><a href="manage/productClass-add.jsp">新增</a></em><a href="manage/productClass.jsp">分类管理</a></dd> <dd><em><a href="manage/product-add.jsp">新增</a></em><a href="manage/product.jsp">商品管理</a></dd> <dt>订单管理</dt> <dd><a href="manage/order.jsp">订单管理</a></dd> <dt>留言管理</dt> <dd><a href="manage/guestbook.jsp">留言管理</a></dd> <dt>新闻管理</dt> <dd><em><a href="manage/news-add.jsp">新增</a></em><a href="manage/news.jsp">新闻管理</a></dd> </dl> </div> </div> <div class="main"> <h2>商品管理</h2> <div class="manage"> <table class="list"> <% //创建用户业务逻辑对象 productnBiz prodctbiz = new productnBizImpl(); List<easybuy_productn> product1 = prodctbiz.findproductList(); request.setAttribute("product",product);%> <tr> <td>ID</td> <td>商品名称</td> <td>操作</td> </tr> <c:forEach var="news" items="${requestScope.product}" > <tr> <td class="first w4 c">${news.ep_id}</td> <td class="thumb"><img src="${news.ep_file_name}" width="50px"/><a href="product-view.jsp" target="_blank">${news.ep_name}</a></td> <td class="w1 c"><a href="manage/product-modify.jsp?id=${news.ep_id}">修改</a> <a href="javascript:Delete(${news.ep_id})">删除</a></td> </tr> </c:forEach> </table> <div class="pager"> <ul class="clearfix"> <li>当前页数:[<%=pageIndex %>/<%=pag%>] <% //判断当前页是否为末页 if(pageIndex>1){ %> <a href="manage/product.jsp?pageIndex=<%=1 %>"> 首页</a> <a href="manage/product.jsp?pageIndex=<%=pageIndex-1%>"> 上一页 </a> <% } //判断当前页是否为末页 if(pageIndex<pag){ %> <a href="manage/product.jsp?pageIndex=<%=pageIndex+1 %>"> 下一页</a> <a href="manage/product.jsp?pageIndex=<%=pag%>"> 末页 </a> <% } %></li> </ul> </div> </div> </div> <div class="clear"></div></div><div id="footer"> Copyright © 2010 易买网络 All Rights Reserved. 京ICP证1000001号</div></body></html>
新闻热点
疑难解答