📄 paginationtag.java
字号:
package com.y2.hr.human.common;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* 自定义标签类,用于生成分页栏
*
* @author yangjiahui
*
*/
public class PaginationTag extends BodyTagSupport {
/**
* 一页可以放几条记录
*/
private int pageSize;
/**
* 当前页是第几页
*/
private int currPageId;
/**
* 一共有几条
*/
private int recordCount;
/**
* 生成的层的宽度
*/
private String width;
/**
* 生成的层的高度
*/
private String height;
/**
* 生成的层的背景
*/
private String bgColor;
/**
* 点击链接的路径
*/
private String url;
@Override
public int doAfterBody() throws JspException {
// TODO Auto-generated method stub
try {
BodyContent con = super.getBodyContent();
JspWriter out = con.getEnclosingWriter();
// 总页数
int pageCount = (int) Math.ceil((double) recordCount / pageSize);
StringBuffer buf = new StringBuffer();
buf
.append("<div style='border:0px solid black;text-align:right;font-size:12px;");
if (bgColor != null) {
buf.append("background-color: " + bgColor + ";");
}
if (width != null) {
buf.append("width: " + width + ";");
} else {
buf.append("width: 100%;");
}
if (height != null) {
buf.append("height: " + height + ";");
}
buf.append("'>");
buf.append("共有<font color='red'>" + recordCount
+ "</font>条记录 ");
buf.append("共<font color='red'>" + pageCount
+ "</font>页 ");
buf.append("当前第<font color='red'>" + currPageId
+ "</font/>页 ");
if (currPageId == 1) {
buf.append("<font color='#b0c4de'>首页</font> ");
buf.append("<font color='#b0c4de'>上一页</font> ");
} else {
buf.append("<a href='" + url
+ "&currPageId=1'>首页</a> ");
buf.append("<a href='" + url + "&currPageId="
+ (currPageId - 1) + "'>上一页</a> ");
}
if (pageCount == currPageId) {
buf.append("<font color='#b0c4de'>下一页</font> ");
buf.append("<font color='#b0c4de'>尾页</font> ");
} else {
buf.append("<a href='" + url + "&currPageId="
+ (currPageId + 1) + "'>下一页</a> ");
buf.append("<a href='" + url + "&currPageId=" + pageCount
+ "'>尾页</a> ");
}
buf
.append("跳转到<input type='text' name='pageId' id='pageId' maxlength='3' size='2' style='width:20px;'/>");
buf
.append(" <img src='/hr/images/main/go.bmp' onclick='pagination()'/> ");
buf.append("</div>");
// 生成验证函数
buf
.append("<script type='text/javascript' language='javascript'> ");
buf
.append(" function pagination(){ ");
buf.append(" var maxId = " + pageCount
+ "; ");
buf.append(" var currId = " + currPageId
+ "; ");
buf
.append(" var pageId = document.getElementById('pageId').value; ");
buf
.append(" if(!pageId.match(/^\\d+$/)){ ");
buf
.append(" alert('请您务必输入数字!'); ");
buf
.append(" }else if(pageId>maxId){ ");
buf
.append(" alert('一共只有'+maxId+'页!'); ");
buf
.append(" }else if(pageId == currId){ ");
buf
.append(" alert('您的期望页面已是当前页!'); ");
buf
.append(" }else if(pageId <1){ ");
buf
.append(" alert('您已到达首页!'); ");
buf
.append(" }else{ ");
buf.append(" location.href='" + url
+ "&currPageId='+pageId; } } ");
buf
.append("</script> ");
out.print(buf.toString());
con.clear();
} catch (Exception ex) {
ex.printStackTrace();
}
return EVAL_PAGE;
}
@Override
public void setBodyContent(BodyContent b) {
// TODO Auto-generated method stub
super.setBodyContent(b);
}
public int getPageSize() {
return pageSize;
}
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
public int getCurrPageId() {
return currPageId;
}
public void setCurrPageId(int currPageId) {
this.currPageId = currPageId;
}
public int getRecordCount() {
return recordCount;
}
public void setRecordCount(int recordCount) {
this.recordCount = recordCount;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getBgColor() {
return bgColor;
}
public void setBgColor(String bgColor) {
this.bgColor = bgColor;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {// 如果未带参数人为添加一个errorId
if (url.indexOf('?') < 0) {
url = url + "?errorId=1";
}
this.url = url;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -