日期:2014-05-20  浏览次数:20691 次

在jsp页面如何对进行所查询出的结果再分页?
刚加载到页面是分页显示出数据,当点击查询按钮后,对所查询出来的结果也能进行分页显示!诸位大虾,能否指点下?感激不尽了。。。。。

------解决方案--------------------
1071782050
------解决方案--------------------
<%

int pagesize = 8; //每页显示条数
int recordCount = list.size();//记录总数
int pageCount = recordCount / pagesize;//总页数
if(recordCount % pagesize>0){
pageCount++;
}
String pagestr = request.getParameter("page");
int currentPage =1;//当前页数

if(pagestr !=null){
currentPage = Integer.parseInt(pagestr);
currentPage = (currentPage < 1) ? 1 : currentPage;
currentPage = (currentPage > pageCount) ? pageCount : currentPage;
}

int formIndex = (currentPage-1)*pagesize;//取结果集当中需要显示的开始索引
int toIndex = currentPage * pagesize;//取结果集当中需要显示的结束索引,不包括结束索引
toIndex = (toIndex > recordCount) ? recordCount : toIndex;

%>
 
<% 
for(int i=formIndex ; i<toIndex;i++){
User user2 = (User)list.get(i);
%>
<tr>
<td>
<div class="article">
<h3><a href="BlogReturn.action?id=<%=user2.getId()%>" ><%=user2.getTitle() %></a></h3>
<p class="author">xxxxxx @ <%=user2.getTimeda() %></p>
<p class="content">
<%=user2.getComment() %>
</p>
<p class="show">浏览[1051] | 评论[5]</p>
</div>
</td>
</tr>
<%

%>
<tr align="right">
<td>
记录总数 <%=recordCount %>条 每页显示 <%=pagesize %> 条 当前页/总页数 <%=currentPage %>/<%=pageCount %> 
<a href="Blog.action?page=1">首页</a>
<a href="Blog.action?page=<%=currentPage-1 %>">上页</a>
<a href="Blog.action?page=<%=currentPage+1 %>">下页</a>
<a href="Blog.action?page=<%=pageCount %>">末页</a>
</td>
</tr>

</table>
=========================================================================================

分页首先的见这么一个类
代码如:
package com.page.util;

public class PageUtil {

private int pagesize;//每页显示条数

private int recordcount;//记录总数

private int currenpage;//当前页

public PageUtil(int pagesize,int recordcount,int currenpage) {
this.pagesize = pagesize;
this.recordcount = recordcount;
setCurrenpage(currenpage);
}
public PageUtil(int pagesize,int recordcount) {
// this.pagesize = pagesize;
// this.recordcount = recordcount;
// setCurrenpage(1);
this(pagesize,recordcount,1);
}
public int getRecordcount() {
return recordcount;
}
public int getPagesize() {
return pagesize;
}
public int getCurrenpage() {
return currenpage;
}
/**

* 获取当前页的下一页
* @return
*/
public int getNextpage(){
int nextpage = currenpage + 1;
if(nextpage > getPageCount())
return getPageCount();
return nextpage;
}
/**

* 获取当前页的上一页
* @return
*/
public int getToppage(){
int toppage = currenpage - 1;
if(toppage < 1)
toppage = 1;
return toppage;

}
/**

* 设置当前页面
* @param currenpage
*/
public void setCurrenpage(int currenpage) {

currenpage = (currenpage < 1) ? 1 : currenpage;
currenpage = (currenpage > getPageCount()) ? getPageCount() : currenpage;