一个通用的jsp分页PageBean
程序员文章站
2023-12-05 16:11:58
package com.shaccp.web.util; import java.util.list; public class pagebean { /** * * *...
package com.shaccp.web.util;
import java.util.list;
public class pagebean {
/**
*
*
* @author ppy 2008-10-18 14:3:56
* totalrecords 总记录数
* list 保存分页的数据
* pageno 当前页
* pagesize 页大小
* query 保存用户查询的字符串
* pageaction 操作分页的servlet或action(struts)
* method (struts中action对应的method)
*
*
*/
private int totalrecords;
private list list;
private int pageno;
private int pagesize;
private string query;
private string pageaction;
private string method;
public void setpageaction(string pageaction) {
this.pageaction = pageaction;
}
public void setmethod(string method) {
this.method = method;
}
public list getlist() {
return list;
}
public void setlist(list list) {
this.list = list;
}
public int getpageno() {
return pageno;
}
public void setpageno(int pageno) {
this.pageno = pageno;
}
public int getpagesize() {
return pagesize;
}
public void setpagesize(int pagesize) {
this.pagesize = pagesize;
}
public int gettotalrecords() {
return totalrecords;
}
public void settotalrecords(int totalrecords) {
this.totalrecords = totalrecords;
}
public void setquery(string query) {
this.query = query;
}
/**
* 取得总页数的方法 return
* totalrecords%pagesize==0?(totalrecords/pagesize):(totalrecords/pagesize+1)
*
* @return
*/
public int gettotalpages() {
return (totalrecords + pagesize - 1) / pagesize;
}
/**
* 得到首页
*
* @return
*/
public int gettoppage() {
return 1;
}
/**
* 得到上一页
*
* @return
*/
public int getpreviouspageno() {
if (pageno <= 1)
return 1;
else
return (pageno - 1);
}
/**
* 得到下一页
*
* @return
*/
public int getnextpageno() {
if (pageno >= gettotalpages()) {
return gettotalpages() == 0 ? 1 : gettotalpages();
} else {
return pageno + 1;
}
}
/**
* 得到尾页
*
* @return
*/
public int getbottompageno() {
return gettotalrecords() == 0 ? 1 : gettotalpages();
}
//页面分页导航的链接 方式一
public string getpagetoolbar1() {
string str = "";
str += "<a href='" + pageaction + "?method=" + method + "&userquery="
+ query + "&pageno=" + getpreviouspageno() + "&pagesize="
+ pagesize + "'>上一页</a> ";
str += "<a href='" + pageaction + "?method=" + method + "&userquery="
+ query + "&pageno=" + getnextpageno() + "&pagesize="
+ pagesize + "'>下一页</a>";
return str;
}
//页面分页导航的链接 方式二
public string getpagetoolbar2() {
string str = "";
int pagesplit = (pageno / 5) * 5;
for (int i = pagesplit - 1; i < (pagesplit + 6); i++) {
if (i <= 0) {
} else if (pageno == i) {
str += i + " ";
} else if (i > gettotalpages()) {
} else {
str += "<a href='" + pageaction + "?method=" + method
+ "&userquery=" + query + "&pageno=" + i + "&pagesize="
+ pagesize + "'>" + i + "</a>" + " ";
}
}
return str;
}
}
import java.util.list;
public class pagebean {
/**
*
*
* @author ppy 2008-10-18 14:3:56
* totalrecords 总记录数
* list 保存分页的数据
* pageno 当前页
* pagesize 页大小
* query 保存用户查询的字符串
* pageaction 操作分页的servlet或action(struts)
* method (struts中action对应的method)
*
*
*/
private int totalrecords;
private list list;
private int pageno;
private int pagesize;
private string query;
private string pageaction;
private string method;
public void setpageaction(string pageaction) {
this.pageaction = pageaction;
}
public void setmethod(string method) {
this.method = method;
}
public list getlist() {
return list;
}
public void setlist(list list) {
this.list = list;
}
public int getpageno() {
return pageno;
}
public void setpageno(int pageno) {
this.pageno = pageno;
}
public int getpagesize() {
return pagesize;
}
public void setpagesize(int pagesize) {
this.pagesize = pagesize;
}
public int gettotalrecords() {
return totalrecords;
}
public void settotalrecords(int totalrecords) {
this.totalrecords = totalrecords;
}
public void setquery(string query) {
this.query = query;
}
/**
* 取得总页数的方法 return
* totalrecords%pagesize==0?(totalrecords/pagesize):(totalrecords/pagesize+1)
*
* @return
*/
public int gettotalpages() {
return (totalrecords + pagesize - 1) / pagesize;
}
/**
* 得到首页
*
* @return
*/
public int gettoppage() {
return 1;
}
/**
* 得到上一页
*
* @return
*/
public int getpreviouspageno() {
if (pageno <= 1)
return 1;
else
return (pageno - 1);
}
/**
* 得到下一页
*
* @return
*/
public int getnextpageno() {
if (pageno >= gettotalpages()) {
return gettotalpages() == 0 ? 1 : gettotalpages();
} else {
return pageno + 1;
}
}
/**
* 得到尾页
*
* @return
*/
public int getbottompageno() {
return gettotalrecords() == 0 ? 1 : gettotalpages();
}
//页面分页导航的链接 方式一
public string getpagetoolbar1() {
string str = "";
str += "<a href='" + pageaction + "?method=" + method + "&userquery="
+ query + "&pageno=" + getpreviouspageno() + "&pagesize="
+ pagesize + "'>上一页</a> ";
str += "<a href='" + pageaction + "?method=" + method + "&userquery="
+ query + "&pageno=" + getnextpageno() + "&pagesize="
+ pagesize + "'>下一页</a>";
return str;
}
//页面分页导航的链接 方式二
public string getpagetoolbar2() {
string str = "";
int pagesplit = (pageno / 5) * 5;
for (int i = pagesplit - 1; i < (pagesplit + 6); i++) {
if (i <= 0) {
} else if (pageno == i) {
str += i + " ";
} else if (i > gettotalpages()) {
} else {
str += "<a href='" + pageaction + "?method=" + method
+ "&userquery=" + query + "&pageno=" + i + "&pagesize="
+ pagesize + "'>" + i + "</a>" + " ";
}
}
return str;
}
}