读书人

旁人做的一个不咋地的java数据分页工具

发布时间: 2012-11-10 10:48:50 作者: rapoo

别人做的一个不咋地的java数据分页工具类
/**
这个是我在网上看到的一个帖子自称是比较完美的分页类,我觉得并不是,譬如他这样HTML与js,java代码包括提交用的form元数都冗杂在一起,如果页面查询有其他的参数怎么办,先列在这里,过几天我自己写一个真正完美的分页组件
*/

public class Pagination {
/**
* 当前页
*/
private int curPage;
/**
* 每页显示的记录数
*/
private int pageSize;
/**
* 记录总行数
*/
private int rowsCount;
/**
* 总页数
*/
private int pageCount;

/**
*
* @param totalSize 总记录数
* @param curPage 当前的页码数
* @param pageSize 每页记录数
*
*/
public Pagination(int totalSize, int curPage, int pageSize) {
this.curPage = curPage;
this.pageSize = pageSize;
this.rowsCount = totalSize;
this.pageCount = (int) Math.ceil((double) rowsCount / pageSize);
}

public int getCurPage() {
return curPage;
}


public int getPageCount() {
return pageCount;
}

public int getPageSize() {
return pageSize;
}

public int getRowsCount() {
return rowsCount;
}
//判断是否首页
public boolean isFirst() {
return (curPage==1)?true:false;
}
//判断是否尾页
public boolean isLast() {
return (curPage==pageCount)?true:false;
}
/**
* 获取工具条
* @return 返回用于翻页、跳转、控制页面大小的工具条
*/
public String getToolBar()
{
String str="<script>function changePageValue(cur_page) {if(document.all.ischange!=null){if(document.all.ischange.value=='1'){document.all.isFirstQuery.value='0';}else{document.all.isFirstQuery.value='1';}}else{document.all.isFirstQuery.value='1';} var curPage=document.getElementById('cur_page');"
+"curPage.value=cur_page;var form=document.forms[0];var a=form.submit();if(a==false){return false;}form.onsubmit();}function mySubmit(){goto_Page(); if(document.all.ischange!=null){if(document.all.ischange.value=='1'){document.all.isFirstQuery.value='0';}else{document.all.isFirstQuery.value='1';}}else{document.all.isFirstQuery.value='1';} var form = document.forms[0];form.submit();}"
+"function goto_Page(){document.getElementById('cur_page').value=document.forms[0].txtPage.value;}</script>";
str+="<input type='hidden' name='cur_page' id='cur_page' value='"+curPage+"'>";
str+="<input type='hidden' name='pageCount' id='pageCount' value='"+pageCount+"'>";
str+="<input type='hidden' name='rowsCount' id='rowsCount' value='"+rowsCount+"'>";
str+="<input type='hidden' name='isFirstQuery' id='isFirstQuery'>";
HttpServletRequest request = ServletActionContext.getRequest();
String root = request.getContextPath();
if(isFirst())
str+="<img src='"+root+"/images/pg-first.gif' alt='首页' border='0'> <img src='"+root+"/images/pg-prev.gif' alt='上一页' border='0'> ";
else
{
str+="<a style='cursor:hand;FONT-SIZE: 12px; COLOR: #D80000;' onclick='javascript:changePageValue(1)'><img src='"+root+"/images/pg-first.gif' alt='首页' border='0'></a> ";
str+="<a style='cursor:hand;FONT-SIZE: 12px; COLOR: #D80000;' onclick='javascript:changePageValue("+(curPage-1)+")'><img src='"+root+"/images/pg-prev.gif' alt='上一页' border='0'></a> ";
}
if(isLast())
str+="<img src='"+root+"/images/pg-next.gif' alt='下一页' border='0'> <img src='"+root+"/images/pg-last.gif' alt='尾页' border='0'> ";
else
{
str+="<a style='cursor:hand;FONT-SIZE: 12px; COLOR: #D80000;' onclick='javascript:changePageValue("+(curPage+1)+")'><img src='"+root+"/images/pg-next.gif' alt='下一页' border='0'></a> ";
str+="<a style='cursor:hand;FONT-SIZE: 12px; COLOR: #D80000;' onclick='javascript:changePageValue("+pageCount+")'><img src='"+root+"/images/pg-last.gif' alt='末页' border='0'></a> ";
}
str+=" 共 <b>"+rowsCount+"</b> 条记录 ";
if (curPage > pageCount){
curPage = pageCount;
}
str+=" [当前第"+this.curPage+"页]/[共 <b>"+pageCount+"</b> 页] ";
str+=" 跳转到第 <input type='text' class='button01' onfocus='this.focus();this.select();' id='txtPage' value='"+curPage+"' size='2' name='txtPage'> 页 ";
str+=" <input type='button' class='input2' title='跳转' style='background-image:url("+root+"/images/go.gif);width:18px;border:0px;cursor:pointer;height:18px;' name='ok' onclick='changePageValue(document.forms[0].txtPage.value)'>";
return str;
}
public String toString()
{
String str = "";
str = "该类是用于分页的工具类";
return str;
}
}

读书人网 >编程

热点推荐