一个我的大问题(分页)

cw550284 2008-05-27 10:24:40
如何实现分页的功能!!
我用的是struts 框架开发的!!
...全文
106 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
cw550284 2009-03-31
  • 打赏
  • 举报
回复
cw550284 2008-12-20
  • 打赏
  • 举报
回复
谢谢
cw550284 2008-12-20
  • 打赏
  • 举报
回复
谢谢
yehong_sky 2008-12-12
  • 打赏
  • 举报
回复
最好是写一个类文件...方便调用...给出代码如下...非常好用

package ita.lm.gfjt.itcn.util;

/**
* @author
* Dec 15, 2005 9:06:55 AM
*/
public class PageUtil {

private int curPage=1; // 当前页面
private int startRow;//起始行
private int endRow;//结束行
private int totalRow;//总条数
private int totalPage;//总页数
private int pageRow;//每页面显示的条目数
private int maxResult;//查询的最大数目
private boolean needPaging;//是否需要分页


public PageUtil() {
init();
}

/**
* 初始化
*
*/
public void init() {
curPage = 1;
startRow = 0;
endRow = 0;
pageRow = 10;
totalPage = 1;
totalRow = 0;
needPaging = true;
maxResult=pageRow;
}


/**
* 获取 curPage 的信息
* Dec 15, 2005 9:42:37 AM Hangzhou Inrun
* @return Returns the curPage int.
*/
public int getCurPage() {
return curPage;
}
/**
* 设置 curPage 的值 类型 为 int
* Dec 15, 2005 9:42:37 AM hangzhou Inrun
* @param curPage The curPage to set.
*/
public void setCurPage(int curPage) {
if (this.curPage < 1) {
this.curPage = 1;
}else{
this.curPage = curPage;
}

}


/**
* @return Returns the pageRow.
*/
public int getPageRow() {
return pageRow;
}

/**
* @param pageRow The pageRow to set.
*/
public void setPageRow(int pageRow) {
this.pageRow = pageRow;
}

/**
* 获取 maxResult 的信息
* Dec 15, 2005 9:45:23 AM Hangzhou Inrun
* @return Returns the maxResult int.
*/
public int getMaxResult() {
return maxResult;
}
/**
* 设置 maxResult 的值 类型 为 int
* Dec 15, 2005 9:45:23 AM hangzhou Inrun
* @param maxResult The maxResult to set.
*/
public void setMaxResult(int maxResult) {
this.maxResult = maxResult;
}

/**
* 获取 totalRow 的信息
* Dec 15, 2005 9:45:23 AM Hangzhou Inrun
* @return Returns the totalRow int.
*/
public int getTotalRow() {
return totalRow;
}
/**
* 设置 totalRow 的值 类型 为 int
* Dec 15, 2005 9:45:23 AM hangzhou Inrun
* @param totalRow The totalRow to set.
*/
public void setTotalRow(int totalRow) {
this.totalRow = totalRow;
if(needPaging){
sumPage();
}else{
noSumPage();
}
}
/**
* 获取 endRow 的信息
* Dec 15, 2005 9:42:37 AM Hangzhou Inrun
* @return Returns the endRow int.
*/
public int getEndRow() {
return endRow;
}
/**
* 设置 endRow 的值 类型 为 int
* Dec 15, 2005 9:42:37 AM hangzhou Inrun
* @param endRow The endRow to set.
*/
public void setEndRow(int endRow) {
this.endRow = endRow;
}



/**
* 获取 needPaging 的信息
* Dec 15, 2005 10:01:24 AM Hangzhou Inrun
* @return Returns the needPaging boolean.
*/
public boolean isNeedPaging() {
return needPaging;
}
/**
* 设置 needPaging 的值 类型 为 boolean
* Dec 15, 2005 10:01:24 AM hangzhou Inrun
* @param needPaging The needPaging to set.
*/
public void setNeedPaging(boolean needPaging) {
this.needPaging = needPaging;
}

/**
* 获取 startRow 的信息
* Dec 15, 2005 9:42:37 AM Hangzhou Inrun
* @return Returns the startRow int.
*/
public int getStartRow() {
return startRow;
}
/**
* 设置 startRow 的值 类型 为 int
* Dec 15, 2005 9:42:37 AM hangzhou Inrun
* @param startRow The startRow to set.
*/
public void setStartRow(int startRow) {
this.startRow = startRow;
}
/**
* 获取 totalPage 的信息
* Dec 15, 2005 9:42:37 AM Hangzhou Inrun
* @return Returns the totalPage int.
*/
public int getTotalPage() {
return totalPage;
}
/**
* 设置 totalPage 的值 类型 为 int
* Dec 15, 2005 9:42:37 AM hangzhou Inrun
* @param totalPage The totalPage to set.
*/
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}

