This article shares the Java code for news list pagination query for everyone for your reference. The specific content is as follows
package com.ibeifeng.test;//Create news test class public class newTest {private long id;private String title;private String content;private String author; public newTest() { super();} public newTest(long id, String title, String content, String author) { this.id = id; this.title = title; this.content = content; this.author = author;} public long getId() { return id;} public void setId(long id) { this.id = id;} public String getTitle() { return title;} public void setTitle(String title) { this.title = title;} public String getContent() { return content;} public void setContent(String content) { this.content = content;} public String getAuthor() { return author;} public void setAuthor(String author) { this.author = author;} @Overridepublic String toString() { return "newTest [id=" + id + ",, content=" + content + ", author=" + author + "]";} }2. Start query <%@ page language="java" import="java.util.*" pageEncoding="utf-8"%><%@ page import="com.ibeifeng.test.newTest"%><% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";%><% List<newTest> list = new ArrayList<newTest>(107);//Set the number of news lines to 107 for (int i = 1; i <= 107; i++) {//Add news in list newTest news = new newTest(0L + i, i + "Rio Olympics", "Maron won the gold medal - the fifth male "Grand Slam" winner in the world table tennis world", "gospel"); list.add(news); }//end of for...Add 107 pieces of data to the set //int pageIndex=10; int iTitleIndex = list.size();//Get the following table int iTitlePages = iTitleIndex / 10 + (iTitleIndex % 10 == 0 ? 0 : 1);//Get the total number of pages int page = 4;//The number of pages starting String str = request.getParameter("page"); if (str != null && !str.trim().equals("")) { int newPage = Integer.valueOf(str); if (newPage < 1) { page = 1; } else if (newPage > iTitlePages) { page = iTitlePages; } else { page = newPage; } } //Create a new set (size the total number of news displayed on each page) Store 107 pieces of data into List<newTest> listPage = new ArrayList<newTest>(10); int ipa = 10;//Get the number of loops in the loop body//The last page has only seven pieces of data if (page == iTitlePages) { //When the current number of pages is the last page, the remaining few lines will be executed several times in the loop body, ipa = list.size() - (iTitlePages - 1) * 10; } for (int i = 0; i < ipa; i++) { //i=0; Get the first ten data for the first loop online newTest arr = list.get(i + (page - 1) * 10); listPage.add(arr); }%><html><body> <table> <tr> <th>Title</th> <td>Author</td> <td>Abstract</td> </tr> <% for (int i = 0; i < listPage.size(); i++) { //java code needs to be protected with <% %》, otherwise it will be executed as a web statement newTest temp = listPage.get(i); %> <tr> <td><%=temp.getTitle()%></td> <td><%=temp.getAuthor()%></td> <td><%=temp.getContent()%></td> </tr> <% }//end of for... %> </table> <% boolean bFirst = page == 1; boolean bLast = page == iTitlePages ; %> <% if (!bFirst) { %> <a href="test.jsp?page=<%=page - 1%>&totopage=11">Previous page</a> <% } %> <!-- When jumping to the first page, the "Previous page" submission dialog box is no longer displayed, the following is the same--> <% if (!bLast) { %> <a href="test.jsp?page=<%=page + 1%>&totopage=11">Next page</a> <% } %><%=page%> Pages of <%=iTitlePages%> Pages</body></html>The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.