求一个清晰点的action jsp分页显示的方法。。。。。。。。。

lqsmn613 2010-06-22 04:37:26
如题,忘记怎么写分页的了。大家来个好点的,下面是我action中获取数据的一个方法:

public ActionForward doView2(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DynaActionForm dyForm = (DynaActionForm) form;
com.ebuilds.util.common.LoginInfo logininfo = (com.ebuilds.util.common.LoginInfo) request.getSession().getAttribute("LoginInfo");
String username = logininfo.getName();
String uid = null;
List list = new ArrayList();
try {
DB db = new DB();
ResultSet rs = db.qu("select * from userinfo where username = '" +
username+"'");
while(rs.next()){
uid = rs.getString("id");
}
if(uid != null){
ResultSet rs1 = db.qu("select * from userinfo where pid = '" +
uid+"'");
while(rs1.next()){
Map map = new HashMap();
map.put("id", rs1.getString("id"));
map.put("pid", rs1.getString("pid"));
map.put("username", rs1.getString("username"));
map.put("name", rs1.getString("name"));
list.add(map);
}
}
db.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
dyForm.set("dtos", list);
return mapping.findForward("doView2");
}


根据上面action中取得的list集合的数据,在jsp中需要用分页来显示,每页14条。分页按钮中分别有:
第一页,上一页,下一页,最后页,转至 第__页 显示1到14条记录 第1页共2页

大家别藏私啊,来个好点的,最后带点代码说明,看的没这么累。多谢。
...全文
284 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
亻生箬祗初見 2010-06-23
  • 打赏
  • 举报
回复
我有个分页的代码,如果需要的话你说下我发给你317165913
duben4814 2010-06-23
  • 打赏
  • 举报
回复
用hibernate可以实现,
假如用javabean的话,我有一个


public class PageUtil {
public static final int SIZE20 = 20;

private int pageSize;//页面显示条数
private int recordCount;//记录总数
private int currentPage; //当前页
private int fromIndex;
private int toIndex;
private int pageCount;

private int currentPageSize;

public PageUtil(){
this(0,0,1);
}
public PageUtil(int pageSize, int recordCount, int currentPage) {
this.pageSize = pageSize;
this.recordCount = recordCount;
setCurrentPage(currentPage);

}
public PageUtil(int pageSize, int recordCount) {
this(pageSize, recordCount, 1);
}

public int getNextPage(){
return currentPage + 1 > getPageCount() ? getPageCount() : currentPage + 1;
}
public int getPrevPage(){
return currentPage - 1 < 1 ? 1 : currentPage - 1;
}
public int getCurrentPageSize() {
return getToIndex() - getFromIndex();
}


//总页数
public int getPageCount() {
if(pageSize==0)
return 1;
int size = recordCount/pageSize;
int mod = recordCount % pageSize;
if(mod != 0)
size++;
return recordCount == 0 ? 1 : size;
}

//包含,起始索引为0
public int getFromIndex() {
System.out.println("from index:"+(currentPage-1) * pageSize);
return (currentPage-1) * pageSize;
}

//不包含
public int getToIndex() {
System.out.println("to index:"+Math.min(recordCount, currentPage * pageSize));
return Math.min(recordCount, currentPage * pageSize);
}

public int getCurrentPage() {
return currentPage;
}
public void setCurrentPage(int currentPage) {
int validPage = currentPage <= 0 ? 1 : currentPage;
validPage = validPage > getPageCount() ? getPageCount() : validPage;
this.currentPage = validPage;
}
public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getRecordCount() {
return recordCount;
}
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}
}

daocha 2010-06-22
  • 打赏
  • 举报
回复
把hashmap 放session 裡面 每頁顯示指定數目的條數
到下一頁的話就往下面讀取 然後顯示
不過這樣的壞處就是不能及時銷毀session裡面的hashmap

要不就是用數據庫limit 分頁,可是每一頁都要讀數據庫

再就是直接讀出來 用JS來控制條數的DISPLAY的值 讓其顯示和隱藏

再就是用標籤庫了 用個分頁標籤吧
wzglovejava 2010-06-22
  • 打赏
  • 举报
回复
分页的方法千奇百态,多的都跟逛街的女人一样,你随便去搜一下,对比一下,哪个好用,就拿来用好了,还用发帖求助呀,俺是看超级女声挺过来的爷们儿,但还是佩服你了。
dongqdonglin 2010-06-22
  • 打赏
  • 举报
回复
数据库查询时候把分页数据做好 剩下的就是展示了
izard999 2010-06-22
  • 打赏
  • 举报
回复
在数据库做分页,页面上只管显示就好了撒
qiheia 2010-06-22
  • 打赏
  • 举报
回复
这里面有几个不错的分页方法,你可以参考下。。。
http://topic.csdn.net/u/20090715/16/08d47b5b-f229-4938-b914-6b2f94320a3e.html
xmy8882040810 2010-06-22
  • 打赏
  • 举报
回复
帮顶下,第一次做沙发~~~~

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