java 自定义标签
程序员文章站
2022-06-09 09:16:29
...
public class PageTag extends TagSupport {
private static final long serialVersionUID = 1L;
/* 当前页 */
private String curPage;
/* 总页数 */
private String totalPage;
/* 页大小(一页显示的大小) */
private String pageSize;
/* 页面url */
private String url;
public void setCurPage(String curPage) {
this.curPage = curPage;
}
public void setPageSize(String pageSize) {
this.pageSize = pageSize;
}
public void setTotalPage(String totalPage) {
this.totalPage = totalPage;
}
public void setUrl(String url) {
this.url = url;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
// 得到分页后的页数,(总页数/页大小)+1
if (pageSize == null || pageSize == "") {
pageSize = "1";
}
// int pageNumber = (Integer.parseInt(totalPage) / Integer
// .parseInt(pageSize));
if (Integer.parseInt(curPage) > Integer.parseInt(totalPage)) {
curPage = String.valueOf(totalPage);
}
if (Integer.parseInt(curPage) < 1) {
curPage = "1";
}
// 显示给用户操作的页面开始端
int start = Integer.parseInt(curPage) - 2;
// 显示给用户操作的页面结束端
int end = Integer.parseInt(curPage) + 2;
// 特殊情况处理(开始端小于0)
if ((Integer.parseInt(curPage) - 2) <= 0) {
start = 1;
}
// 特殊情况处理(结束端大于总页数)
if ((Integer.parseInt(curPage) + 2) > Integer.parseInt(totalPage)) {
end = Integer.parseInt(totalPage);
}
if ((end <= 5) && (Integer.parseInt(totalPage) >= 5)) {
end = 5;
}
if (((end - start) <= 5) && (Integer.parseInt(totalPage) >= 5)) {
start = end - 4;
}
try {
// 取得action的url
String actionUrl = "";
if (url.indexOf("?") != -1) {
actionUrl = url.substring(0, url.indexOf("?"));
}
StringBuilder html = new StringBuilder();
// 当前页输入框的验证
html.append("<script type=\"text/javascript\">");
html.append("$(function() {");
html.append("$(\"#pageTagForm\").validate({");
html.append("rules : {");
html.append("curpage : {");
html.append("required : true,");
html.append("digits : true");
html.append("}");
html.append("},");
html.append("messages : {");
html.append("curpage : {");
html.append("required : \"\",");
html.append("digits : \"\"");
html.append("}");
html.append("}");
html.append("});");
html.append("});");
html.append("</script>");
html.append(" <form id=\"pageTagForm\" action=\""
+ ((url.indexOf("?") == -1) ? url : actionUrl)
+ "\" method=\"get\" >");
html.append(" <table align=\"right\">");
html.append(" <tr height=\"10\" align=\"right\" >");
html.append(" <td width=\"110\" align=\"center\">");
html.append(" <a href=\"" + url
+ ((url.indexOf("?") == -1) ? "?" : "&&")
+ "curpage=1\">[首页]</a>");
html.append(" <a href=\"" + url
+ ((url.indexOf("?") == -1) ? "?" : "&&") + "curpage=");
html.append((((Integer.parseInt(curPage) - 1) == 0) ? curPage
: (Integer.parseInt(curPage) - 1)));
html.append("\">[上一页]</a></td><td width=\"110\" align=\"center\">");
for (int i = start; i <= end; i++) {
if (i != Integer.parseInt(curPage)) {
html.append("<a href=\"" + url
+ ((url.indexOf("?") == -1) ? "?" : "&&")
+ "curpage=" + i + "\">");
html.append("[" + i + "]</a> ");
} else {
html.append("<b>" + i + "</b>");
}
}
html.append(" </td><td width=\"110\" align=\"center\"><a href=\""
+ url + ((url.indexOf("?") == -1) ? "?" : "&&")
+ "curpage=");
html.append((((Integer.parseInt(curPage) + 1) > Integer
.parseInt(totalPage)) ? curPage : (Integer
.parseInt(curPage) + 1)));
html.append("\">[下一页]</a><a href=\"" + url
+ ((url.indexOf("?") == -1) ? "?" : "&&") + "curpage=");
html.append(totalPage + "\">[末页]</a>");
html.append("</td><td width=\"110\">");
html.append("<input id=\"curpage\" name=\"curpage\" style=\"width:20px;height:15px;\" />");
html.append("<input name=\"totalpage\" type=\"hidden\" value=\""
+ totalPage + "\" style=\"width:20px;height:15px;\" />");
html.append("<input name=\"id\" type=\"hidden\" value=\""
+ ((url.indexOf("?") == -1) ? 0 : (url.substring(url
.indexOf("=") + 1)))
+ "\" style=\"width:20px;height:15px;\" />");
html.append("<input type=\"submit\" value=\"go\"/></td></tr></table></form>");
out.print(html.toString());
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
}
1、在工程WEB-INF目录下创建pageTag.tld(详细查看附件)文件;
2、页面引用:
<%@ taglib prefix="page" uri="/WEB-INF/pageTag.tld"%>
<page:outpage url="" pageSize=""
totalPage="" curPage=""></page:outpage>
下一篇: mysql手记_MySQL