学习总结之JavaWeb实现分页

分页实现

实现分页功能需要了解几个参数:

1,totalPage      总页数
2,count        一共有多少数据
3,pageSize     每页显示几条
4,currentPage   当前第几页
5,category      商品的种类(导航栏查询需要用到)
6,List<Book> book  商品集合
把这些参数封装到PageBean中
    (1)利用mysql的limit,进行物理分页
    (2)select * from 表名 limit m,n;
    (3)m默认是从0开始,代表是第1条记录
    (4)n是指从第m+1条开始,取n条。
       例如: select * from book limit 4,10;
       从第五条记录开始,取出来10条。

我们需要做的步骤如下:

1.知道一共多少条记录   

select count(*) from 表;
2.每一页显示多少条记录
这个是认为定义的,即pageSize的初始化值

3.一共有多少页
3.1 总页数=总条数%每页条数==0?总条数/每页条数:总条数/每页条数+1;
3.2 总页数=Math.ceil(总条数*1.0/每页条数);  (一般用这个)

4.当前页码
默认值为1  即currentPage初始化为1
当点击上一页,下一页,就是对页码进行加减操作

View层

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>


<title>bookStore列表</title>
<%--导入css --%>
<link rel="stylesheet" href="${pageContext.request.contextPath }/css/main.css" type="text/css" />
</head>

<body class="main">

	<jsp:include page="head.jsp" />
	<jsp:include page="menu_search.jsp" />

	<div id="divpagecontent">
		<table width="100%" border="0" cellspacing="0">
			<tr>

				<td>
					<div style="text-align:right; margin:5px 10px 5px 0px">
						<a href="index.jsp">首页</a>    >    ${pb.book[0].category }    >    图书列表
					</div>

					<table cellspacing="0" class="listcontent">
						<tr>
							<td>
								<h1>商品目录</h1>
								<hr />
								<h1>${pb.book[0].category }</h1>    共${pb.count }种商品
								<hr />
								<div style="margin-top:20px; margin-bottom:5px">
									<img src="${pageContext.request.contextPath }/images/productlist.gif" width="100%" height="38" />
								</div>

								<table cellspacing="0" class="booklist">
									<tr>
								<c:forEach items="${pb.book }" var="b">
										<td>

											<div class="divbookpic">
												<p>
													<a href="#"><img src="${pageContext.request.contextPath }/upload/${b.img_url}" width="115" height="129"
														border="0" /> </a>
												</p>
											</div>

											<div class="divlisttitle">
												<a href="${pageContext.request.contextPath }/servlet/findBookInfoServlet?id=${b.id}">书名:${b.name }<br />售价:${b.price } </a>
											</div>
										</td>
									</c:forEach>	
									</tr>
								</table>
							
								<div class="pagination">
									<ul>                                                                                         //如果已经是第一页了,页数值赋值为1,不再进行减操作
										<li class="disablepage"><a href="${pageContext.request.contextPath  }/servlet/pageServlet?currentPage=${pb.currentPage==1?1:pb.currentPage-1}&category=${pb.category}"><<上一页</a></li>
										
										<li>第${pb.currentPage }页/共${pb.totalPage }页</li>
																																 //如果已经是最后一页了,页数值赋值为总页数,不再进行加操作
										<li class="nextPage"><a href="${pageContext.request.contextPath  }/servlet/pageServlet?currentPage=${pb.currentPage==pb.totalPage?pb.totalPage:pb.currentPage+1}&category=${pb.category}">下一页>></a></li>

									</ul>
								</div></td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</div>
	<jsp:include page="foot.jsp" />
</body>
</html>


Web层


public class PageServlet extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//初始化每页显示的记录条数
		int pageSize = 4;
		
		//初始化当前页数默认为第一页
		int currentPage = 1;
		
		String category = request.getParameter("category");
		if(category==null){
			category="";
		}
		//从页面的上一页或者下一页得到数据
		String currPage = request.getParameter("currentPage");
		
		//第一次访问资源时,currPage可能为null
		if(currPage!=null){
			currentPage = Integer.parseInt(currPage);
		} 
		
		BookServiceImpl bs = new BookServiceImpl();
		//分页查询,并返回PageBean对象
		
		PageBean pb = bs.findBooksPage(currentPage, pageSize,category);
	
		//分发转向
		request.setAttribute("pb", pb);
		request.getRequestDispatcher("/product_list.jsp").forward(request, response);

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

Service层

public class BookServiceImpl {
	public PageBean findBooksPage(int currentPage, int pageSize,String category) {
			
			try {
				int count  = bookDao.count(category);//得到总记录数
				int totalPage = (int)Math.ceil(count*1.0/pageSize); //求出总页数
				List<Book> books= bookDao.findBooks(currentPage,pageSize,category);
				
				//把5个变量封装到PageBean中,做为返回值
				PageBean pb = new PageBean();
				pb.setBook(books);
				pb.setCount(count);
				pb.setCurrentPage(currentPage);
				pb.setPageSize(pageSize);
				pb.setTotalPage(totalPage);
				pb.setCategory(category);
				return pb;
			} catch (SQLException e) {
				e.printStackTrace();
			}
			
			return null;
		}
}

Dao层
public class BookDaoImpl {
	public List<Book> findBooks(int currentPage, int pageSize, String category) throws SQLException {
		QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
		String sql = "select * from book where 1=1";
		List list = new ArrayList();
		if(!"".equals(category)){
			sql+=" and category=?";
			list.add(category);
		}
		sql+=" limit ?,?";
		list.add((currentPage-1)*pageSize);
		list.add(pageSize); 
		return qr.query(sql, new BeanListHandler<Book>(Book.class),list.toArray());
	}
}













































猜你喜欢

转载自blog.csdn.net/poison6/article/details/78001831