如何用java实现分页效果(eclipse工具)

2024-12-04 12:21:01
推荐回答(5个)
回答(1):

package dl.wsxx.base;
public class Pager {
private int totalRows; // 总行数
private int pageSize; // 每页弊缓显示的行数
private int currentPage; // 当前页号
private int totalPages; // 总页数
private int startRow; // 当前页在数据纳拆库中的起始行
private int pageStartRow; // 当前页开始行
private int pageEndRow; // 当前页结束行洞卜枣
private int hasNextPage; // 下一页存在标识[0:不存在,1:存在]
private int hasPreviousPage; // 前一页存在标识[0:不存在,1:存在]
public Pager() {
}

public Pager(int _totalRows,int _pageSize) {
pageSize = _pageSize;
totalRows = _totalRows;
totalPages = totalRows / pageSize;
int mod = totalRows % pageSize;
if (mod > 0) {
totalPages++;
}
currentPage = 1;
startRow = 0;
}
public int getStartRow() {
return startRow;
}
public int getpageStartRow() {
return pageStartRow;
}
public int getpageEndRow() {
return pageEndRow;
}
public int getTotalPages() {
return totalPages;
}
public int getCurrentPage() {
return currentPage;
}
public int getPageSize() {
return pageSize;
}
public int getHasNextPage() {
return hasNextPage;
}
public int getHasPreviousPage() {
return hasPreviousPage;
}
public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
}
public void setStartRow(int startRow) {
this.startRow = startRow;
}
public void setPageStartRow(int pageStartRow) {
this.pageStartRow = pageStartRow;
}
public void setPageEndRow(int pageEndRow) {
this.pageEndRow = pageEndRow;
}
public void setTotalPages(int totalPages) {
this.totalPages = totalPages;
}
public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public void setHasNextPage(int hasNextPage) {
this.hasNextPage = hasNextPage;
}
public void setHasPreviousPage(int hasPreviousPage) {
this.hasPreviousPage = hasPreviousPage;
}
public int getTotalRows() {
return totalRows;
}
public void first() {
currentPage = 1;
startRow = 0;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void previous() {
if (currentPage == 1) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void next() {
if (currentPage < totalPages) {
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void last() {
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
pageStartRow = startRow + 1;
this.hasFlagSet(currentPage, totalPages);
if (this.hasNextPage == 0) {
pageEndRow = totalRows;
} else {
pageEndRow = startRow + pageSize;
}
}
public void refresh(int _currentPage) {
currentPage = _currentPage;
if (currentPage > totalPages) {
last();
}
this.hasFlagSet(currentPage, totalPages);
}
private void hasFlagSet(int currentPage, int totalPages) {
if (currentPage == totalPages) {
if (currentPage == 1) {
this.hasPreviousPage = 0;
this.hasNextPage = 0;
} else {
this.hasPreviousPage = 1;
this.hasNextPage = 0;
}
} else {
if (currentPage == 1) {
this.hasPreviousPage = 0;
this.hasNextPage = 1;
} else {
this.hasPreviousPage = 1;
this.hasNextPage = 1;
}
}
}
}

这是我的工程里的分页核心代码,希望对你有用,还有ssh分页文档,可以参照研究一下。

回答(2):

用java实现翻页代码跟eclipse没有关系。
参考代码如下:

<%
const MaxPerPage=20
dim sql
dim rs
dim totalPut
dim CurrentPage
dim TotalPages
dim i,j
%>模橘


<%
conn = "DBQ=" + server.mappath("wj028.mdb") + ";DefaultDir=;DRIVER={Microsoft Access Driver (*.mdb)};"
sql = "SELECT * FROM USER order by id desc"
set rs=server.createobject("adodb.recordset")
rs.open SQL,conn,1,1
rs.MoveFirst
rs.pagesize=MaxPerPage
howmanyfields=rs.Fields.Count-1

If trim(Request("Page"))<>戚燃"" then
CurrentPage= CLng(request("Page"))
If CurrentPage> rs.PageCount then
CurrentPage = rs.PageCount
End If
Else
CurrentPage= 1
End If

if rs.eof then
response.write "

没有记录!

"
else
totalPut=rs.recordcount
if CurrentPage<>1 then
if (currentPage-1)*MaxPerPagers.move(currentPage-1)*MaxPerPage
dim bookmark
bookmark=rs.bookmark
end if
end if

dim n,k
if (totalPut mod MaxPerPage)=0 then
n= totalPut \ MaxPerPage
else
n= totalPut \ MaxPerPage + 1
end if%>
<% i=0
do while not rs.EOF and i%>
<%
rs.movenext
i=i+1
loop
%>
第<%=currentpage%>页
共<%=n%>页
<%
k=currentpage
if k<>1 then
response.write "["+"首页]"
response.write "["+"旦仔团上一页]"
else
Response.Write "[首页][上一页]"
end if
if k<>n then
response.write "["+"下一页] "
response.write "["+"尾页] "
else
Response.Write "[下一页][尾页]"
end if
%>
<%
end if
set rs=nothing
set conn=nothing
%>

回答(3):

分页无非就那几个参数,每页显示数、当前页、总数据量,然后根据公式去算把,很简单。稍微复杂一点的是根据条件查询,配改查询的结果分页,不灶祥过培辩判你思路对了也没问题

回答(4):

这个跟eclipse没关系,参考代码:

package dl.wsxx.base;
public class Pager {
    private int totalRows; // 总行数
    private int pageSize; // 每页显示的行数
    private int currentPage; // 当前页号
    private int totalPages; // 总页数
    private int startRow; // 当前页在数据库中的起始行
    private int pageStartRow; // 当前页开始行
    private int pageEndRow; // 当前页结束行
    private int hasNextPage; // 下一页存在标识[0:不存在,1:存在]
    private int hasPreviousPage; // 前一页存在标识[0:不存在,1:存在]
    public Pager() {
    }
                                                     誉拦                             李培                               
    public Pager(int _totalRows,int _pageSize) {
        pageSize = _pageSize;
        totalRows = _totalRows;
        totalPages = totalRows / pageSize;
        int mod = totalRows % pageSize;
        if (mod > 0) {
            totalPages++;
        }
        currentPage = 1;
        startRow = 0;
    }
    public int getStartRow() {
        return startRow;
    }
    public int getpageStartRow() {
        return pageStartRow;
    }
    public int getpageEndRow() {
        return pageEndRow;
    }
    public int getTotalPages() {
        return totalPages;
    }
    public int getCurrentPage() {
        return currentPage;
    }
    public int getPageSize() {
        return pageSize;
    }
    public int getHasNextPage() {
        return hasNextPage;
    }
    public int getHasPreviousPage() {
        return hasPreviousPage;
    }
    public void setTotalRows(int totalRows) {
        this.totalRows = totalRows;
    }
    public void setStartRow(int startRow) {
        this.startRow = startRow;
    }
    public void setPageStartRow(int pageStartRow) {
        this.pageStartRow = pageStartRow;
   庆扰胡 }
    public void setPageEndRow(int pageEndRow) {
        this.pageEndRow = pageEndRow;
    }
    public void setTotalPages(int totalPages) {
        this.totalPages = totalPages;
    }
    public void setCurrentPage(int currentPage) {
        this.currentPage = currentPage;
    }
    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
    public void setHasNextPage(int hasNextPage) {
        this.hasNextPage = hasNextPage;
    }
    public void setHasPreviousPage(int hasPreviousPage) {
        this.hasPreviousPage = hasPreviousPage;
    }
    public int getTotalRows() {
        return totalRows;
    }
    public void first() {
        currentPage = 1;
        startRow = 0;
        pageStartRow = startRow + 1;
        this.hasFlagSet(currentPage, totalPages);
        if (this.hasNextPage == 0) {
            pageEndRow = totalRows;
        } else {
            pageEndRow = startRow + pageSize;
        }
    }
    public void previous() {
        if (currentPage == 1) {
            return;
        }
        currentPage--;
        startRow = (currentPage - 1) * pageSize;
        pageStartRow = startRow + 1;
        this.hasFlagSet(currentPage, totalPages);
        if (this.hasNextPage == 0) {
            pageEndRow = totalRows;
        } else {
            pageEndRow = startRow + pageSize;
        }
    }
    public void next() {
        if (currentPage < totalPages) {
            currentPage++;
        }
        startRow = (currentPage - 1) * pageSize;
        pageStartRow = startRow + 1;
        this.hasFlagSet(currentPage, totalPages);
        if (this.hasNextPage == 0) {
            pageEndRow = totalRows;
        } else {
            pageEndRow = startRow + pageSize;
        }
    }
    public void last() {
        currentPage = totalPages;
        startRow = (currentPage - 1) * pageSize;
        pageStartRow = startRow + 1;
        this.hasFlagSet(currentPage, totalPages);
        if (this.hasNextPage == 0) {
            pageEndRow = totalRows;
        } else {
            pageEndRow = startRow + pageSize;
        }
    }
    public void refresh(int _currentPage) {
        currentPage = _currentPage;
        if (currentPage > totalPages) {
            last();
        }
        this.hasFlagSet(currentPage, totalPages);
    }
    private void hasFlagSet(int currentPage, int totalPages) {
        if (currentPage == totalPages) {
            if (currentPage == 1) {
                this.hasPreviousPage = 0;
                this.hasNextPage = 0;
            } else {
                this.hasPreviousPage = 1;
                this.hasNextPage = 0;
            }
        } else {
            if (currentPage == 1) {
                this.hasPreviousPage = 0;
                this.hasNextPage = 1;
            } else {
                this.hasPreviousPage = 1;
                this.hasNextPage = 1;
            }
        }
    }
}

回答(5):

我也想知道