list轉page對象實現分頁查詢

首先需要寫一個PageBean對象,下面代碼可直接複製。

PageBean.java

package com.hedong.page;

import java.util.List;
/**
 * 分頁實體類
 * @author Peng
 * @Date2020年2月3日 上午9:40:10
 */
public class PageBean<T> {
 
	private int currPage;//當前頁數
    private int pageSize;//每頁顯示的記錄數
    private int totalCount;//總記錄數
    private int totalPage;//總頁數
    private List<T> lists;//每頁的顯示的數據
	
	public PageBean() {
		super();
	}
 
	public int getCurrPage() {
		return currPage;
	}
 
	public void setCurrPage(int currPage) {
		this.currPage = currPage;
	}
 
	public int getPageSize() {
		return pageSize;
	}
 
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
 
	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<T> getLists() {
		return lists;
	}
 
	public void setLists(List<T> lists) {
		this.lists = lists;
	}
	
}

下面是一個將list轉爲pagebean對象的方法,參數爲當前頁數:currentPage,每頁數據數目:pageSize,需要分頁的list:list。

		//將list轉page
		public static <T> PageBean<T> listTopage(int currentPage,int pageSize,List<T> list){
				//頁面對象
				PageBean<T> pageBean = new PageBean<T>();
				
				//封裝當前頁數
				pageBean.setCurrPage(currentPage);
				
				//每頁顯示的數據數目
				pageBean.setPageSize(pageSize);
				
				//封裝總記錄數
				int totalCount = list.size();
				pageBean.setTotalCount(totalCount);
				
				//封裝總頁數
				double tc = totalCount;
				Double num=Math.ceil(tc/pageSize);//向上取整
				pageBean.setTotalPage(num.intValue());
				
				//封裝該頁list
				int start=(currentPage-1)*pageSize;
				if (start + pageSize > totalCount)
					pageBean.setLists(list.subList(start, totalCount));
				else
					pageBean.setLists(list.subList(start, start + pageSize));
				return pageBean;
		}

然後就可以在條件查詢需要的list之後調用該方法獲得一個PageBean對象,該對象中就包含了這一頁的所有信息,可傳到前端進行展示

後端:

		//留言翻頁
		@RequestMapping("/turnPage")
		@ResponseBody
		public String toturnPage(HttpServletRequest request,HttpServletResponse response,Model model) {
	
			String successCode="1";
			int currentPage=Integer.parseInt(request.getParameter("currentPage"));
			int goodsid=Integer.parseInt(request.getParameter("goodsid"));
			
			//獲取評論
			CommentExample commentExample=new CommentExample();
			com.hedong.pojo.CommentExample.Criteria criteria=commentExample.createCriteria();
			criteria.andGoodsidEqualTo(goodsid);
			List<Comment> comments=commentService.selectComment(commentExample);
			
			//將評論分頁
			PageBean<Comment> pagemsg=OperateController.listTopage(currentPage,1,comments);
			request.getSession().setAttribute("pagemsg", pagemsg);
			return successCode;
		}

前端:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@page import="com.hedong.pojo.User"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>商品詳情</title>
</head>
	<body style="background-color: white;">
		<div style="text-align: center;">
			<table style="margin-top: 20px;border-collapse: collapse;" >
			    <c:forEach var="n" items="${pagemsg.lists}">
			        <tr style="text-align: center;  border: 1px solid #e5e5e5;">
			        	<td width="10%"><img src="${n.getHeadpath() }" height="50" width="50" style="margin-top: 10px;margin-bottom: 10px;"/></td>
			        	<td width="70%">${n.getContent() }</td>
			            <td width="10%">${n.getUsername() }</td>
			            <td width="10%">${n.getCommenttime() }</td>
			        </tr>
		        </c:forEach>
		    </table>
		    
		    
		    <table  border="0" cellspacing="0" cellpadding="0"  width="900px">
				<tr>		 
					<td class="td2">
					   <span>第${pagemsg.currPage }/ ${pagemsg.totalPage}頁</span>  
					   <span>總記錄數:${pagemsg.totalCount }  每頁顯示:${pagemsg.pageSize}</span>  
					   <span>
					       <c:if test="${pagemsg.currPage != 1}">
					           <a οnclick="turnPage(1,'${specificGoods.getGoodsid() }')">[首頁]</a>  
					           <a οnclick="turnPage('${pagemsg.currPage-1}','${specificGoods.getGoodsid() }')" >[上一頁]</a>   
					       </c:if>
					       
					       <c:if test="${pagemsg.currPage != pagemsg.totalPage}">
					           <a οnclick="turnPage('${pagemsg.currPage+1}','${specificGoods.getGoodsid() }')" >[下一頁]</a>  
					           <a οnclick="turnPage('${pagemsg.totalPage}','${specificGoods.getGoodsid() }')">[尾頁]</a>  
					       </c:if>
					   </span>
					</td>
				</tr>
			</table>

		</div>
	</body>
</html>

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章