/**
* 返回下一页
* @return int nextPage
*/
public int getNextPage() {
if (curPage == totalPage) {
return totalPage;
}
return this.curPage + 1;
}

/**
* 返回上一页
* @return int prePage
*/
public int getPrePage() {
if (curPage == 1) {
return curPage;
}
return this.curPage - 1;
}

/**
* 返回首页
* @return int firstPage
*/
public int getFirstPage() {
return 1;
}

/**
* 分页计算
*/
private void sumPage() {
if (pageRow < 1){
pageRow = 1;
}
if (totalRow > 0) {
totalPage = (int) ((totalRow - 1) / pageRow) + 1; // 计算总页面
if(curPage>totalPage)curPage=totalPage;
if(curPage<1)curPage=1;
this.startRow = pageRow * (curPage - 1); // 当前页第一行显示的记录号
if (curPage *pageRow < totalRow) {
this.endRow = curPage * pageRow+1; // 当前页最后一行
maxResult = pageRow;
} else {
endRow = totalRow+1;
maxResult = totalRow -startRow + 1;
}
} else {
totalRow = 0;
totalPage = 0;
curPage = 1;
startRow = 0;
endRow = 0;
}
}
/**
* 如果不需要分页计算
* @author
* @version PIMS V2.0
* @createTime Dec 15, 2005 10:09:42 AM
*/
private void noSumPage(){
curPage=1;
startRow=0;
endRow=totalRow+1;
maxResult=totalRow;
}
}
huayiluo 2008-05-29
  • 打赏
  • 举报
回复
用了struts就加个hibernate吧。
Query q=session.createQuery("");
q.setFirstResult(arg0)
q.setMaxResults(arg0)
一板砖夯死你 2008-05-29
  • 打赏
  • 举报
回复
自己写个分页的实现方法,再封装成标签,或者可以用displaytag
cw550284 2008-05-29
  • 打赏
  • 举报
回复
有什么方法可以不在页面里面嵌入java代码吗?
cw550284 2008-05-29
  • 打赏
  • 举报
回复
很好!!谢谢!!
小官同志 2008-05-29
  • 打赏
  • 举报
回复
纯js可以控制翻页,就是从数据库里提取数据后,按页的条数,分多个div显示,以隐藏div的形式隐藏不显示部分。
cw550284 2008-05-28
  • 打赏
  • 举报
回复
就是实现下一页,上一页的功能!!
不上后台数据库的分页,就是前台的分页显示!!
谢谢
cw550284 2008-05-28
  • 打赏
  • 举报
回复
就是实现下一页,上一页的功能!!
不上后台数据库的分页,就是前台的分页显示!!
谢谢
longtramp 2008-05-28
  • 打赏
  • 举报
回复
给一个js的实现

//每页显示列表的数量
int pageSize = 6;
String strPagePos = request.getParameter("pageNo");
//显示第几页
int pageNo;
if(strPagePos == null || strPagePos.equals("")){
pageNo = 1;
}else{
try{
pageNo = Integer.parseInt(strPagePos.trim());
}catch(NumberFormatException e){
pageNo = 1;
}
//判断传入参数
if(pageNo <= 0) pageNo = 1;
}

//查询一共有多少条数据
Connection conn = DB.getConn();
Statement count = conn.createStatement();
ResultSet rsCount = count.executeQuery("select count(*)from article where rootid="+id);
rsCount.next();
int RCount = rsCount.getInt(1);

//计算一共有多少页面
int pageCounts = RCount % pageSize == 0 ? RCount / pageSize : RCount /pageSize + 1 ;
//查询的位置大于页面的最大数
if(pageNo > pageCounts)pageNo = pageCounts;

//查询的起始位置
int startPos = (pageNo - 1) * pageSize;

List<Article> articles = new ArrayList<Article>();

//使用数据库分页显示
String sql = "select * from article where rootid=? order by pdate asc limit ?,?";
PreparedStatement pstmt = DB.getPstmt(conn,sql);
pstmt.setInt(1,id);
pstmt.setInt(2,startPos);
pstmt.setInt(3,pageSize);
//System.out.println(sql);
ResultSet rs = DB.getRsQuery(pstmt);

while(rs.next()){
Article a = new Article();
a.setId(rs.getInt("id"));
a.setRootid(rs.getInt("rootid"));
a.setPid(rs.getInt("pid"));
a.setTitle(rs.getString("title"));
a.setPdate(rs.getTimestamp("pdate"));
a.setCon(rs.getString("con"));
a.setIsleaf(rs.getInt("isleaf") == 0 ? true:false);
articles.add(a);
}

jofy1004 2008-05-27
  • 打赏
  • 举报
回复
分页指页面显示咋实现,还是后台处理咋实现?

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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