java web的簡單分頁

1、先新建一個與分頁信息有關的類

package model;

public class PageBean {

	private int currentPage;//當前頁數
	private int pageSize;//每頁顯示記錄數
	private int start;//每頁開始的記錄數
	private int totalPage;//總頁數
	public int getCurrentPage() {
		return currentPage;
	}
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}
	public PageBean(int currentPage, int pageSize) {
		super();
		this.currentPage = currentPage;
		this.pageSize = pageSize;
	}
	public int getPageSize() {
		return pageSize;
	}
	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}
	public int getStart() {
		return (currentPage-1)*pageSize;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getTotalPage() {
		return totalPage;
	}
	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}
	
	
}

(其中的每頁顯示的第一條數據序後start=(currentPage-1)*pageSize)


2、對數據庫進行操作,獨處總的數據數目

/**
	 * 查詢總的記錄數即得到totalPage
	 */
	public int getTotalPage(Connection con)throws Exception{
		String sql="select count(*) as total from t_contentlist t1,t_user t2 where t1.userID=t2.userId ";
		PreparedStatement preparedStatement=con.prepareStatement(sql);
		ResultSet rs=preparedStatement.executeQuery();
		if(rs.next()){
			return rs.getInt("total");
		}else{
		return 0;
	}
	}

3、對servlet進行操作

先構建一個構造方法

private String genPagation(int totalNum,int currentPage,int pageSize){
		int totalPage=totalNum%pageSize==0?totalNum/pageSize:totalNum/pageSize+1;
		StringBuffer pageCode=new StringBuffer();
		pageCode.append("<li><a href='main?page=1'>首頁</a></li>");
		if(currentPage==1){
			pageCode.append("<li class='disabled'><a href='#'>上一頁</a></li>");
		}else{
			pageCode.append("<li><a href='main?page="+(currentPage-1)+"'>上一頁</a></li>");
		}
		for(int i=currentPage-2;i<=currentPage+2;i++){
			if(i<1||i>totalPage){
				continue;
			}
			if(i==currentPage){
				pageCode.append("<li class='active'><a href='#'>"+i+"</a></li>");
			}else{
				pageCode.append("<li><a href='main?page="+i+"'>"+i+"</a></li>");
			}
		}
		if(currentPage==totalPage){
			pageCode.append("<li class='disabled'><a href='#'>下一頁</a></li>");
		}else{
			pageCode.append("<li><a href='main?page="+(currentPage+1)+"'>下一頁</a></li>");
		}
		pageCode.append("<li><a href='main?page="+totalPage+"'>尾頁</a></li>");
		return pageCode.toString();
	}


然後對操作數據庫

@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		String  page=req.getParameter("page");
		String 	pageSize="11";
		if (StringUtil.isEmpty(page)) {
			page="1";
		}
		PageBean pageBean=new PageBean(Integer.parseInt(page), Integer.parseInt(pageSize));
		Connection con=null;
		try {
			con=dbUtil.getCon();
			List<ContentList> contentLists=contentDao.conentList(con,pageBean);
			List<StoreInfo> storeInfos=contentDao.storeInfo(con);
			int totalNum=contentDao.getTotalPage(con);
			String pageCode=this.genPagation(totalNum, Integer.parseInt(page), Integer.parseInt(pageSize));
			req.setAttribute("contentList", contentLists);
			req.setAttribute("storeInfo", storeInfos);
			req.setAttribute("pageCode", pageCode);
			req.getRequestDispatcher("main.jsp").forward(req, resp);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			dbUtil.close(con);
		}
		
	}

(其中當前頁默認爲1,每頁顯示數爲11)
4、最後顯示界面

<div align="right" class="pagination pagination-lg">
				<ul>
					${pageCode }
				</ul>
				</div>
		</div>



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