jsp页面怎样实现分页?

weijingzhu1990 2010-08-15 10:58:55
JSP 在页面中如何实现分页 ? 用工厂方便 还是在页面中方便?
...全文
635 25 打赏 收藏 转发到动态 举报
写回复
用AI写文章
25 条回复
切换为时间正序
请发表友善的回复…
发表回复
外星人(ET) 2010-08-16
  • 打赏
  • 举报
回复
用jquery 的插件——jquery.jpage
「已注销」 2010-08-16
  • 打赏
  • 举报
回复
围观这么多种分页。

眼花缭乱。
liwis521125 2010-08-16
  • 打赏
  • 举报
回复
用个分页标签就好了~~自己写个,需要可以问我~!
Chariszou 2010-08-16
  • 打赏
  • 举报
回复
用page_taglib.jar,不过不太好用,我根据它的源代码进行了修改
还有ec组件也好用
ethan_z 2010-08-16
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 namoemitoufo 的回复:]

将页面要显示的数据存入一个list中,写到一个javabean中,在这个javabean中还应包含有总页数,当前页...等等字段,这样使用的时候将其放到request对象中,使用的时候直接通过el表达式显示到页面中,没必要每个页面都去写分页的代码。。。
[/Quote]

恩这样的分页很方便.
AsheBin 2010-08-16
  • 打赏
  • 举报
回复
我觉得用自定义的标签比较好,隐藏实现,扩展方便,不同页面配置不同的参数也可达到通用的目的
liuyuhua0066 2010-08-16
  • 打赏
  • 举报
回复
写个分页类,用hibernate分页,很方便
snowwhite1129 2010-08-16
  • 打赏
  • 举报
回复
那个简单用哪个了。
ChiChengIT 2010-08-16
  • 打赏
  • 举报
回复
可以用面向对象的思想来实现
把要分的页面看作一个对象

import java.util.List;

public class PageBean {
private int current = 1; // 当前页编号
private int pageSize = 5;// 每页显示几条记录
private int totalRecord;// 总共几条记录
private int totalPage;// 需要用几页显示
private boolean pre;// 是否有上一页
private boolean next;// 是否有下一页
private List list; // 当前页显示的记录集合
public PageBean() {
super();
// TODO Auto-generated constructor stub
}
public int getCurrent() {
if (current > totalPage) {
current = totalPage;
}
return current;
}

public void setCurrent(int current) {
this.current = current;
}

public int getPageSize() {
return pageSize;
}

public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

public int getTotalRecord() {
return totalRecord;
}

public void setTotalRecord(int totalRecord) {
this.totalRecord = totalRecord;
this.getTotalPage();// 获得总记录数的同时,完成总页数的初始化
}

// 获得总页数
public int getTotalPage() {
totalPage = (totalRecord + pageSize - 1) / pageSize;
return totalPage;
}

public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}

// 是否有上一页
public boolean isPre() {
pre = current > 1 ? true : false;
return pre;
}

public void setPre(boolean pre) {
this.pre = pre;
}

// 是否有下一页
public boolean isNext() {
next = current < totalPage ? true : false;
return next;
}

public void setNext(boolean next) {
this.next = next;
}

public List getList() {
return list;
}

public void setList(List list) {
this.list = list;
}

// 获得当前页第一条记录索引
public int getStart() {
return (getCurrent() - 1) * pageSize;
}

}
accessmanager88 2010-08-16
  • 打赏
  • 举报
回复
这是开源免费的中间件,使用MIT协议发布的。
accessmanager88 2010-08-16
  • 打赏
  • 举报
回复
应该使用真分页,假分页太浪费资源了。

我做的权限管理中间件,支持数据查询类权限。比如:查询订单、查询客户资料。我们的查询支持:
1,(真)分页;
2,自定义条件,比如50w以上的订单;
3,返回结果集,封装成你指定的类,比如订单你指定封装为:com.yourcompany.Bill
http://www.ralasafe.org/zh
UPC_思念 2010-08-15
  • 打赏
  • 举报
回复
将页面表现层的代码写到一个javabean中,这样使用的时候将其放到request对象中,使用的时候直接通过el表达式显示到页面中,没必要每个页面都去写分页的代码
枫欢 2010-08-15
  • 打赏
  • 举报
