📄 page.java
字号:
package com.example.businessmodel;
import java.util.*;
import javax.servlet.http.HttpServletRequest;
import org.hibernate.Session;
public class Page {
private List elements;
private int StartPosition;
private int TotalRecords;
private int PageSize;
private String nextLabel = "下一页>>";
private String previousLabel = "<<上一页";
private String totalRecordsLabel = "?条记录";
private String totalLabel = "共?页 ";
private String currentLabel = "当前第?页 ";
private String form = "thisForm";
private String titleColor = "#dfe1ee";
private String listTitleColor = "#9499C6";
private String oddRowColor = "#FFFFFF";
private String evenRowColor = "#ECEDF5";
private final String jumpTo = "\n<script language=\"javascript\">" +
"\n\tfunction jumpto(start, form){" +
"\n\t\tvar oForm = document.all(form);" +
"\n\t\tif(oForm==null) return;" +
"\n\t\toForm.start.value=start;" +
"\n\t\toForm.submit();" +
"\n\t}\n" +
"\nfunction jumptoPage(pageSize, form){" +
"\n\tif(event.keyCode != 13) return;" +
"\n\t\tvar pageValue = parseInt(event.srcElement.value);" +
"\n\t\tif(isNaN(pageValue) || pageValue<=0) {" +
"\n\t\t\tevent.srcElement.value=\"\";" +
"\n\t\t\treturn;" +
"\n\t\t}" +
"\n\t\tjumpto((pageValue-1)*pageSize + 1, form);" +
"\n\t}" +
"\n</script>";
public Page(){
}
public Page(int ps) {
if(ps <= 0) {
throw new RuntimeException("PageSize必须大于零。");
} else {
elements = Collections.EMPTY_LIST;
StartPosition = 1;
TotalRecords = 0;
PageSize = ps;
return;
}
}
public Page(List l, int s, int t, int ps) {
StartPosition = getValidStart(s, t, ps);
elements = new Vector(l);
TotalRecords = t;
PageSize = ps;
}
public List getList() {
return elements;
}
private boolean hasNextPage() {
return (StartPosition + elements.size()) - 1 < TotalRecords;
}
private boolean hasPreviousPage() {
return StartPosition > 1;
}
private int getStartOfNextPage() {
return getValidStart(StartPosition + elements.size(), TotalRecords, PageSize);
}
private int getStartOfPreviousPage() {
return getValidStart(StartPosition - PageSize, TotalRecords, PageSize);
}
public int getSize() {
return elements.size();
}
public int getPageCount() {
return (TotalRecords - 1) / PageSize + 1;
}
public int getPageNumber() {
return (StartPosition - 1) / PageSize + 1;
}
public int getPageNum(){
return StartPosition;
}
private int getPageSize() {
return PageSize;
}
public int getTotalRecords() {
return TotalRecords;
}
private static int getValidStart(int s, int t, int ps) {
if(ps <= 0)
throw new RuntimeException("PageSize必须大于零。");
if(t < 0)
throw new RuntimeException("记录总数不能小于零。");
if(s > t)
return ((t - 1) / ps) * ps + 1;
if(s < 1)
return 1;
else
return s;
}
public String getPageHeader() throws Exception{
String retVal="";
try {
StringBuffer html = new StringBuffer();
if(hasPreviousPage()) {
html.append(appendPageLabel(getStartOfPreviousPage(), previousLabel));
}
if(hasNextPage()) {
html.append(appendPageLabel(getStartOfNextPage(), nextLabel));
}
currentLabel = (appendReplace(currentLabel, " <input type=\"text\" size=\"5\" maxlength=\"5\" value=\"?\" style=\"text-align:right\" onkeyup=\"jumptoPage(" + getPageSize() +",'" + form + "')\">")).toString();
html.append(appendReplace(currentLabel,(new Integer(getPageNumber())).toString()));
html.append(appendReplace(totalLabel,(new Integer(getPageCount())).toString()));
html.append(appendReplace(totalRecordsLabel,(new Integer(getTotalRecords())).toString()));
html.append(jumpTo);
retVal=new String(html);
}
catch(Exception e){
e.printStackTrace();
}
finally{
return retVal;
}
}
private String appendPageLabel(int page, String content) {
return "<a href=\"javascript:jumpto(" + page + ",'" + form + "')\">" + content + "</a> ";
}
private StringBuffer appendReplace(String source, String info){
int index = source.indexOf("?");
StringBuffer ret = new StringBuffer(source);
ret.replace(index, index+1, info);
return ret;
}
public String getTitleColor() {
return titleColor;
}
public void setTitleColor(String titleColor) {
this.titleColor = titleColor;
}
public String getEvenRowColor() {
return evenRowColor;
}
public void setEvenRowColor(String evenRowColor) {
this.evenRowColor = evenRowColor;
}
public String getOddRowColor() {
return oddRowColor;
}
public void setOddRowColor(String oddRowColor) {
this.oddRowColor = oddRowColor;
}
public String getListTitleColor() {
return listTitleColor;
}
public void setListTitleColor(String listTitleColor) {
this.listTitleColor = listTitleColor;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -