新闻列表的分页查询java代码实现
本文实例为大家分享了新闻列表分页查询的java代码,供大家参考,具体内容如下
packagecom.ibeifeng.test;
//创建新闻测试类
publicclassnewTest{
privatelongid;
privateStringtitle;
privateStringcontent;
privateStringauthor;
publicnewTest(){
super();
}
publicnewTest(longid,Stringtitle,Stringcontent,Stringauthor){
this.id=id;
this.title=title;
this.content=content;
this.author=author;
}
publiclonggetId(){
returnid;
}
publicvoidsetId(longid){
this.id=id;
}
publicStringgetTitle(){
returntitle;
}
publicvoidsetTitle(Stringtitle){
this.title=title;
}
publicStringgetContent(){
returncontent;
}
publicvoidsetContent(Stringcontent){
this.content=content;
}
publicStringgetAuthor(){
returnauthor;
}
publicvoidsetAuthor(Stringauthor){
this.author=author;
}
@Override
publicStringtoString(){
return"newTest[id="+id+",title="+title+",content="+content
+",author="+author+"]";
}
}
2.开始查询
<%@pagelanguage="java"import="java.util.*"pageEncoding="utf-8"%>
<%@pageimport="com.ibeifeng.test.newTest"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"
+request.getServerName()+":"+request.getServerPort()
+path+"/";
%>
<%
List<newTest>list=newArrayList<newTest>(107);//设定新闻行数为107行
for(inti=1;i<=107;i++){//list中添加新闻
newTestnews=newnewTest(0L+i,i+"里约奥运","马龙获得金牌-世界乒坛第五位男子“大满贯”得主","福音");
list.add(news);
}//endoffor...添加107条数据到集合中
//intpageIndex=10;
intiTitleIndex=list.size();//获取集合下表标
intiTitlePages=iTitleIndex/10
+(iTitleIndex%10==0?0:1);//获取页数的总数
intipage=4;//开始的页数
Stringstr=request.getParameter("page");
if(str!=null&&!str.trim().equals("")){
intnewPage=Integer.valueOf(str);
if(newPage<1){
ipage=1;
}elseif(newPage>iTitlePages){
ipage=iTitlePages;
}else{
ipage=newPage;
}
}
//创建一个新的集合(大小每个页面显示的新闻总数)将107条数据分别存储到其中
List<newTest>listPage=newArrayList<newTest>(10);
intipa=10;//获取循环体的循环次数//最后一页只有七条数据
if(ipage==iTitlePages){//当当前页数为最后一页时,剩余几行则循环体之执行剩余的行的数次,
ipa=list.size()-(iTitlePages-1)*10;
}
for(inti=0;i<ipa;i++){//i=0;获取前十个数据第一次循环时ipage=4
newTestarr=list.get(i+(ipage-1)*10);
listPage.add(arr);
}
%>
<html>
<body>
<table>
<tr>
<th>标题</th>
<td>作者</td>
<td>摘要</td>
</tr>
<%
for(inti=0;i<listPage.size();i++){//java代码需要用<%%》保护起来否则会被当做web语句执行
newTesttemp=listPage.get(i);
%>
<tr>
<td><%=temp.getTitle()%></td>
<td><%=temp.getAuthor()%></td>
<td><%=temp.getContent()%></td>
</tr>
<%
}//endoffor...
%>
</table>
<%
booleanbFirst=ipage==1;
booleanbLast=ipage==iTitlePages;
%>
<%
if(!bFirst){
%>
<ahref="test.jsp?page=<%=ipage-1%>&totopage=11">上一页</a>
<%
}
%>
<!--当跳转到第一页时不再显示“上一页”提交对话框,下同-->
<%
if(!bLast){
%>
<ahref="test.jsp?page=<%=ipage+1%>&totopage=11">下一页</a>
<%
}
%>第<%=ipage%>页 共<%=iTitlePages%>页
</body>
</html>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。
热门推荐
7 简短的二胎祝福语
10 简短霸气女儿生日 祝福语
11 祝福语高考小众诗句简短
12 元旦祝福语20秒简短
13 老师过年祝福语大全 简短
14 一生祝福语简短
15 产检祝福语简短霸气
16 朋友店开张祝福语简短
17 生日爱情祝福语大全简短
18 朋友女儿生日祝福语 简短