package com.startech.util;
import java.io.IOException;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 分页标签的可执行程序
*
* @author jn0906c
*
*/
public class PageTag extends TagSupport {
private static final long serialVersionUID = 1L;
private int rscount;// 总记录数
private int currentpage;// 当前页
private String action;// 提交action
private String className = BBSTEXT;// 样式
private int pagesize = Constants.PAGESIZE;// 每页记录
private int pagecount;// 总页数
/** **************样式常量******************** */
public static final String TEXT = "text";
public static final String IMAGE = "image";
public static final String BBSTEXT = "bbstext";
public static final String BBSIMAGE = "bbsimage";
public String getAction() {
return action;
}
public void setAction(String action) {
this.action = action;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
public int getCurrentpage() {
return currentpage;
}
public void setCurrentpage(int currentpage) {
this.currentpage = currentpage;
}
public int getRscount() {
return rscount;
}
public void setRscount(int rscount) {
this.rscount = rscount;
try {
this.pagecount = ((this.rscount - 1) / this.pagesize) + 1;
} catch (Exception ex) {
this.pagecount = 0;
}
}
@Override
public int doStartTag() throws JspException {
// TODO 自动生成方法存根
String pagetool = pagetool(className);
JspWriter out = pageContext.getOut();
try {
out.write(pagetool);
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
return super.doStartTag();
}
/**
* 分页工具条
*
* @param fileName
* String
* @return String
*/
public String pagetool(String flag) {
StringBuffer str = new StringBuffer();
String url = this.getParamUrl();
int ProPage = this.currentpage - 1;
int Nextpage = this.currentpage + 1;
// 文字的分页
if (flag.equals(TEXT)) {
str.append("<form method='post' name='pageform' action=''>");
str
.append("<table width='100%' border='0' cellspacing='0' cellpadding='0' class='pgToolbar'>");
str.append("<tr>");
str.append("<td width='3%'> </td>");
str.append("<td height='26'>");
str.append("共有记录" + this.rscount + "条 ");
str.append("共" + this.pagecount + "页 ");
str.append("每页" + this.pagesize + "记录 ");
str.append("现在" + this.currentpage + "/" + this.pagecount + "页");
str.append("</td><td>");
if (this.currentpage > 1) {
str.append("<a href='" + url + "&page.currentpage=1'>首页</a>");
str.append(" ");
str.append("<a href='" + url + "&page.currentpage=" + ProPage
+ "'>上一页</a>");
str.append(" ");
} else {
str.append("首页");
str.append(" ");
str.append("上一页");
str.append(" ");
}
if (this.currentpage < this.pagecount) {
str.append("<a href='" + url + "&page.currentpage=" + Nextpage
+ "'>下一页</a>");
str.append(" ");
} else {
str.append("下一页");
str.append(" ");
}
if (this.pagecount > 1 && this.currentpage != this.pagecount) {
str.append("<a href='" + url + "&page.currentpage=" + pagecount
+ "'>尾页</a>");
str.append(" ");
} else {
str.append("尾页");
str.append(" ");
}
str.append("转到");
str
.append("<select name='page.currentpage' onchange='javascript:ChangePage(this.value);'>");
for (int j = 1; j <= pagecount; j++) {
str.append("<option value='" + j + "'");
if (currentpage == j) {
str.append("selected");
}
str.append(">");
str.append("" + j + "");
str.append("</option>");
}
str.append("</select>页");
str.append("</td><td width='3%'> </td></tr></table>");
str.append("<script language='javascript'>");
str.append("function ChangePage(testpage){");
str.append("document.pageform.action='" + url
+ "&page.currentpage='+testpage+'';");
str.append("window.location=document.pageform.action;");
str.append("}");
str.append("</script>");
str.append("</form>");
} else if (flag.equals(IMAGE)) {
/**
* 图片的分页
*/
} else if (flag.equals(BBSTEXT)) {
/**
* 论坛形式的分页[直接以数字方式体现]
*/
str
.append("<table width='100%' border='0' cellspacing='0' cellpadding='0' style='font-size: 13px;'>");
str.append("<tr>");
str.append("<td width='3%'> </td>");
str.append("<td height='26'>");
str.append("记录" + this.rscount + "条 ");
str.append("共" + this.pagecount + "页 ");
str.append("每页" + this.pagesize + "记录 ");
str.append("现在" + this.currentpage + "/" + this.pagecount + "页");
str.append("</td><td>");
// 设定是否有首页的链接
if (this.currentpage > 1) {
str.append("<a href='" + url + "&page.currentpage=1'>首页</a>");
str.append(" ");
}
// 设定是否有上一页的链接
if (this.currentpage > 1) {
str.append("<a href='" + url + "&page.currentpage=" + ProPage
+ "'>上一页</a>");
str.append(" ");
}
// 如果总页数只有10的话
if (this.pagecount <= 10) {
for (int i = 1; i <= this.pagecount; i++) {
if (this.currentpage == i) {
str.append("<font color=red>[" + i
+ "]</font> ");
} else {
str.append("<a href='" + url + "&page.currentpage=" + i
+ "'>" + i + "</a> ");
}
}
} else {
// 说明总数有超过10页
// 制定特环的开始页和结束页
int endPage = this.currentpage + 4;
if (endPage > this.pagecount) {
endPage = this.pagecount;
}
int startPage = 0;
if (this.pagecount >= 8 && this.currentpage >= 8) {
startPage = this.currentpage - 5;
} else {
// 表示从第一页开始算
startPage = 1;
}
System.out.println(startPage);
System.out.println(endPage);
for (int i = startPage; i <= endPage; i++) {
if (this.currentpage == i) {
str.append("<font color=red>[" + i
+ "]</font> ");
} else {
str.append("<a href='" + url + "&page.currentpage=" + i
+ "'>" + i + "</a> ");
}
}
}
// 设定是否有下一页的链接
if (this.currentpage < this.pagecount) {
str.append("<a href='" + url + "&page.currentpage=" + Nextpage
+ "'>下一页</a>");
str.append(" ");
}
// 设定是否有尾页的链接
if (this.pagecount > 1 && this.currentpage != this.pagecount) {
str.append("<a href='" + url + "&page.currentpage=" + pagecount
+ "'>尾页</a>");
str.append(" ");
}
str.append("</td><td width='3%'> </td></tr></table>");
} else if (flag.equals(BBSIMAGE)) {
/**
* 论坛形式的分页[以图片的方式体现]
*/
// 设定分页显示的CSS
str.append("<style>");
str
.append("BODY {FONT-SIZE: 12px;FONT-FAMILY:宋体;WIDTH: 60%; PADDING-LEFT: 25px;}");
str
.append("DIV.meneame {PADDING-RIGHT: 3px; PADDING-LEFT: 3px; FONT-SIZE: 80%; PADDING-BOTTOM: 3px; MARGIN: 3px; COLOR: #ff6500; PADDING-TOP: 3px; TEXT-ALIGN: center}");
str
.append("DIV.meneame A {BORDER-RIGHT: #f