一.这个分页能用但是不优化,代码还有点乱,没有改进
1.自己制作还没改完
PageController
package gjl; import java.util.ArrayList; import java.util.List; public class PageController { private int curentPageIndex=1; private int countPerpage=5; private int pageCount; public int getPageCount() { return pageCount; } public void setPageCount(int pageCount) { this.pageCount = pageCount; } public int getCurentPageIndex() { return curentPageIndex; } public void setCurentPageIndex(int curentPageIndex) { this.curentPageIndex = curentPageIndex; } public int getCountPerpage() { return countPerpage; } public void setCountPerpage(int countPerpage) { this.countPerpage = countPerpage; } public List getsmalList(List bigList,int curentPageIndex,int pageTotal){ this.curentPageIndex=curentPageIndex; List smallList=new ArrayList(); for (int i = (this.curentPageIndex-1)*countPerpage; i < this.curentPageIndex*countPerpage&&i<pageTotal; i++) { smallList.add(bigList.get(i)); } return smallList; } }
2.TestServlet
package gjl; import java.io.IOException; import java.io.PrintWriter; import java.util.List; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import cn.itcast.bean.Person; import cn.itcast.service.PersonService; public class TestServlet extends HttpServlet { /** * Constructor of the object. */ public TestServlet() { super(); } /** * Destruction of the servlet. <br> */ public void destroy() { super.destroy(); // Just puts "destroy" string in log // Put your code here } /** * The doGet method of the servlet. <br> * * This method is called when a form has its tag value method equals to get. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * The doPost method of the servlet. <br> * * This method is called when a form has its tag value method equals to post. * * @param request the request send by the client to the server * @param response the response send by the server to the client * @throws ServletException if an error occurred * @throws IOException if an error occurred */ public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PersonService personService = null; try { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("beans.xml"); personService = (PersonService)applicationContext.getBean("personService"); } catch (RuntimeException e) { e.printStackTrace(); } /*List<Person> persons = personService.getPersons(); for(Person person : persons){ System.out.println(person.getName()); }*/ /*PageController pController=(PageController)request.getAttribute("pc"); if (pController==null) { pController=new PageController(); List<Person> persons = personService.getPersons(); pController.setBigList(persons); }*/ List<Person> persons = personService.getPersons(); PageController pController=new PageController(); int pageCount=persons.size(); int pageTotal=0; if (pageCount%5==0) { pageTotal=pageCount/5; } else { pageTotal=pageCount/5+1; } String pageNumberString=(String) request.getParameter("pageNumber"); System.out.println(pageNumberString+"1111111"); if (pageNumberString==null) { pageNumberString="1"; } int pageNumber=Integer.parseInt(pageNumberString); List list=pController.getsmalList(persons,pageNumber,pageCount); /*for (int i = 0; i < list.size(); i++) { System.out.println(list.get(i)); }*/ request.setAttribute("persons", list); request.setAttribute("currentPage", pageNumber); request.setAttribute("pageTotal", pageTotal); //System.out.println("11"); request.getRequestDispatcher("MyJsp.jsp") .forward(request, response); } /** * Initialization of the servlet. <br> * * @throws ServletException if an error occurs */ public void init() throws ServletException { // Put your code here } }
3.MyJsp.jsp
<%@ page language="java" import="java.util.*,cn.itcast.bean.*" pageEncoding="utf-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'MyJsp.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <!-- <link rel="stylesheet" type="text/css" href="styles.css" mce_href="styles.css"> --> </head> <body> <%List<Person> list=(List<Person>)request.getAttribute("persons"); %> <table> <%for(int i=0;i<list.size();i++){%> <tr> <td> <%=list.get(i).getName() %> </td> <%} %> </tr> </table> <a href="TestServlet?pageNumber=1" mce_href="TestServlet?pageNumber=1">首页</a> <%if((Integer)request.getAttribute("currentPage")!=1){ %> <a href="TestServlet?pageNumber=<%=(Integer)request.getAttribute(" mce_href="TestServlet?pageNumber=<%=(Integer)request.getAttribute("currentPage")-1%>">上一页</a> <%} %> <%if(request.getAttribute("currentPage")!=request.getAttribute("pageTotal") ){ %> <a href="TestServlet?pageNumber=<%=(Integer)request.getAttribute(" mce_href="TestServlet?pageNumber=<%=(Integer)request.getAttribute("currentPage")+1%>">下一页</a> <%} %> <a href="TestServlet?pageNumber=<%=request.getAttribute(" mce_href="TestServlet?pageNumber=<%=request.getAttribute("pageTotal") %>">尾页</a> </body> </html>