新闻列表的分页查询java代码实现
家电维修 2023-07-16 19:17www.caominkang.com家电维修技术
本文实例为大家讲解了新闻列表分页查询的java代码,供大家参考,具体内容如下
package .ibeifeng.test; //创建新闻测试类 public class neTest { private long id; private String title; private String content; private String author; public neTest() { super(); } public neTest(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; } @Override public String toString() { return "neTest [id=" + id + ", title=" + title + ", content=" + content + ", author=" + author + "]"; } } 2.开始查询 <%@ page language="java" import="java.util." pageEncoding="utf-8"%> <%@ page import=".ibeifeng.test.neTest"%> <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/"; %> <% Listlist = ne ArrayList (107);//设定新闻行数为107行 for (int i = 1; i <= 107; i++) {//list中添加新闻 neTest nes = ne neTest(0L + i, i + "里约奥运", "马龙获得金牌-世界乒坛第五位男子“大满贯”得主", "福音"); list.add(nes); }//end of for...添加107条数据到集合中 //int pageIndex=10; int iTitleIndex = list.size();//获取集合下表标 int iTitlePages = iTitleIndex / 10 + (iTitleIndex % 10 == 0 ? 0 : 1);//获取页数的总数 int ipage = 4;//开始的页数 String str = request.getParameter("page"); if (str != null && !str.trim().equals("")) { int nePage = Integer.valueOf(str); if (nePage < 1) { ipage = 1; } else if (nePage > iTitlePages) { ipage = iTitlePages; } else { ipage = nePage; } } //创建一个新的集合(大小每个页面显示的新闻总数) 将107条数据分别存储到其中 List listPage = ne ArrayList (10); int ipa = 10;//获取循环体的循环次数//一页只有七条数据 if (ipage == iTitlePages) { //当当前页数为一页时,剩余几行则循环体之执行剩余的行的数次, ipa = list.size() - (iTitlePages - 1) 10; } for (int i = 0; i < ipa; i++) { //i=0;获取前十个数据 第一次循环时ipage=4 neTest arr = list.get(i + (ipage - 1) 10); listPage.add(arr); } %> 标题 作者 摘要 <% for (int i = 0; i < listPage.size(); i++) { //java代码需要用<% %》保护起来否则会被当做eb语句执行 neTest temp = listPage.get(i); %> <%=temp.getTitle()%> <%=temp.getAuthor()%> <%=temp.getContent()%> <% }//end of for... %> <% boolean bFirst = ipage == 1; boolean bLast = ipage == iTitlePages ; %> <% if (!bFirst) { %> &toage=11">上一页 <% } %> <% if (!bLast) { %> &toage=11">下一页 <% } %>第<%=ipage%>页 共<%=iTitlePages%>页