前言:分页有两种,真分页和假分页,两者区别点击这里,我也有好多代码都是看他的。话不多说,直接上代码。
一.首先创建一个页面信息实体类。
package com; import java.util.List; public class PageBean { private int page;//当前页 private int pagesize;//页面数据条数 private int indexpage=1;//首页 private int endpage;//尾页 private int count;//总数据条数 private int pagenumber;//总页面数 private List<Object> list;//得到的数据放入list集合中 public int getPage() { return page; } public void setPage(int page) { this.page = page; } public int getPagesize() { return pagesize; } public void setPagesize(int pagesize) { this.pagesize = pagesize; } public int getIndexpage() { return indexpage; } public void setIndexpage(int indexpage) { this.indexpage = indexpage; } public int getEndpage() { return endpage; } public void setEndpage() { this.endpage=pagenumber; } public int getCount() { this.count=list.size(); return count; } public void setCount() { this.count=list.size(); } public int getPagenumber() { return pagenumber; } public void setPagenumber() { this.pagenumber=(count%pagesize==0)?count/pagesize:count/pagesize+1; } public List<Object> getList() { return list; } public void setList(List<Object> list) { this.list = list; } @Override public String toString() { return "PageBean [page=" + page + ", pagesize=" + pagesize + ", indexpage=" + indexpage + ", endpage=" + endpage + ", count=" + count + ", pagenumber=" + pagenumber + ", list=" + list + "]"; } }
2.接着创建一个接收前端页面传过来的值和返回值给前端的servlet
package com; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; @SuppressWarnings("serial") public class FirstServlet extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub System.out.println("-----------------------------进入-------------------------"); //定义一个对象集合,存储数据,并且创建了53条数据 List<Object> list1=new ArrayList<Object>(); for(int i=0;i<53;i++){ User user=new User(); user.setAge(11); user.setName("bobo"+i); list1.add(user); } //1.接受前端的要跳转到的目标页的页面值 String str_page= req.getParameter("page"); //2.创建pageBean实例 PageBean pagebean=new PageBean(); pagebean.setList(list1); //将53条数据的集合放到页面中 pagebean.setCount(); //数据条数 pagebean.setPagesize(5); //每页大小 pagebean.setPagenumber();//总页面数 pagebean.setEndpage(); //伟业 pagebean.setIndexpage(1); //首页 //3.定义一个集合,该集合存放目标页要显示的5条数据,最后一页不一定是5条 List<Object> currentData=new ArrayList<Object>(); //4.1.判断前端要跳转到的目标页的页面值,我这里初始化页面是从servlet进入,所以首次是一个空值。2.如果已经是首页,但用户的错误操作点击上一页 //那目标也应该还是首页, if(str_page==null||Integer.parseInt(str_page)<1) { pagebean.setPage(1); } //如果为尾页,还点击下一页,那目标也还是在尾页 else if(Integer.parseInt(str_page)>pagebean.getPagenumber()){ pagebean.setPage(pagebean.getPagenumber()); } //其他正常情况 else{ pagebean.setPage(Integer.parseInt(str_page)); } //确定目标页显示的数据列表 for(int index=(pagebean.getPage()-1)*pagebean.getPagesize();index<pagebean.getPage()*pagebean.getPagesize()&&index<pagebean.getCount();index++){ User user=new User(); currentData.add(list1.get(index)); } // System.out.println(pagebean.toString()); HttpSession session=req.getSession(); // session.setAttribute("data", data); // System.out.println(currentData.toString()); //session.setAttribute("currentPage", currentPage); //传递当前页应取得的数据,和页面信息 session.setAttribute("pageInfo", pagebean); session.setAttribute("userList", currentData); req.getRequestDispatcher("first.jsp").forward(req, resp); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { // TODO Auto-generated method stub doGet(req,resp); } }
3.最后,写jsp代码
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" import="java.util.*" import="com.User" import="com.PageBean"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <% List<User> userList=(List<User>)session.getAttribute("userList"); request.setAttribute("userList",userList); PageBean pageBean=(PageBean)session.getAttribute("pageInfo"); request.setAttribute("pageBean",pageBean); %> <!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>Insert title here</title> <script type=“text/javascript" src="jquery-1.4.3.min.js"></script> <script type="text/javascript" src="first.js"></script> </head> <body> <div id="content_right"> <br /> <br /> <div class="btwz"> 人员列表 </div> <br> <form name="form1" action="Staff_InfoServlet" method="post"> <table cellspacing="1" class="table"> <tr class="table_header"> <td valign="middle" align="left"> 姓名 </td> <td valign="middle" align="left"> 年龄 </td> </tr> <c:forEach items="${userList}" var="list"> <tr class="row2" onMouseOver="this.className='row1'" onMouseOut="this.className='row2'"> <td valign="middle" align="left"> ${list.name} </td> <td valign="middle" align="left"> ${list.age} </td> </tr> </c:forEach> </table> </form> <p class="paging"> <a href="/Test/First.action?page=${pageBean.indexpage}">首页</a> <a href="/Test/First.action?page=${pageBean.page-1}"> < 上一页 </a> <strong>第${pageBean.page}页/共${pageBean.pagenumber}页</strong> <a href="/Test/First.action?page=${pageBean.page+1}">下一页 ></a> </p> <br /> <br /> </div> </div> </div> </body> </html>
注释已经很详细了,基本上很明了。我也不做过多的赘述了。以后有发现再来。