易買網是我為期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>
新聞熱點
疑難解答