web21 分页
2017-10-08 本文已影响0人
路人爱早茶
- eclipse中是workspace的名字,修改工程名字需注意发布路径(真正在服务器内部名字)改没有:右键项目-properties
- vo(value object)专门传递值区别于实体
- str.trim()去掉字符串空格
- 数组在创建时候必须定义大小并且定义几个放几个而集合不限制
- 集合转数组(list.toarray)
---------注意搜索语句多个不确定书写,注意拼接空格
public List<Product> look(VO vo) throws SQLException {
QueryRunner qr = new QueryRunner(MyCurrentConn.getCombpdatasource());
String sql="select *from product where 1=1 ";
ArrayList<Object> arr = new ArrayList<>();
if (vo.getPname()!=null&&!vo.getPname().trim().equals("")) {
sql+=" and pname like ? ";
arr.add("%"+vo.getPname().trim()+"%");
}
if (vo.getCid()!=null&&!vo.getCid().trim().equals("")) {
sql+=" and cid=? ";
arr.add(vo.getCid().trim());
}
if (vo.getIs_hot()!=null&&!vo.getIs_hot().trim().equals("")) {
sql+=" and is_hot=?";
arr.add(vo.getIs_hot().trim());
}
List<Product> products =qr.query(sql, new BeanListHandler<Product>(Product.class),arr.toArray());
return products;
}
------------------jQuery获得
$(function() {
$("#is_hot option[value='${vo.is_hot}']").prop("selected", true);
$("#cid option[value='${vo.cid}']").prop("selected", true);
})
- 出现找不到文件的缓存错误就project-转换一下自动手动编译
- jquery记得导包(<script type="text/javascript" src="${pageContext.request.contextPath }/js/jquery-1.11.3.min.js"></script>)
- 分页的数据封装
public class PageBean<T> {
private int currentPage;
private int currentNum;
private int totalPage;
private int totalNum;
//每页显示数据
private List<T> currentPageProduct=new ArrayList<T>();
- select count (*)from product;返回是一个long型
- math.ceil(总页数/总条数)此为向上取整
- limit ?,? 起始索引,当页条数
- 分页
------------web-获得点击页数,每页数据写死, 将返回数据list,页数,当前页所有信息封装
- Integer.parseInt(currentPageStr);转化int
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String currentPageStr = request.getParameter("page");
if (currentPageStr == null) {
currentPageStr = "1";
}
int currentPage = Integer.parseInt(currentPageStr);
int currentCount = 12;
ProductListSer proListSer = new ProductListSer();
PageBean<Product> pageBean = null;
try {
pageBean = proListSer.getPage(currentPage, currentCount);
} catch (SQLException e) {
e.printStackTrace();
}
request.setAttribute("pageBean", pageBean);
request.getRequestDispatcher("/product_list.jsp").forward(request, response);
}
----------------ser层将web层和dao层数据封装返回web层对象
public class ProductListSer {
public PageBean<Product> getPage(int currentPage, int currentCount) throws SQLException {
// 获得总条数
// 获得分页显示数据:起始索引=(当前页-1)*当前页条数
// 总页数:math.ceil(总条数/每页条数)有小数进一位
ProductDao pDao = new ProductDao();
int totalCount= pDao.getTotalCount();
PageBean<Product> pageBean = new PageBean<>();
pageBean.setCurrentNum(currentCount);
pageBean.setCurrentPage(currentPage);
pageBean.setTotalNum(totalCount);
pageBean.setTotalPage((int)Math.ceil(1.0*totalCount/currentCount));
int index=(currentPage-1)*currentCount;
pageBean.setCurrentPageProduct(pDao.getCurrentPagePro(index,currentCount));
return pageBean;
}
}
--------------dao层,获取总数据条数,limit返回每页数据
- qr.query(sql, new ScalarHandler());返回long型
- "select * from product limit ?,? ";参数是:起始索引,返回条数
public int getTotalCount() throws SQLException {
QueryRunner qr = new QueryRunner(MyCurrentConn.getCombpdatasource());
String sql="select count(*) from product ";
Long totalCount=(Long) qr.query(sql, new ScalarHandler());
return totalCount.intValue();
}
public List<Product> getCurrentPagePro(int index,int currentCount) throws SQLException {
QueryRunner qr = new QueryRunner(MyCurrentConn.getCombpdatasource());
String sql="select * from product limit ?,? ";
return qr.query(sql, new BeanListHandler<Product>(Product.class),index,currentCount);
}
---------------------jsp页面,分页是三部分,中间点击页面数,前进和后退,都需要判断当前页跳转将要页面page带回
<c:forEach items="${pageBean.currentPageProduct }" var="pro">
<div class="col-md-2" style="height: 250px">
<a href="product_info.htm"> <img
src="${pageContext.request.contextPath }/${pro.pimage}" width="170"
height="170" style="display: inline-block;">
</a>
<p>
<a href="product_info.html" style='color: green'>${pro.pname}</a>
</p>
<p>
<font color="#FF0000">商城价:¥${pro.shop_price}</font>
</p>
</div>
</c:forEach>
</div>
<!--分页 -->
<div style="width: 380px; margin: 0 auto; margin-top: 50px;">
<ul class="pagination" style="text-align: center; margin-top: 10px;">
<!-- 判断返回 -->
<c:if test="${pageBean.currentPage==1 }">
<li class="disabled"><a href="javascript:void(0);"
aria-label="Previous"><span aria-hidden="true">«</span></a></li>
</c:if>
<c:if test="${pageBean.currentPage!=1 }">
<li><a
href="${pageContext.request.contextPath }/productList?page=${pageBean.currentPage-1 }"
aria-label="Previous"><span aria-hidden="true">«</span></a></li>
</c:if>
<!-- 判断当前页 -->
<c:forEach begin="1" end="${pageBean.totalPage }" var="page">
<c:if test="${pageBean.currentPage==page }">
<li class="active"><a href="javaScript:void(0);">${page }</a></li>
</c:if>
<c:if test="${pageBean.currentPage!=page }">
<li><a
href="${pageContext.request.contextPath }/productList?page=${page }">${page }</a></li>
</c:if>
</c:forEach>
<!-- 判断向后 -->
<c:if test="${pageBean.currentPage==pageBean.totalPage }">
<li class="disabled"><a href="javaScript:void(0);"
aria-label="Next"> <span aria-hidden="true">»</span>
</a></li>
</c:if>
<c:if test="${pageBean.currentPage!=pageBean.totalPage }">
<li><a
href="${pageContext.request.contextPath }/productList?page=${pageBean.currentPage+1 }"
aria-label="Next"> <span aria-hidden="true">»</span>
</a></li>
</c:if>
Paste_Image.png