Paging implementation:
The following key steps are required to achieve paginated display of data:
① determine the total number of pages displayed per page
② calculate the total number of pages displayed
③ Writing SQL statements
Implementation results:
When you click on the next page, the Address bar address is? pageindex=2
1. Create Util package, page class, define related attributes and encapsulate:
PackageCn.news.util;Importjava.util.List;ImportCn.news.entity.NewsDetail; Public classPage {//Current Page Private intPageIndex; //Page Record Count Private intpageSize; //Dramatic show real Data PrivateList<newsdetail>list; //Total Pages Private intTotalPages; //Total Record Count Private inttotalrecords; Public intGetpageindex () {returnPageIndex; } Public voidSetpageindex (intPageIndex) { This. PageIndex =PageIndex; } Public intgetpagesize () {returnpageSize; } Public voidSetPageSize (intpageSize) { This. pageSize =pageSize; } PublicList<newsdetail>getList () {returnlist; } Public voidSetlist (list<newsdetail>list) { This. List =list; } Public intgettotalpages () {returnTotalPages; } Public voidSettotalpages (inttotalpages) { This. TotalPages =TotalPages; } Public intgettotalrecords () {returntotalrecords; } Public voidSettotalrecords (inttotalrecords) { This. totalrecords =totalrecords; }}
2, in the DAO implementation class Newsdetaildaoimpl definition obtains the news current page data method Getonepage, and implants two parameters pageindex, pageSize
@Override PublicList<newsdetail> Getonepage (intPageIndex,intpageSize)throwsException {List<NewsDetail> list=NewArraylist<newsdetail>(); String SQL= "SELECT Top" +pagesize+ "* from Newsdetails where newsId not in (select Top" + (pageIndex-1) *pagesize+ "NewsId from Newsdet ails) "; ResultSet RS=executeQuery (SQL); if(rs!=NULL) { while(Rs.next ()) {Newsdetail News=NewNewsdetail (); News.setnewsid (Rs.getint ("NewsId")); News.setnewstitle (Rs.getstring ("Newstitle")); News.setnewscontent (Rs.getstring ("Newscontent")); News.setnewscreatedate (Rs.getdate ("Newscreatedate")); News.setnewsauthor (Rs.getstring ("Newsauthor")); News.setnewscategoryid (Rs.getint ("Newscategoryid")); List.add (news); } } returnlist; }
3, in the Newsservlet class to pass data to the JSP page
PackageCn.news.servlet;Importjava.io.IOException;Importjava.util.List;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportCn.news.entity.NewsDetail;ImportCn.news.impl.NewsDetailDaoImpl;ImportCn.news.util.Page; Public classNewsservletextendsHttpServlet {/** */ Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {doPost (request,response); } /** */ Public voidDoPost (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {//Instantiate DAONewsdetaildaoimpl dao=NewNewsdetaildaoimpl (); Try{Page page=NewPage (); //Default 3 Data intPagesize=3; Page.setpagesize (pageSize); //Current Page intMyindex=1; String PageIndex=request.getparameter ("PageIndex"); if(pageindex!=NULL&& ("!pageindex.equals")) {Myindex=Integer.parseint (PageIndex); }Else{Myindex=1; } //Current Page AssignmentPage.setpageindex (Myindex); intMytotalpages=0; inttotalpages=Dao.getallcount (); if(totalpages%pagesize==0) {mytotalpages=totalpages/pageSize; } Else{mytotalpages=totalpages/pagesize+1; } page.settotalpages (Mytotalpages); //Generic DataList<newsdetail> list=Dao.getonepage (Page.getpageindex (), pageSize); Page.setlist (list); Request.setattribute ("List", page); Request.getrequestdispatcher ("/index.jsp"). Forward (request, response); //list<newsdetail> list=dao.getallnews (); } Catch(Exception e) {e.printstacktrace (); } }}
4. Get the generic collection on index.jsp page and get the current page and next page
<% page Page2= (page) request.getattribute ("list"); for (Newsdetail item:page2.getList ()) { %> <li><a href= ' newspages/news_read.jsp ' ><%=item.getnewstitle ()%></a> <span><%=item.getnewscreatedate ()%></span></li> <% } % > <p align= "right" > Current page: [<%=page2.getpageindex ()%>/<%=page2.gettotalpages ()%>]& nbsp <a href= "<%=path%>/servlet/newsservlet?pageindex=<%=page2.getpageindex () +1%>" > Next </a> <a href= "#" > End </a> </p> </ul>
Press release System < pagination >