页面分页问题

来源:1-1 MySQL数据库简介及安装

qq_张黎明_2

2018-09-17 11:18:03

servlet

@Override
	protected void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		String pageStr = request.getParameter("page");
		int page = 1;
		if(null != pageStr && !"".equals(pageStr)){
			page = Integer.parseInt(pageStr);
		}
		
		List<Product> productList = LocalCache.getProducts();
		
		int totalProducts = productList.size();
		int totalPage = totalProducts % 12 > 0 ? totalProducts /12 + 1 : totalProducts /12;
		
		request.setAttribute("curPage", page);
		request.setAttribute("prePage", page > 1 ? page - 1 : 1);
		request.setAttribute("nextPage", totalPage > page ? page + 1 : totalPage);
		request.setAttribute("totalPage", totalPage);

		
		request.setAttribute("products", LocalCache.getProducts(page,12));
		request.getRequestDispatcher("/WEB-INF/views/biz/list.jsp").forward(request, response);
	}

数据传输层

public static List<Product> getProducts(int page,int size){

List<Product> products = new ArrayList<>(productMap.values());

int start = (page - 1) * size;

int end = products.size() >= page * size ? page * size : products.size();

return products.subList(start, end);

}


第一次流程下来能理解,page值为1,但是第二次翻页时再次请求servlet时,page值还是1,没有变换的?这是如何分页的?

写回答

2回答

好帮手慕阿莹

2018-09-17

String pageStr = request.getParameter("page");  

 int page = 1;

        if(null != pageStr && !"".equals(pageStr)){

            page = Integer.parseInt(pageStr);

        }

         

  首先第一次设置为p,如果 pageStr 不为空,并且不为“”字符串,就把page赋值为从前台页面传过来的pageStr啦。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

0

qq_张黎明_2

提问者

2018-09-17



                <form method="post" action="/CartDemo/product/list.do" style="display: inline">

                    <input type="hidden" name="page" value="1">

                    <input type="hidden" name="title" value="${title}">

                    <input type="submit" value="首页" class="btn">

                </form>

                <form method="post" action="/CartDemo/product/list.do" style="display: inline">

                    <input type="hidden" name="page" value="${prePage}">

                    <input type="hidden" name="title" value="${title}">

                    <input type="submit" value="上一页" class="btn">

                </form>

                <form method="post" action="/CartDemo/product/list.do" style="display: inline">

                    <input type="hidden" name="page" value="${nextPage}">

                    <input type="hidden" name="title" value="${title}">

                    <input type="submit" value="下一页" class="btn">

                </form>

                <form method="post" action="/CartDemo/product/list.do" style="display: inline">

                    <input type="hidden" name="page" value="${totalPage}">

                    <input type="hidden" name="title" value="${title}">

                    <input type="submit" value="尾页" class="btn">

                </form>

                第${curPage}页/共${totalPage}页


0

0 学习 · 4297 问题

查看课程