1.定义PageBean
package com.shenqz.util; import java.io.Serializable; import java.util.ArrayList; import java.util.List; import org.apache.poi.ss.formula.functions.T; /** * * @author Administrator * * @param <T> */ @SuppressWarnings("hiding") public class PageBean<T> implements Serializable { private static final long serialVersionUID = -6710532081301192385L; /** * 基本属性分析: * 1.当前页 currentPage * 2.每页多少数据 pageSize * 3.数据总条数 totalCount * 4.总页数 pageCount * 5.数据集合 List<T> list * 6.每页显示的最多的页码数 pageNumSize * 7.拼接的分页字符串 pageHtml */ private int currentPage = 1; private int pageSize = 2; private int totalCount; private int pageCount; private List<T> list; private int pageNumSize = 5; private String pageHtml; /** * 基本逻辑分析: * 1.list总数判断得出totalCount和pageCount * 2.currentPage*pageSize得出startRow和endRow * 3.通过list.submit(startRow,endRow)得出需要展示的当前页的数据; * 4.通过currentPage、pageSize、totalCount、pageCount拼出分页html字符串前台 * 需要拼接的项目有首页、上一页、下一页、末页以及中间具体页码 * 5.前台点击上述项目时,ajax传递currentPage和pageSize及查询条件至后台 * 6.根据查询条件查询出list,和currentPage和PageSize传入PageBean对象生成需要展示的list以及分页html */ /** * <p>Description: pageBean初始化方法</p> * @param list 数据集合 currentPage 页面传递的当前页码 pageSize 页面传递的pageSize */ public void initPage(List<T> list, Integer currentPage){ //1.初始化各属性的值 if(currentPage != null && currentPage >= 1){ this.currentPage = currentPage; } this.list = list; this.totalCount = this.list.size(); this.pageCount = totalCount%this.pageSize == 0?totalCount/this.pageSize:totalCount/this.pageSize+1; int startRow = (this.currentPage-1)*this.pageSize; int endRow = this.currentPage*this.pageSize; if(endRow > list.size()){ endRow = list.size(); } //2.拼接分页html this.pageHtml = initPageHtml(); //3.截取list if(!this.list.isEmpty()){ this.list = this.list.subList(startRow, endRow); } } /** * 拼接分页html */ public String initPageHtml(){ /** * 基本逻辑分析: * 1.判断list是否为空 * 2.根据currentPage判断是否有首页、上一页、下一页、末页 */ StringBuffer sb = new StringBuffer(); if(!this.list.isEmpty()){ /** * 样式及hover */ String background = "background: #2db3c1;border: 1px solid #2db3c1;color: #fff;"; String style = "text-decoration:none;padding: 8px 10px;margin-left: 3px;border: 1px solid #cdcccc;color: #2db3c1;cursor: pointer;max- 50px;"; String hover = "onmouseover="$(this).attr('style','"+style+background+"');" onmouseout="$(this).attr('style','"+style+"');""; //页面拼接开始 String firstPage = "<a href='javascript:skipToPage(1);' style=""+style+"""+hover+">首页</a>"; sb.append(firstPage); String prePage = ""; if(this.currentPage != 1){ prePage = "<a href='javascript:skipToPage("+(this.currentPage-1)+");' style=""+style+"""+hover+">上一页</a>"; }else{ prePage = "<a href='javascript:skipToPage("+(this.currentPage-1)+");' style=""+style+"display:none;""+hover+">上一页</a>"; } sb.append(prePage); //当前页小于每页页码数时,默认从第一个开始 int num = 1; //当前页大于每页页码数时 if(this.currentPage >= pageNumSize){ if((this.currentPage+(pageNumSize-1)/2) >= this.pageCount ){ num = this.currentPage-(this.pageNumSize-(this.pageCount+1-this.currentPage)); }else{ num = this.currentPage - (pageNumSize-1)/2; } } int endNum = (num+pageNumSize-1) > pageCount ?pageCount:(num+pageNumSize-1); for(int i = num; i <= endNum; i++){ String select = ""; String pageNumHtml = " <a href='javascript:skipToPage("+i+");' style=""+style+"""+hover+">"+i+"</a>"; if(this.currentPage == i){ select = background; pageNumHtml = " <a href='javascript:skipToPage("+i+");' style=""+style+select+"">"+i+"</a>"; } sb.append(pageNumHtml); } String nextPage = ""; if(this.currentPage != this.pageCount){ nextPage = "<a href='javascript:skipToPage("+(this.currentPage+1)+");' style=""+style+"""+hover+">下一页</a>"; }else{ nextPage = "<a href='javascript:skipToPage("+(this.currentPage+1)+");' style=""+style+"display:none;""+hover+">下一页</a>"; } sb.append(nextPage); String endPage = "<a href='javascript:skipToPage("+this.pageCount+");' style=""+style+"""+hover+">末页</a>"; sb.append(endPage); sb.append("第<input size=3 maxlength=6 id='currentPage' value=" + this.currentPage + ">页 " + "<a href=javascript:ToPage(document.getElementById('currentPage').value)>Go</a>"); sb.append(" 共" + this.pageCount + "页 "); sb.append("每页<input size=3 maxlength=6 id='pageSize' value=" + this.pageSize + ">条 "); String counyStr = "<span>共"+totalCount+"条数据</span>"; sb.append(counyStr); } return sb.toString(); } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { if(currentPage >= 1){ this.currentPage = currentPage; } } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { if(pageSize >= 1){ this.pageSize = pageSize; } } public int getTotalCount() { return totalCount; } public int getPageCount() { return pageCount; } public int getPageNumSize() { return pageNumSize; } public void setPageNumSize(int pageNumSize) { if(pageNumSize >= 1){ this.pageNumSize = pageNumSize; } } public List<T> getList() { return list; } public String getPageHtml() { return pageHtml; } }
2.controller
package com.shenqz.controller; import java.util.ArrayList; import java.util.List; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.shenqz.entity.User; import com.shenqz.util.PageBean; @Controller public class PageController { private PageBean<User> page; public PageController(){ page = new PageBean<User>(); } @RequestMapping("page.do") @ResponseBody public String pageHtml(Integer cur,Integer pageSize){ List<User> list = new ArrayList<User>(); list.add(new User("shenqz", 27, "15357000000")); list.add(new User("shenqz1", 27, "15357000000")); list.add(new User("shenqz2", 27, "15357000000")); list.add(new User("shenqz3", 27, "15357000000")); list.add(new User("shenqz4", 27, "15357000000")); list.add(new User("shenqz5", 27, "15357000000")); list.add(new User("shenqz6", 27, "15357000000")); list.add(new User("shenqz7", 27, "15357000000")); list.add(new User("shenqz8", 27, "15357000000")); list.add(new User("shenqz9", 27, "15357000000")); list.add(new User("shenqz10", 27, "15357000000")); list.add(new User("shenqz11", 27, "15357000000")); list.add(new User("shenqz12", 27, "15357000000")); list.add(new User("shenqz13", 27, "15357000000")); list.add(new User("shenqz14", 27, "15357000000")); list.add(new User("shenqz15", 27, "15357000000")); list.add(new User("shenqz16", 27, "15357000000")); list.add(new User("shenqz17", 27, "15357000000")); list.add(new User("shenqz18", 27, "15357000000")); list.add(new User("shenqz19", 27, "15357000000")); list.add(new User("shenqz20", 27, "15357000000")); /*PageBean<User> page = new PageBean<User>(); */ if(cur != null){ page.setCurrentPage(cur); } if(pageSize != null){ if(pageSize != page.getPageSize()){ page.setPageSize(pageSize); page.setCurrentPage(1); } } System.out.println("pageSize:"+page.getPageSize()); page.initPage(list, page.getCurrentPage()); String str = page.initPageHtml(); JSONObject object = new JSONObject(); object.put("page", str); object.put("list", page.getList()); return object.toJSONString(); } public static void main(String[] args) { PageBean<User> page = new PageBean<User>(); System.out.println(page.getCurrentPage()); } }
3.jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>首页</title> <script type="text/javascript" src="js/jquery-1.8.3.js"></script> <script type="text/javascript"> function skipToPage(cur){ var pageSize = $("#pageSize").val(); queryList(cur,pageSize); } function ToPage(cur){ skipToPage(cur) } function queryList(cur,pageSize){ $.ajax({ url : "page.do", data : {"cur":cur,"pageSize":pageSize}, async : true, type : "post", dataType : "json", success : function(data){ var str = JSON.stringify(data.page); str = str.replace(/"/g, ""); var list ; $("#d2").text(""); $.each(data.list,function(k){ list+="<tr><td>"+data.list[k].username+"</td><td>"+data.list[k].age+"</td><td>"+data.list[k].iphone+"</td></tr>" }); $("#d2").append(list); $("#d2").append(str); }, error :function(){ } }) } </script> </head> <body> <a href = "javascript:void(0)" onclick = "queryList();">查询所有</a> <div id="d2"></div> </body> </html>
4.页面效果