本文实例为大家分享了新闻列表分页查询的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 + "/"; %> <% Listlist = new ArrayList (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 &#63; 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 listPage = new ArrayList (10); int ipa = 10;//获取循环体的循环次数//最后一页只有七条数据 if (ipage == iTitlePages) { //当当前页数为最后一页时,剩余几行则循环体之执行剩余的行的数次, ipa = list.size() - (iTitlePages - 1) * 10; } for (int i = 0; i <% boolean bFirst = ipage == 1; boolean bLast = ipage == iTitlePages ; %> <% if (!bFirst) { %> 上一页 <% } %> <% if (!bLast) { %> 下一页 <% } %>第<%=ipage%>页 共<%=iTitlePages%>页
<% for (int i = 0; i 标题 作者 摘要 <% }//end of for... %> <%=temp.getTitle()%> <%=temp.getAuthor()%> <%=temp.getContent()%>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。