📄 page.java
字号:
/*
* Copyright (c) 2008-2010 Tanming1003 Inc.
* All rights reserved.
*
* tanming1003<tanming1003@163.com>
*
*
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* Neither the name of tanming1003 nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
package hunnu.edu.cn.product.common.splitPages;
import hunnu.edu.cn.product.log.LogFactory;
import hunnu.edu.cn.product.log.LogLog;
import java.io.Serializable;
/**
* @author tanming1003@163.com
* @date 2008-10-10
* @time 下午04:20:06
* @project_name Product
* @package_name hunnu.edu.cn.product.common.splitPages
* @file_name Page.java
* @version 1.0
*/
public class Page implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 8555642164170733863L;
private final LogLog log=LogFactory.newInstance().getLog(this.getClass());
public static final Page EMPTY_PAGE=new Page();
public static final int DEFAULT_PAGE_SIZE=10;//默认页面的大小
private int start;//本页数据在数据库中开始的位置
private int avaCount;//本页可用的数据条数
private int totalSize;//数据库中总记录条数
private boolean isFirstPage;//是否是第一页
private boolean isLastPage;//是否是最后一页
private Object data;//本页包含的数据
private int pageSize=DEFAULT_PAGE_SIZE;//页面大小
private int currentPageNo;//本页面的号码
private int totalPageCount; //总页面数
protected Page()
{
super();
}
/**
* 分页数据初始方法,由子类调用
* @param start 本页数据在数据库中开始的位置
* @param avaCount 本页可用的数据条数
* @param totalSize 数据库中总记录条数
* @param pageSize 页面的大小
* @param data 本页包含的数据
*/
protected Page(int start,int avaCount,int totalSize,int pageSize,Object data)
{
this.initPage(start, avaCount, totalSize, pageSize, data);
}
/**
* 分页数据初始方法,由子类调用
* @param start 本页数据在数据库中开始的位置
* @param avaCount 本页可用的数据条数
* @param totalSize 数据库中总记录条数
* @param pageSize 本页面的大小
* @param data 本页包含的数据
*/
protected void initPage(int start,int avaCount,int totalSize,int pageSize,Object data)
{
this.start=start;
this.avaCount=avaCount;
this.totalSize=totalSize;
this.pageSize=pageSize;
this.data=data;
if(avaCount>totalSize)
{
log.error("页面记录数大于数据库总记录数!",new RuntimeException("页面记录数大于数据库总记录数!"));
}
this.currentPageNo=(start-1)/pageSize+1;
this.totalPageCount=(totalSize-1)/pageSize+1;
if(totalSize==0&&avaCount==0)
{
this.currentPageNo=1;
this.totalPageCount=1;
}
}
/**
* 获得本页面的数据
* @return 本页的数据
*/
public Object getData()
{
return this.data;
}
/**
* 获得本页的数据记录大小
* @return 本页的记录大小
*/
public int getPageSize()
{
return this.avaCount;
}
public int getTotalSize(){
return this.totalSize;
}
/**
* 获得本页面记录在数据库的开始位置
* @return 本页面记录在数据库的开始位置
*/
public int getStart()
{
return this.start;
}
/**
*获得本页面记录在数据库中最后一个记录的位置
* @return
*/
public int getEnd()
{
int end=start+pageSize-1;
if(end<0)
return 0;
return end;
}
/**
* 获得本页面的号码
* @return 本页面的编号
*/
public int getCurrentPageNo()
{
return this.currentPageNo;
}
/**
* 获得总页面数
* @return 总页面数
*/
public int getTotalPageCount()
{
return this.totalPageCount;
}
/**
* 判断本页面是否有下一页
* @return 是否有下一页
*/
public boolean hasNextPage()
{
if(avaCount==0&&totalSize==0)
return false;
return (start-1<totalSize);
}
/**
* 判断本页面是否有上一页
* @return 是否有上一页
*/
public boolean hasPreviousPage()
{
return (this.getCurrentPageNo()>1);
}
/**
* 判断本页面是否为第一页
* @return 本页面是否为第一页
*/
public boolean isFirstPage()
{
isFirstPage=!hasPreviousPage();
return isFirstPage;
}
/**
* 判断本页面是否为最后一页
* @return 本页面是否为最后一页
*/
public boolean isLastPage()
{
isLastPage=!hasNextPage();
return isLastPage;
}
/**
* 获得上一页记录在数据库开始位置
* @return 上一页记录在数据库开始位置
*/
public int getStartOfPreviousPage()
{
int previousStart=this.start-this.pageSize;
return (previousStart>1)?previousStart:1;
}
/**
* 获得下一页记录在数据库的开始位置
* @return 下一页记录在数据库的开始位置
*/
public int getStartOfNextPage()
{
return start+avaCount;
}
/**
* 获得任何页面的记录在数据库的开始位置
* @param pageNo 页面的号码(第几页)
* @return 任何页面的记录在数据库的开始位置
*/
public int getStartOfAnyPage(int pageNo,int pageSize)
{
int index=(pageNo-1)*pageSize+1;
if(index<1)
index=1;
return index;
}
/**
* 获得实现分页处理的javascript代码
* @param queryJSFunctionName 实现分页的JS脚本名字,页码变动时会自动回调该方法
* @param pageNoParamName 页码参数名称
* @return
*/
public String getHTML(String queryJSFunctionName,String pageNoParamName)
{
if(this.getTotalPageCount()<1)
return "<input type='hidden' name="+pageNoParamName+" value='1'>";
if(queryJSFunctionName==null||queryJSFunctionName.equals(""))
queryJSFunctionName="gotoPage";
if(pageNoParamName==null||pageNoParamName.equals(""))
pageNoParamName="pageno";
String gotoPage=" "+queryJSFunctionName;
StringBuffer html=new StringBuffer("\n");
html.append("<script language=\"javascript1.2\" >\n");
html.append("function").append(gotoPage).append("(pageNo){\n");
html.append(" var curPage=1;\n");
html.append(" try{ curPage=document.all[\"");
html.append(pageNoParamName).append("\"].value;\n");
html.append(" document.all[\"").append(pageNoParamName).append("\"].value=pageNo;\n");
html.append(" return true;\n");
html.append(" }catch(e){\n");
html.append(" alert('尚未定义查询方法function ");
html.append(queryJSFunctionName).append("()!');\n");
html.append(" document.all[\"").append(pageNoParamName).append("\"].value=curPage;\n");
html.append(" return false;");
html.append(" }\n");
html.append("}\n");
html.append("</script>\n");
html.append("\n");
html.append("<table border=0 cellspacing=0 cellpadding=0 align=center width=80%>\n");
html.append(" <tr>\n");
html.append(" <td align=left>\n");
html.append(" 共 ").append(this.getTotalPageCount()).append(" 页 ");
html.append(" </td>\n");
html.append(" <td align=right>\n");
if(hasPreviousPage())
{
html.append(" <a href='javascript:").append(gotoPage).append("(").append(this.getCurrentPageNo()-1).append(")'>上一页</a>\n");
}
html.append(" </td>\n");
html.append(" <td align=right>\n");
html.append(" 第 ").append(this.getCurrentPageNo()).append(" 页");
html.append(" </td>\n");
html.append(" <td align=right>\n");
if(hasNextPage())
{
html.append(" <a href='javascript:").append(gotoPage).append("(").append(this.getCurrentPageNo()+1).append(")'>下一页</a>\n");
}
html.append(" </td>\n");
html.append(" </tr>\n");
html.append("</table>");
html.append("\n");
return html.toString();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -