记录下分页用到的代码:
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;
public class DoFenYeTaglib extends SimpleTagSupport {
private int pageNum;//当前页数
private int totalPage;//总页数
private int allCounts;//取出记录总条数
private int count;//每页显示多少条
private String className;
private String para;
private String functionName;
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
public String getFunctionName() {
return functionName;
}
public int getAllCounts() {
return allCounts;
}
public void setAllCounts(int allCounts) {
this.allCounts = allCounts;
}
public void setFunctionName(String functionName) {
this.functionName = functionName;
}
public String getPara() {
return para;
}
public void setPara(String para) {
this.para = para;
}
public int getPageNum() {
return pageNum;
}
public void setPageNum(int pageNum) {
this.pageNum = pageNum;
}
public int getTotalPage() {
return totalPage;
}
public void setTotalPage(int totalPage) {
this.totalPage = totalPage;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public void doTag() throws JspException, IOException {
JspWriter out = getJspContext().getOut();
int liststep = 10;//最多显示分页页数
int pages = 1;//默认显示第一页
if (pageNum != 0) {
pages = pageNum;//分页页码变量
}
if (totalPage < pages) {
pages = totalPage;//如果分页变量大总页数,则将分页变量设计为总页数
}
if (pages < 1) {
pages = 1;//如果分页变量小于1,则将分页变量设为1
}
int listbegin = (pages - (int) Math.ceil((double) liststep / 2));//从第几页开始显示分页信息
if (listbegin < 1) {
listbegin = 1;
}
int listend = pages + liststep/2;//分页信息显示到第几页
if (listend > totalPage) {
listend = totalPage + 1;
}
// 显示 首页 上一页
if (pages > 1) {
out.print("<a href='" + this.className + ".do?" + this.para + "="+ this.functionName + "&pageNum=1&count="
+ this.count + "&allCounts=" + this.allCounts +"'> 首页 </a>");
out.print("<a href='" + this.className + ".do?" + this.para + "="+ this.functionName + "&pageNum=" + (this.pageNum - 1)
+ "&count=" + this.count + "&allCounts=" + this.allCounts + "'> <上一页 </a>");
}
//显示 分页码
for (int i = listbegin; i < listend; i++) {
if (i != pages) {//如果i不等于当前页
out.print("<a href='" + this.className + ".do?" + this.para + "="+ this.functionName + "&pageNum=" + i
+ "&count=" + this.count + "&allCounts=" + this.allCounts + "'> <b>" + i + "</b> </a>");
} else {
out.print(" <b>" + i + " </b>");
}
}
//显示 尾页 下一页
if (pages != totalPage) {
out.print("<a href='" + this.className + ".do?" + this.para + "="+ this.functionName + "&pageNum=" + (this.pageNum + 1)
+ "&count=" + this.count + "&allCounts=" + this.allCounts + "'> 下一页> </a>");
out.print("<a href='" + this.className + ".do?" + this.para + "="+ this.functionName + "&pageNum=" + this.totalPage
+ "&count=" + this.count + "&allCounts=" + this.allCounts + "'> 尾页 </a>");
}
super.doTag();
}
}