回复
每个数据库的分页方法都不一样,直接写网页上就行了

例如:sqlserver 的 要用子查询,
mysql 的 用关键字limit
UPC_思念 2010-08-15
  • 打赏
  • 举报
回复
http://blog.csdn.net/zxingchao2009/archive/2010/08/11/5805375.aspx
我博客上有,欢迎光临
justchenjie 2010-08-15
  • 打赏
  • 举报
回复
这个就是个算法,代码也不多,直接写在页面就行了
alexmitsui 2010-08-15
  • 打赏
  • 举报
回复
路过帮顶!:)
macower 2010-08-15
  • 打赏
  • 举报
回复
1:真分页
2:假分页
liping079094256 2010-08-15
  • 打赏
  • 举报
回复
package com.bbs.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import com.bbs.connect.bbs_connecting;
import com.bbs.object.bbs_page;

public class bbs_Pagedao {
private bbs_page page;

public bbs_page getPage() {
return page;
}

public void setPage(bbs_page page) {
this.page = page;
}
public ArrayList<bbs_page>select_allpages()
{
ArrayList<bbs_page>list=new ArrayList<bbs_page>();
bbs_connecting connecting=new bbs_connecting();
Connection conn=null;
Statement st=null;
ResultSet rs=null;
try {
conn=connecting.getConnection();
st=conn.createStatement();
String sql="select*from tb_bbspage";
rs=st.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while(rs.next())
{
bbs_page page=new bbs_page();
page.setClassid(rs.getInt("class"));
page.setContent(rs.getString("content"));
page.setId(rs.getInt("id"));
page.setSenderid(rs.getInt("senderid"));
page.setTime(rs.getTimestamp("sendtime").toString());
page.setTitle(rs.getString("title"));
list.add(page);
}

} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}
public void insertPage() {
Connection conn = null;
bbs_connecting connecting = new bbs_connecting();
try {
conn = connecting.getConnection();
Statement st = conn.createStatement();
String sql = "insert into tb_bbspage (senderid,title,content,sendtime,class)values("
+ page.getSenderid()
+ ",'"
+ page.getTitle()
+ "','"
+ page.getContent() + "',now()," + page.getClassid() + ")";
st.executeUpdate(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

public bbs_page selectPage(bbs_page page) {
Connection conn = null;
bbs_connecting connecting = new bbs_connecting();
try {
conn = connecting.getConnection();
Statement st = conn.createStatement();
String sql = "select *from bbs.tb_bbspage where id=" + page.getId();
ResultSet rs = st.executeQuery(sql);
rs.next();
page.setContent(rs.getString("content"));
page.setSenderid(rs.getInt("senderid"));
page.setTitle(rs.getString("title"));
page.setTime(rs.getTimestamp("sendtime").toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return page;
}

public ArrayList<bbs_page> user_selectpages(int userid) {
ArrayList<bbs_page> list = new ArrayList<bbs_page>();
Connection conn = null;
Statement st = null;
ResultSet rs = null;
bbs_connecting connecting = new bbs_connecting();
try {
conn = connecting.getConnection();
st = conn.createStatement();
String sql = "select*from tb_bbspage where senderid="+userid;
rs = st.executeQuery(sql);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
while (rs.next())

{
bbs_page newpage=new bbs_page();
newpage.setClassid(rs.getInt("class"));
newpage.setContent(rs.getString("content"));
newpage.setId(rs.getInt("id"));
newpage.setSenderid(rs.getInt("senderid"));
newpage.setTime(rs.getTimestamp("sendtime").toString());
newpage.setTitle(rs.getString("title"));
list.add(newpage);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
conn.close();
st.close();
rs.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return list;
}

public static void main(String args[]) {
bbs_Pagedao dao = new bbs_Pagedao();
bbs_page paging = new bbs_page();
paging.setTitle("dfdfdf");
paging.setContent("sfsfsdfsdfsdfsfs");
paging.setClassid(1);
paging.setSenderid(1);
dao.setPage(paging);
dao.insertPage();
}
}
以上是JavaBean
下面是页面
<%
bbs_Pagedao dao=new bbs_Pagedao();
bbs_page paging;
ArrayList<bbs_page> list=dao.select_allpages();
int perpagenumbers=10;
int curpage=1;

if(request.getParameter("curpage")!=null)
{
curpage=new Integer(request.getParameter("curpage"));
}
if(curpage<=0)
curpage=1;

int countpage=list.size()/perpagenumbers;
if(list!=null)
{
int i=(curpage-1)*perpagenumbers;
int j=i;
for(;i<list.size()&&(i-j)<10;i++)
{
paging=list.get(i);
%>
<tr><td><%out.println(i+1); %></td><td onmousedown=show_submenu(event,<%out.print(i%10+1); %>,1)><%=paging.getSenderid()%></td><td onmousedown=show_submenu(event,<%out.print(i%10+1); %>,2)><%=paging.getTitle() %></td><td onmousedown=show_submenu(event,<%out.print(i%10+1); %>,3)><%=paging.getClassid() %></td><td><%=paging.getTime() %></td></tr>

<%}} %>

</table>
<font size=2>第<%out.print(curpage); %>页       共<%out.println(countpage); %>页     <a href="bbs_showpages.jsp?curpage=<%out.println(curpage-1); %>">上一页</a>    <a href="bbs_showpages.jsp?curpage=<%out.println(curpage+1) ;%>">下一页</a></font>


liping079094256 2010-08-15
  • 打赏
  • 举报
回复
编写JavaBean查询数据库,存在list对象中。
jsp调用JavaBean实现分页,就可以了;
yinxiaoqi 2010-08-15
  • 打赏
  • 举报
回复
贴出个代码:

package sqlbean;

import java.sql.ResultSet;
import java.sql.SQLException;

import datebase.JDBC;

public class AttendstatusPage {
int pagenow;
int pagecount;
int pagesize;
int rowcount;

public AttendstatusPage() {
pagenow = 1;
pagecount = 0;
pagesize = 20;
rowcount = 0;
}

public int fpage(String spagenow) {
AttendstatusPage pb = new AttendstatusPage();
int pagecount = pb.countpage();
if (spagenow != null) {
pagenow = Integer.parseInt(spagenow);
if (pagenow > pagecount)
pagenow = pagecount;
} else {
pagenow = 1;
}
return pagenow;
}

public int countpage() {
JDBC db = new JDBC();
String sql = "select count(*) from ZC_Attendstatus";
ResultSet rs = db.query(sql);
try {
while (rs.next())
rowcount = rs.getInt(1);
} catch (SQLException e) {
e.printStackTrace();
}
if (rowcount % pagesize == 0)
pagecount = rowcount / pagesize;
else
pagecount = rowcount / pagesize + 1;
return pagecount;
}

public int rowpage() throws SQLException {
JDBC db = new JDBC();
String sql = "select count(*) from ZC_Attendstatus";
for (ResultSet rs = db.query(sql); rs.next();)
rowcount = rs.getInt(1);

return rowcount;
}

}




String spagenow = request.getParameter("pagenow");
int pagesize = 20;//每页显示的记录
int pagenow = spage.fpage(spagenow);//希望显示第几页
int rowcount = spage.rowpage();//共有几条记录(查表获得)
int pagecount = spage.countpage();//共有几页(计算)
List list = sq.specialtySetupQuery(pagesize,pagenow);
<%
if(pagenow!=1){%>
<html:link page="/specialtysetuppage.do?pagenow=1">首页</html:link>
<%}%>

[当前是第
<font color="red"><%=pagenow %></font>页] [共有
<font color="red"><%=rowcount %></font>记录][共有
<font color="red"><%=pagecount %></font>页]
<%if(pagenow!=1){%>
<html:link page="<%="/specialtysetuppage.do?pagenow="+(pagenow-1) %>">上一页</html:link>

<%} %>
<%if(pagenow<pagecount){%>

 
<html:link page="<%="/specialtysetuppage.do?pagenow="+(pagenow+1) %>">下一页</html:link>

<%}%>
<%
if(pagenow!=pagecount){%>
<html:link page="<%="/specialtysetuppage.do?pagenow="+pagecount %>">尾页</html:link>
<%}%>
加载更多回复(4)

81,091

社区成员

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

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