商品信息的分页实现

首先准备一个vo类(value object)

(声明:以下为主要所需代码)

package cn.jhome.domain;

import java.util.ArrayList;
import java.util.List;
public class PageBean {
// 当前页码
private int currentPage;
// 每页显示的商品数
private int currentCount;
// 总商品数
private int totalCount;
// 总页码数
private int totalPage;
// 每页显示的商品信息
private List productList = new ArrayList();
public int getCurrentPage() {
return currentPage;
}

public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}

public int getCurrentCount() {
return currentCount;
}


public void setCurrentCount(int currentCount) {
this.currentCount = currentCount;
}


public int getTotalCount() {
return totalCount;
}

public void setTotalCount(int totalCount) {
this.totalCount = totalCount;
}

public int getTotalPage() {
return totalPage;
}


public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}

public List getProductList() {
return productList;
}

public void setProductList(List productList) {
this.productList = productList;
}

}

-------------------------------------------------------------------------------------

web层代码

package cn.jhome.web;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.jhome.domain.PageBean;
import cn.jhome.domain.Product;
import cn.jhome.service.ProductService;
import cn.jhome.service.impl.ProductServiceImpl;

public class FindProductsByPageServlet extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//模拟当前页面为1
// int currentPage = 1;
String currentPageStr = request.getParameter("currentPage");
if(currentPageStr==null) currentPageStr = "1";
int currentPage = Integer.parseInt(currentPageStr);
//定义当前页显示商品信息的条数
int currentCount = 12;
//获取总商品数
ProductService productService = new ProductServiceImpl();
PageBean pageBean = productService.findPageBean(currentPage, currentCount);

//转发到product_list页面
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);

}

public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}

}

-----------------------------------------------------------------------------------------------------------------

service层主要代码

//分页操作
public PageBean findPageBean(int currentPage,int currentCount) {
//目的:就是想办法封装一个PageBean并返回
PageBean pageBean = new PageBean();
//当前页码
pageBean.setCurrentPage(currentPage);
//每页显示的商品数
pageBean.setCurrentCount(currentCount);
//总商品数
int totalCount = productDao.getTotalCount();
pageBean.setTotalCount(totalCount);
//总页码数
/*
* 总条数 当前页显示的条数 总页数
* 10 4 3
* 11 4 3
* 12 4 3
* 13 4 4

* 公式:总页数 = 总条数%当前显示的条数==0 ?总条数/当前显示条数 : 总条数/当前显示条数+1; 

*/
//取余为0 刚好为0  就直接取整, 若取余不为0 取整后+1
int totalPage = totalCount%currentCount == 0 ? totalCount/currentCount : ((totalCount/currentCount)+1);
pageBean.setTotalPage(totalPage);

//每页显示的商品信息
/*
* 页数与limit起始索引的关系
* 例如 每页显示4条
* 页数 其实索引 每页显示商品数
* 1 0 4
* 2 4 4
* 3 8 4
* 4 12 4

* 索引index = (当前页数-1)*每页显示的条数

*/
int index = (currentPage - 1)*currentCount;
List productList = productDao.findProductsByPage(index, currentCount);
pageBean.setProductList(productList);

return pageBean;

}

--------------------------------------------------------------------------------------------------------------

dao层主要代码

//获取总商品数

@Override
public int getTotalCount() {
String sql = "select count(*) from product";
Long query = null;
try {
query = (Long) queryRunner.query(sql, new ScalarHandler());
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("获取商品总数异常");
}
return query.intValue();
}


@Override
    public List findProductsByPage(int index, int currentCount) {
String sql = "select * from product limit ?,?";
List productList = null;
try {
productList = queryRunner.query(sql, new BeanListHandler(Product.class),             index,currentCount);
} catch (SQLException e) {
e.printStackTrace();
throw new RuntimeException("分页查询异常");
}
return productList;

}

-------------------------------------------------------------------------------------------------

jsp代码解析

展示商品信息代码块:

//${pageBean.productList } 取出request域中存储的pageBean的productList元素即每页显示的商品信息

//${pageContext.request.contextPath }/${product.pimage }  当前项目名称/图片 ${product.pimage } 可以理解成 product.getPimage();<-----后面依次类推 



分页代码块:

//解析:

//pageBean.currentPage 取出request域中存储的pageBean的currentPage元素即当前页

//javascript:void(0); 设置为点不了,额 ,反正我是这么理解

/*

这个是重点***,重点字面意思 当前项目名称/servlet(分页查询的servlet)?当前页名=当前页-1;

解析:你点击这个a标签是,将执行当前项目下findProductsByPage这个servlet,currentPage实质上是传递当前页的参数过去,此servlet接收到参数之后,就会进行分页查询操作,最后显示对应页面的商品信息。

*/


特别提示:本jsp页面是用Bootstrap做的......上述代码为主要分页操作的代码。

你可能感兴趣的:(Java)