新闻列表的分页查询java代码实现
作者:梦牵绕了谁的灵魂 时间:2022-02-24 19:46:25
本文实例为大家分享了新闻列表分页查询的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>
标签:java,分页,查询
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
java反射应用详细介绍
2022-06-14 04:59:45
使用spring框架中的组件发送邮件功能说明
2022-12-29 03:53:55
![](https://img.aspxhome.com/file/2023/1/63341_0s.png)
java如何获得redis所有的key-value
2022-03-13 12:22:14
![](https://img.aspxhome.com/file/2023/3/60823_0s.jpg)
Java如何在沙箱环境中测试支付宝支付接口
2023-11-02 14:55:15
![](https://img.aspxhome.com/file/2023/5/58745_0s.png)
15个顶级Java多线程面试题(附答案)
2023-11-24 01:53:44
Android TextView中文字通过SpannableString设置属性用法示例
2023-07-26 07:11:51
![](https://img.aspxhome.com/file/2023/8/84748_0s.jpg)
SpringBoot 创建web项目并部署到外部Tomcat
2023-09-15 18:25:04
深入了解Java ServletContext
2023-11-08 22:36:27
![](https://img.aspxhome.com/file/2023/7/59387_0s.png)
Java中异常处理之try和catch代码块的使用
2021-11-21 13:30:23
Java 在PDF中添加骑缝章示例解析
2023-11-24 22:41:35
![](https://img.aspxhome.com/file/2023/5/59985_0s.png)
java操作Apache druid的实例代码
2023-12-24 02:10:58
IDEA+Maven搭建Spring环境的详细教程
2023-11-25 07:50:34
![](https://img.aspxhome.com/file/2023/0/59880_0s.png)
springboot使用自定义注解实现aop切面日志
2023-11-11 09:14:48
VS2019配置OpenCV时找不到Microsoft.Cpp.x64.user的解决方法
2023-07-01 01:57:36
![](https://img.aspxhome.com/file/2023/1/100301_0s.png)
详解JAVA动态代理
2023-11-24 22:52:04
java简单实现斗地主发牌功能
2023-06-18 16:22:44
![](https://img.aspxhome.com/file/2023/6/57506_0s.jpg)
java 数据结构单链表的实现
2022-07-24 09:45:33
spring cloud Ribbon用法及原理解析
2021-11-28 15:27:21
java jdbc连接和使用详细介绍
2023-01-13 18:12:52
springboot整合quartz项目使用案例
2023-02-13 19:57:12
![](https://img.aspxhome.com/file/2023/6/62446_0s.png)