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

功能入口: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:

@Override
	public 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

@Override
	public 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值