请问在struts框架中怎么实现JSP页面展示查询记录页面的分页逻辑。具体怎么实现各页的跳转?最好给个例子,麻烦各位大侠了。
解决方案 »
- 请教如何写jsp页面跳转
- 关于urlrewritefilter和struts冲突问题
- B/S求助
- 在服务器端输出是中文,但在jsp页面上输出的是???(?)
- 一个关于smartUpload上传下载的问题!!!
- 请高手赐教了。!急~~~~~~~~
- 在bean中需要使用request应该import什么包?
- 我把tomcat装成win2000服务了,可是....?
- 怎样打印网页中的一个table中的内容,简单!!!
- 请问各位,在iplanet下,我在一台机器上,同时打开两个窗口,应该产生,2个session,为什么iplanet自动把前一个session给注销成后一个session了呢??
- OGNL的表达式
- 用jsp遇到的问题
* 实现数字翻页组件
*
* @author tzc
*
*/
@SuppressWarnings("unused")
public class NumberPage { private Integer currentPage;// 当前页数
private Integer pageSize;// 页容量
private Integer countPage;// 总页数
private Integer totalCount;// 总条数
private String toUrl;// 要发送的地址
private Integer beginPageIndex;// 开始页数
private Integer endPageIndex;// 结束页数
private Integer showPageIndexCount;// 页面显示几个页码编号
private Integer previousPage;// 上一页
private Integer nextPage;// 下一页
private Boolean upOrDownPage;// 是否向上翻页 为真 向上翻页 为假 向下翻页 默认为真
private Integer lastBeginPage;// 保存上回的最第一页 // construct
public NumberPage() {
this.upOrDownPage = true;// 默认向上翻页
} /* Getter & Setter */ public Integer getCurrentPage() {
return currentPage;
} public void setCurrentPage(Integer currentPage) {
if (currentPage <= 1)// 如果当前页数小于等于1 当前页数就为1
{
this.currentPage = 1;
} else if (this.getCountPage() <= currentPage)// 如果当前页数大于等于总页数 就等于总页数
{
this.currentPage = this.countPage;
} else// 否则 当前页 等于 传进来的页数
{
this.currentPage = currentPage;
}
// 开始和结束的页码
if (this.currentPage % (this.showPageIndexCount - 1) == 1) {
if (this.upOrDownPage) {// 如果当前页数除以 每页的间隔-1 ==1
this.setBeginPageIndex(this.currentPage);
this
.setEndPageIndex(this.currentPage
+ this.showPageIndexCount);
} else {
this.setBeginPageIndex(this.currentPage
- this.showPageIndexCount + 1);
if (this.currentPage == 1)// 如果当前页数为1
{
this.setEndPageIndex(this.showPageIndexCount + 1);
} else {
this.setEndPageIndex(this.currentPage + 1);
}
}
} else {
this.setBeginPageIndex(this.getLastBeginPage());
this.setEndPageIndex(this.getBeginPageIndex()
+ this.showPageIndexCount);
}
} public Integer getPageSize() {
return pageSize;
} public void setPageSize(Integer pageSize) {
this.pageSize = pageSize;
} // 求总页数
public Integer getCountPage() {
if (this.totalCount % this.pageSize == 0) {
this.countPage = this.totalCount / this.pageSize;
} else {
this.countPage = (this.totalCount / this.pageSize) + 1;
}
return countPage;
} public void setCountPage(Integer countPage) {
this.countPage = countPage;
} public Integer getTotalCount() {
return totalCount;
} public void setTotalCount(Integer totalCount) {
this.totalCount = totalCount;
} public String getToUrl() {
return toUrl;
} public void setToUrl(String toUrl) {
this.toUrl = toUrl.substring(1) + "¤tPage=";
} // 得到当前显示在页面上的第一个页码
public Integer getBeginPageIndex() {
return beginPageIndex;
} // 个第一个页码赋值
public void setBeginPageIndex(Integer beginPageIndex) {
if (beginPageIndex <= 1) {// 如果开始的页码小于等于1 就等于1
this.beginPageIndex = 1;
} else if (beginPageIndex >= this.getCountPage()) {
// 如果开始的页码大于等于总页数 就等于总也页数
this.beginPageIndex = this.countPage;
} else {
this.beginPageIndex = beginPageIndex;
}
} // 得到当前显示在页面上的最后一个页码
public Integer getEndPageIndex() {
if (this.endPageIndex <= 1 && !this.upOrDownPage) {
this.endPageIndex = this.showPageIndexCount;
}
if (this.endPageIndex >= this.countPage) {
this.endPageIndex = this.countPage + 1;
}
return endPageIndex;
} // 给最后的页码赋值
public void setEndPageIndex(Integer endPageIndex) {
if (endPageIndex <= 1) {// 如果最后的页码小于等于1 最后的页码为1
this.endPageIndex = 1;
} else if (endPageIndex >= this.getCountPage()) {
// 如果最后的页码 大于等于总总页数 就等于总页数
this.endPageIndex = this.countPage + 1;
} else {
this.endPageIndex = endPageIndex;
}
} public Integer getShowPageIndexCount() {
return showPageIndexCount;
} public void setShowPageIndexCount(Integer showPageIndexCount) {
this.showPageIndexCount = showPageIndexCount;
} public Integer getPreviousPage() {// 上一页
// 如果当前页数小于等于1 上一页就为1
if (this.currentPage <= 1) {
return 1;
} else {// 否则就是当前页数-1
return this.currentPage - 1;
}
} public Boolean getUpOrDownPage() {
return upOrDownPage;
} public void setUpOrDownPage(Boolean upOrDownPage) {
this.upOrDownPage = upOrDownPage;
} public void setPreviousPage(Integer previousPage) {
this.previousPage = previousPage;
} public void setNextPage(Integer nextPage) {
this.nextPage = nextPage;
} public Integer getNextPage() {
// 如果当前页数加1到 了最后一页 就显示最后一页
if (this.currentPage + 1 >= this.countPage) {
return this.countPage;
} else {// 否则就是当前页数加1
return this.currentPage <= 1 ? 2 : this.currentPage + 1;
}
} // 个开始和结束的页面赋值
public void setBeginAndEndPageIndex(Integer beginIndex) {
if (this.currentPage % (this.showPageIndexCount - 1) != 1) {
this.setBeginPageIndex(beginIndex);
this.setEndPageIndex(beginIndex + this.showPageIndexCount);
}
} public Integer getLastBeginPage() {
return lastBeginPage;
} public void setLastBeginPage(Integer lastBeginPage) {
this.lastBeginPage = lastBeginPage;
} /**
* Servlet 实现数字翻页 页面请求的参数
*
* @param currentPageAndLast
* 每页的容量
* @param pageSize
* 一次显示页的数量
* @param showPageIndexCount
* //总条数
* @param totalCount
* //要请求的地址
* @param tourl
* 返回输出的翻页工作区
* @return
*/
public String PagerOperation(String currentPageAndLast, int pageSize,
int showPageIndexCount, int totalCount, String tourl) {
String word = "";
this.setToUrl(tourl);
this.setPageSize(pageSize);// 给页容量赋值
this.setShowPageIndexCount(showPageIndexCount);// 每面显示多少页
this.setTotalCount(totalCount);// 给总条数赋值
return printHtmlContent(currentPageAndLast);
} /***************************************************************************
* spring 注入后有些参数已赋值 就可以调用该方法
*
* @param currentPageAndLast
* 接收页面传来的参数
* @param totalCount
* 总条数
* @param url
* 要跳转的路径
* @return
*/
public String printPageBySpring(String currentPageAndLast, int totalCount,
String url) {
this.setTotalCount(totalCount);
this.setToUrl(url);
return printHtmlContent(currentPageAndLast);
} // 打印页面内容
private String printHtmlContent(String currentPageAndLast) {
String word = "";
if (currentPageAndLast != null
&& currentPageAndLast.matches("^\\d+[\\_]{1}\\d+$")) {
String arr[] = currentPageAndLast.split("_");
try {
// 上翻页还是下翻页
if (arr[0].equals(arr[1])) {
this.setUpOrDownPage(false);
} else {
this.setUpOrDownPage(true);
}
this.setLastBeginPage(Integer.parseInt(arr[1]));
this.setCurrentPage(Integer.parseInt(arr[0]));// 给当前页赋值
} catch (Exception e) {
// 处理异常
this.setUpOrDownPage(false);
this.setLastBeginPage(1);
this.setCurrentPage(1);// 给当前页赋值 }
} else {
this.setUpOrDownPage(false);
this.setLastBeginPage(1);
this.setCurrentPage(1);// 给当前页赋值 }
this.setLastBeginPage(this.beginPageIndex);
// 输出翻页工作区
// 如果当前页数为一
if (this.getCurrentPage() == 1) {
word += "上一页 ";// 不准向上翻页
} else// 大于一就可以向上翻页
{
word += "<a href='"
+ toUrl
+ this.getPreviousPage()
+ "_"
+ (this.currentPage == this.getBeginPageIndex() ? this
.getLastBeginPage()
- this.getShowPageIndexCount() : this
.getLastBeginPage()) + "'>上一页</a>";
}
if (this.getBeginPageIndex() > 1) {
word += " <a href='" + toUrl + "1_" + this.getLastBeginPage()
+ "'>1</a> ... ";
}
for (int i = this.getBeginPageIndex(); i < this.getEndPageIndex(); i++) {
// 如果当前页==某页 某页某有样式不准点击
if (i == this.getCurrentPage()) {
word += " " + i + " ";
} else {
word += " <a href='" + toUrl + i + "_"
+ this.getLastBeginPage() + "'>" + i + "</a>";
}
}
if (this.getCurrentPage() < this.getEndPageIndex()) {
// 如果当前最后页小于总页数
if (this.getEndPageIndex() <= this.getCountPage()
&& this.getEndPageIndex() - 1 < this.getCountPage()) {
word += " ... <a href='" + toUrl + this.getCountPage() + "_"
+ this.getLastBeginPage() + "'>" + this.getCountPage()
+ "</a>";
}
} // 如果当前页为最后一页
if (this.getCurrentPage() == this.getCountPage()
|| this.getCountPage() == 0) {
word += " 下一页";// 不准向下翻页
} else// 小于最后一页就可以向下翻
{
word += " <a href='"
+ toUrl
+ this.getNextPage()
+ "_"
+ (this.getEndPageIndex() - 1 == this.currentPage ? this
.getLastBeginPage()
+ this.getShowPageIndexCount() : this
.getLastBeginPage()) + "'>下一页</a>";
}
word += "第 " + this.getCurrentPage() + "/" + this.getCountPage()
+ " 页 共" + this.getTotalCount() + "条数据";
return word;
}}
首先创建一个分页对象:
package com.zf.util;import java.util.List;
public class PageBean {
private int current ;//当前页
private int pageSize ;//每页显示的数量
private int totalRecord;//总记录数
private int totalPage;//总页数
private boolean pre;//是否有上一页
private boolean next;//是否有下一页
private List list;//当前页你要显示的对象集合
public int getCurrent() {
if (current > totalPage)
current = totalPage;
if (current < 1)
current = 1;
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() {
if (list == null || list.size() < 1)
return null;
return list;
} public void setList(List list) {
this.list = list;
} public int getStart() {
return (getCurrent() - 1) * pageSize;
}}
页面的部分代码:
<div id="link">
<c:if test="${classForm.pageBean.pre}">
<a href="javascript:toList(1);"><font color="#CCCCCC">第一页</font></a>
<a href="javascript:toList(${classForm.pageBean.current-1 });"><font color="#CCCCCC">上一页</font></a>
</c:if>
<c:if test="${classForm.pageBean.next}">
<a href="javascript:toList(${classForm.pageBean.current+1 });"><font color="#CCCCCC">下一页</font></a>
<a href="javascript:toList(${classForm.pageBean.totalPage });"><font color="#CCCCCC">最后一页</font></a>
</c:if> 到
<select name="current" id="current" onChange="toList(this.value);">
<c:forEach var="i" begin="1" end="${classForm.pageBean.totalPage}">
<option value="${i}" ${i==classForm.pageBean.current?"selected":""}>
${i }
</option>
</c:forEach>
</select>
页 每页
<input type="text" class="txt" size="2" name="pageSize"
value="${classForm.pageBean.pageSize }" />
条 (共${classForm.pageBean.totalRecord }条记录)
<input type="submit" value="GO" class="btn" />
</div> <script type="text/javascript">
function toList(current){
document.getElementById("current").value=current;
document.forms[0].submit();
}
</script>
private int totalRows; //总行数
private int pageSize = 10; //每页显示的行数
private int currentPage; //当前页号
private int totalPages; //总页数
private int startRow; //当前页在数据库中的起始行 public Page(int _totalRows) {
totalRows = _totalRows;
totalPages=totalRows/pageSize;
int mod=totalRows%pageSize;
if(mod>0){
totalPages++;
}
currentPage = 1;
startRow = 0;
} public int getStartRow() {
return startRow;
} public int getTotalPages() {
return totalPages;
} public int getCurrentPage() {
return currentPage;
} public int getPageSize() {
return pageSize;
} public void setTotalRows(int totalRows) {
this.totalRows = totalRows;
} public void setStartRow(int startRow) {
this.startRow = startRow;
} 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 int getTotalRows() {
return totalRows;
} public void first() {
currentPage = 1;
startRow = 0;
} public void previous() {
if (currentPage == 1) {
return;
}
currentPage--;
startRow = (currentPage - 1) * pageSize;
} public void next() {
if (currentPage < totalPages) {
currentPage++;
}
startRow = (currentPage - 1) * pageSize;
} public void last() {
currentPage = totalPages;
startRow = (currentPage - 1) * pageSize;
} public void refresh(int _currentPage) {
currentPage = _currentPage;
if (currentPage > totalPages)
last();
}}