新闻列表的分页查询java代码实现
本文实例为大家分享了新闻列表分页查询的java代码,供大家参考,具体内容如下
package com.ibeifeng.test; //创建新闻测试类 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; } @Override public String toString() { return "newTest [id=" + id + ", title=" + title + ", content=" + content + ", author=" + author + "]"; } } 2.开始查询 <%@ 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);//设定新闻行数为107行 for (int i = 1; i <= 107; i++) {//list中添加新闻 newTest news = new newTest(0L + i, i + "里约奥运", "马龙获得金牌-世界乒坛第五位男子“大满贯”得主", "福音"); list.add(news); }//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 newPage = Integer.valueOf(str); if (newPage < 1) { ipage = 1; } else if (newPage > iTitlePages) { ipage = iTitlePages; } else { ipage = newPage; } } //创建一个新的集合(大小每个页面显示的新闻总数) 将107条数据分别存储到其中 List<newTest> listPage = new ArrayList<newTest>(10); int ipa = 10;//获取循环体的循环次数//最后一页只有七条数据 if (ipage == iTitlePages) { //当当前页数为最后一页时,剩余几行则循环体之执行剩余的行的数次, ipa = list.size() - (iTitlePages - 1) * 10; } for (int i = 0; i < ipa; i++) { //i=0;获取前十个数据 第一次循环时ipage=4 newTest arr = list.get(i + (ipage - 1) * 10); listPage.add(arr); } %> <html> <body> <table> <tr> <th>标题</th> <td>作者</td> <td>摘要</td> </tr> <% for (int i = 0; i < listPage.size(); i++) { //java代码需要用<% %》保护起来否则会被当做web语句执行 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 = ipage == 1; boolean bLast = ipage == iTitlePages ; %> <% if (!bFirst) { %> <a href="test.jsp?page=<%=ipage - 1%>&totopage=11">上一页</a> <% } %> <!-- 当跳转到第一页时不再显示“上一页”提交对话框,下同 --> <% if (!bLast) { %> <a href="test.jsp?page=<%=ipage + 1%>&totopage=11">下一页</a> <% } %>第<%=ipage%>页 共<%=iTitlePages%>页 </body> </html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持我们。
赞 (0)