###Jsp+Servlet购物商城day02.3:分页显示商品

来源:互联网 发布:最好的猫粮 知乎 编辑:程序博客网 时间:2024/05/21 08:52

功能入口:head.jsp导航栏。分类查询商品链接。===Ajax  js代码DOM拼接的

处理Servlet:ProductServlet

功能出口:productList.jsp


 功能入口:head.jsp导航栏。分类查询商品链接

<script type="text/javascript">//===页面加载完成,触发一个函数 $(function(){$.post("${pageContext.request.contextPath}/CategoryServlet",//请求路径{"method":"findAll"},//提交的数据function(v){//alert(v);//将数据设置进导航条 //<li><a href="#">电脑办公</a></li>for(var i = 0; i < v.length; i++){var cat = v[i];//{"cid":xxx,"cname":"yyyy"}$("#showCat").append("<li><a href='${pageContext.request.contextPath}/ProductServlet?method=findByCid&cid=" + cat.cid + "'>" + cat.cname + "</a></li>");}},//回调函数"json"//数据类型);}); </script>

处理Servlet:ProductServlet

实现步骤:

首先是数据库:mysql

select * from product where cid=?  limit start, size;

页面显示不仅仅是商品信息,还有当前页号,pageSize。所以封装了PageBean。


这里给了一个工具类PageBean。===当然可以自己写,很简单。

package cn.itcast.domain;import java.io.Serializable;import java.util.List;public class PageBean<T> implements Serializable{private int pageNumber;//当前页 ---前台传递private int pageSize;//一页显示多少条数据 ---后台自定义private int startIndex;//起始索引下标 --- 计算出来private int totalRecord;//总记录数---  查询出来private int totalPage; //总页数  ---  计算出来private List<T> result;//每一页的数据public PageBean() {super();// TODO Auto-generated constructor stub}public int getPageNumber() {return pageNumber;}public void setPageNumber(int pageNumber) {this.pageNumber = pageNumber;}public int getPageSize() {return pageSize;}public void setPageSize(int pageSize) {this.pageSize = pageSize;}public int getStartIndex() {return startIndex=(this.getPageNumber()-1)*this.getPageSize();}public void setStartIndex(int startIndex) {this.startIndex = startIndex;}public int getTotalRecord() {return totalRecord;}public void setTotalRecord(int totalRecord) {this.totalRecord = totalRecord;}public int getTotalPage() {return totalPage=(this.getTotalRecord()%this.getPageSize()==0?(this.getTotalRecord()/this.getPageSize()):(this.getTotalRecord()/this.getPageSize()+1));}public void setTotalPage(int totalPage) {this.totalPage = totalPage;}public List<T> getResult() {return result;}public void setResult(List<T> result) {this.result = result;}}
ProductServlet:

public String findByCid(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {ProductService ps = new ProductServiceImpl();String cid = request.getParameter("cid");String pagenum = request.getParameter("pageNum");if (pagenum == null) {pagenum="1";}try {PageBean pb = ps.findByPage(cid,pagenum );request.setAttribute("pb", pb);} catch (SQLException e) {e.printStackTrace();}return "/product_list.jsp";}
service:

@Overridepublic PageBean findByPage(String cid, String pagenum) throws SQLException {ProductDao pd  = new ProductDaoImpl();PageBean pbean = new PageBean(); pbean.setPageNumber(Integer.parseInt(pagenum)); pbean.setPageSize(12); pbean.setStartIndex((Integer.parseInt(pagenum)-1)*12); int totalRecord = pd.getCountByCid(cid); pbean.setTotalRecord(totalRecord); //int totalPage = totalRecord/5==0?totalRecord/5:totalRecord/5+1; //pbean.setTotalPage(totalPage );//===计算来的,不用set List<Product> list = pd.findByPage( cid, pbean); pbean.setResult(list);return pbean ;}

dao

@Overridepublic List<Product> findByPage(String cid, PageBean pb) throws SQLException {QueryRunner qr = new QueryRunner(C3p0Utils.getDataSource());String sql = "select * from product  where cid = ? limit ?,?";/* SQLException:  near ''0','5'' at line 1 Query:  select * from product  where cid = ? limit ?,? Parameters: [1, 0, 5]//String[]  params ={cid,  pb.getStartIndex()+"",  pb.getPageSize()+""};// * */Object[]  params ={cid,  pb.getStartIndex(),  pb.getPageSize()};List<Product> list = qr.query(sql , new BeanListHandler<Product>(Product.class),params );return list;}




阅读全文
0 0
原创粉丝点击