package com.zking.tag;
import com.zking.util.PageBean;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
import java.io.IOException;
import java.util.Map;
import java.util.Set;
/**
* Page助手类
*
* @author tgq
*
*/
public class PageTag extends BodyTagSupport {
private static final long serialVersionUID = 1L;
private PageBean pageBean;
public PageBean getPageBean() {
return pageBean;
}
public void setPageBean(PageBean pageBean) {
this.pageBean = pageBean;
}
@Override
public int doStartTag() throws JspException {
// 获取流
JspWriter out = pageContext.getOut();
try {
out.print(toHTML());
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
/**
* JSP自定义页面内容展示
*
* @return
*/
private String toHTML() {
StringBuilder sb = new StringBuilder();
// 这里拼接的是一个上一次发送的请求以及携带的参数,唯一改变的就是页码
sb.append("<form id='pageBeanForm' action='" + pageBean.getUrl() + "' method='post'>");
sb.append("<input type='hidden' name='page'>");
// 重要设置拼接操作,将上一次请求参数携带到下一次
Map<String, String[]> paMap = pageBean.getParameterMap();
if (paMap != null && paMap.size() > 0) {
Set<Map.Entry<String, String[]>> entrySet = paMap.entrySet();
for (Map.Entry<String, String[]> entry : entrySet) {
for (String val : entry.getValue()) {
if (!"page".equals(entry.getKey())) {
sb.append("<input type='hidden' name='" + entry.getKey() + "' value='" + val + "'>");
}
}
}
}
sb.append("</form>");
/**
* 页码数据配置
*/
int page = pageBean.getPage(); // 当前页码
int max = pageBean.getMaxPage();// 最大页码
int before = page > 4 ? 4 : page - 1;// 当前页码前面的页码
int after = 10 - 1 - before;// 当前页码后面的页码
// 如果最大页码没有10页,就显示当前页面后面的所有页码 max - page
after = page + after > max ? max - page : after;
// 初始页码和结束页码值判断 disabled
boolean startFlag = page == 1;
boolean endFlag = max == page;
// 拼接分页条
sb.append("<ul class='pagination'>");
sb.append("<li class='page-item " + (startFlag ? "disabled" : "")
+ "'><a class='page-link' href='javascript:gotoPage(1)'>首页</a></li>");
sb.append("<li class='page-item " + (startFlag ? "disabled" : "")
+ "'><a class='page-link' href='javascript:gotoPage(" + pageBean.getPrevPage() + ")'><</a></li>");
// 代表了当前页的前4页
for (int i = before; i > 0; i--) {
sb.append("<li class='page-item'><a class='page-link' href='javascript:gotoPage(" + (page - i) + ")'>"
+ (page - i) + "</a></li>");
}
sb.append("<li class='page-item active'><a class='page-link' href='javascript:gotoPage(" + pageBean.getPage()
+ ")'>" + pageBean.getPage() + "</a></li>");
// 代表了当前页的后5页
for (int i = 1; i <= after; i++) {
sb.append("<li class='page-item'><a class='page-link' href='javascript:gotoPage(" + (page + i) + ")'>"
+ (page + i) + "</a></li>");
}
sb.append("<li class='page-item " + (endFlag ? "disabled" : "")
+ "'><a class='page-link' href='javascript:gotoPage(" + pageBean.getNextPage() + ")'>></a></li>");
sb.append("<li class='page-item " + (endFlag ? "disabled" : "")
+ "'><a class='page-link' href='javascript:gotoPage(" + pageBean.getMaxPage() + ")'>尾页</a></li>");
sb.append(
"<li class='page-item go-input'><b>到第</b><input class='page-link' type='text' id='skipPage' name='' /><b>页</b></li>");
sb.append("<li class='page-item go'><a class='page-link' href='javascript:skipPage()'>确定</a></li>");
sb.append("<li class='page-item'><b>共" + pageBean.getTotal() + "条</b></li>");
sb.append("</ul>");
// 拼接分页的js代码
sb.append("<script type='text/javascript'>");
sb.append("function gotoPage(page) {");
sb.append("document.getElementById('pageBeanForm').page.value = page;");
sb.append("document.getElementById('pageBeanForm').submit();");
sb.append("}");
sb.append("function skipPage() {");
sb.append("var page = document.getElementById('skipPage').value;");
sb.append("if (!page || isNaN(page) || parseInt(page) < 1 || parseInt(page) > " + max + ") {");
sb.append("alert('请输入1~N的数字');");
sb.append("return;");
sb.append("}");
sb.append("gotoPage(page);");
sb.append("}");
sb.append("</script>");
return sb.toString();
}
}