Java tutorial
/* * Copyright 2009 the original author or authors. * * Licensed under the fit License, Version 1.0 (the "License"); */ package com.wlt.webm.util; import java.io.IOException; import javax.servlet.http.HttpServletRequest; import javax.servlet.jsp.JspTagException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.BodyTagSupport; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; /** * @description: * @author:King 2009/07/28 * @version:1.0 * @modify: */ public class PageTag extends BodyTagSupport { private static final long serialVersionUID = -6250075601936086320L; // private Log log = LogFactory.getLog(this.getClass()); //url private String url; //pagepagePageAttributerequest private PageAttribute page; //,(css,) private String style; public void setUrl(String url) { this.url = url; } public void setPage(PageAttribute page) { this.page = page; } public void setStyle(String style) { this.style = style; } public int doStartTag() throws JspTagException { return EVAL_BODY_BUFFERED; } public int doEndTag() throws JspTagException { //style,() StringBuffer buf = new StringBuffer(); if ("green".equals(style)) { // getGreenPageinfo(buf); } else if ("blue".equals(style)) { // getBluePageinfo(buf); } else if ("red".equals(style)) { getRedPageinfo(buf); } else if ("black".equals(style)) { //getBlackPageinfo(buf); getBluePageinfo(buf); } JspWriter out = pageContext.getOut(); try { out.print(buf.toString()); } catch (IOException e) { log.error("" + e.getMessage()); e.printStackTrace(); } return SKIP_BODY; } /** * * ,, * @param buf */ public void getGreenPageinfo(StringBuffer buf) { try { //request HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); // int curPage = page.getCurPage(); String path = request.getContextPath(); // int pageCountView = 10; //url? if (url.indexOf("?") != -1) { if (null != page.getParams() && !"".equals(page.getParams())) { url = path + "/" + url + page.getParams() + "&curPage="; } else { url = path + "/" + url + "&curPage="; } } else { if (null != page.getParams() && !"".equals(page.getParams())) { url = path + "/" + url + "?" + page.getParams() + "curPage="; } else { url = path + "/" + url + "?" + "curPage="; } } buf.append("<div class=\"page_body\">\n"); if (curPage == 1) { buf.append("<a href=\"javascript:void(0)\" class=\"page_home\" title=\"\"></a>\n"); buf.append("<a href=\"javascript:void(0)\" class=\"page_forward\" title=\"\"></a>\n"); } else { buf.append("<a name=\"pageHref\" href=\"" + url + "1&" + "\" class=\"page_home\" title=\"\"></a>\n"); buf.append("<a name=\"pageHref\" href=\"" + url + page.getProPage() + "\" class=\"page_forward\" title=\"\"></a>\n"); } if (curPage > pageCountView && curPage != 1) { //curPagepageCountView,curPage!=1 buf.append("<a name=\"pageHref\" href=\"" + url + (curPage - pageCountView) + "\" title=\"\">...</a>\n"); } // int start = (curPage / pageCountView) * pageCountView; if (curPage % pageCountView == 0) { start = (curPage / pageCountView - 1) * pageCountView; } else { start = (curPage / pageCountView) * pageCountView; } // int end = 0; if (page.getPageCount() - start < 10) { end = page.getPageCount(); } else { end = start + pageCountView; } for (int i = start + 1; i <= end; i++) { if (curPage == i) { buf.append("<span class=\"on\" style=\"font-weight: bold\">" + i + "</span>\n"); } else { buf.append("<a name=\"pageHref\" href=\"" + url + i + "\">" + i + "</a>\n"); } } if (page.getPageCount() - curPage > 10) { //-pageCountView buf.append("<a name=\"pageHref\" href=\"" + url + (curPage + pageCountView) + "\" title=\"\">...</a>\n"); } if (curPage == page.getPageCount()) { buf.append("<a href=\"javascript:void(0);\" class=\"page_behind\" title=\"\"></a>\n"); buf.append("<a href=\"javascript:void(0);\" class=\"page_end\" title=\"\"></a>\n"); } else { buf.append("<a name=\"pageHref\" href=\"" + url + page.getNextPage() + "\" class=\"page_behind\" title=\"\"></a>\n"); buf.append("<a name=\"pageHref\" href=\"" + url + page.getPageCount() + "\" class=\"page_end\" title=\"\"></a>\n"); } buf.append("<span> :</span>\n"); buf.append( "<input type=\"text\" size=\"5\" name=\"skipCurPage\" id=\"skipCurPage\" class=\"input\" onkeyup=\"this.value=this.value.replace(/[^\\d]/g,'')\"/><label style=\"display:none\">" + url + "</label>\n"); buf.append("<span></span>\n"); buf.append( "<input type=\"button\" name=\"skipBtn\" id=\"skipBtn\" value=\"GO\" class=\"button_c\" /> " + page.getPageSize() + " " + page.getRsCount() + " " + page.getCurPage() + "/" + page.getPageCount() + " \n"); buf.append("</div>"); } catch (Exception e) { log.error("PageTag.getGreenPageinfo:" + e.getMessage()); e.printStackTrace(); } } /** * * ,, * @param buf */ public void getBlackPageinfo(StringBuffer buf) { try { //request HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); String path = request.getContextPath(); // int curPage = page.getCurPage(); // int pageCountView = 5; //url? if (url.indexOf("?") != -1) { if (null != page.getParams() && !"".equals(page.getParams())) { url = path + "/" + url + page.getParams() + "&curPage="; } else { url = path + "/" + url + "&curPage="; } } else { if (null != page.getParams() && !"".equals(page.getParams())) { url = path + "/" + url + "?" + page.getParams() + "curPage="; } else { url = path + "/" + url + "?curPage="; } } if (curPage == 1) { //buf.append("<a href=\"javascript:void(0)\" title=\"\"></a>\n"); //buf.append("<a href=\"javascript:void(0)\" title=\"\"></a>\n"); //buf.append("<li><a class='hover' href=\"" + url + curPage +"\">1</a></li>"); } else { //buf.append("<a href=\"" + url + "1&"+"\" title=\"\"></a>\n"); buf.append("<a name=\"pageHref\" href=\"" + url + page.getProPage() + "\" ></a>"); } if (curPage > pageCountView && curPage != 1) { //curPagepageCountView,curPage!=1 buf.append("<a name=\"pageHref\" href=\"" + url + (curPage - 5) + "\" title=\"\">...</a>"); } // int start = (curPage / pageCountView) * pageCountView; if (curPage % pageCountView == 0) { start = (curPage / pageCountView - 1) * pageCountView; } else { start = (curPage / pageCountView) * pageCountView; } // int end = 0; if (page.getPageCount() - start < 5) { end = page.getPageCount(); } else { end = start + pageCountView; } for (int i = start + 1; i <= end; i++) { if (curPage == i) { buf.append("<a class='hover' name=\"pageHref\" href=\"" + url + i + "\">" + i + "</a>"); } else { buf.append("<a name=\"pageHref\" href=\"" + url + i + "\">" + i + "</a>"); } } if (curPage % 5 == 0) { //-pageCountView buf.append("<a name=\"pageHref\" href=\"" + url + (curPage + pageCountView) + "\" title=\"\">...</a>"); } else if ((curPage == (page.getPageCount() - 5)) || (curPage == (page.getPageCount() - 4)) || (curPage == (page.getPageCount() - 6)) || (curPage == (page.getPageCount() - 7))) { buf.append("<a name=\"pageHref\" href=\"" + url + (curPage + pageCountView) + "\" title=\"\">...</a>"); } else if (curPage < page.getPageCount() && curPage < ((page.getPageCount() % 5) * 5) + 1) { buf.append("<a name=\"pageHref\" href=\"" + url + (curPage + pageCountView) + "\" title=\"\">...</a>"); } if (curPage == page.getPageCount()) { buf.append("<a name=\"pageHref\" href=\"javascript:void(0);\" title=\"\"></a>"); //buf.append("<li><a href=\"javascript:void(0);\" title=\"\"></a></li>"); } else { buf.append("<a name=\"pageHref\" href=\"" + url + page.getNextPage() + "\" title=\"\"></a>"); //buf.append("<li><a href=\"" + url + page.getPageCount()+ "\" title=\"\"></a></li>"); } } catch (Exception e) { log.error("PageTag.getBlackPageinfo:" + e.getMessage()); e.printStackTrace(); } } // public void getRedPageinfo(StringBuffer buf) { try { //request HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); // int curPage = page.getCurPage(); String path = request.getContextPath(); // int pageCountView = 6; //url? if (url.indexOf("?") != -1) { if (null != page.getParams() && !"".equals(page.getParams())) { url = path + "/" + url + page.getParams() + "&curPage="; } else { url = path + "/" + url + "&curPage="; } } else { if (null != page.getParams() && !"".equals(page.getParams())) { url = path + "/" + url + "?" + page.getParams() + "curPage="; } else { url = path + "/" + url + "?" + "curPage="; } } buf.append("<div class=\"page_body\">\n"); if (curPage == 1) { buf.append("<a href=\"javascript:void(0)\" class=\"page_home\" title=\"\"></a>\n"); buf.append("<a href=\"javascript:void(0)\" class=\"page_forward\" title=\"\"></a>\n"); } else { buf.append("<a name=\"pageHref\" href=\"" + url + "1&" + "\" class=\"page_home\" title=\"\"></a>\n"); buf.append("<a name=\"pageHref\" href=\"" + url + page.getProPage() + "\" class=\"page_forward\" title=\"\"></a>\n"); } if (curPage > pageCountView && curPage != 1) { //curPagepageCountView,curPage!=1 buf.append("<a name=\"pageHref\" href=\"" + url + (curPage - pageCountView) + "\" title=\"\">...</a>\n"); } // int start = (curPage / pageCountView) * pageCountView; if (curPage % pageCountView == 0) { start = (curPage / pageCountView - 1) * pageCountView; } else { start = (curPage / pageCountView) * pageCountView; } // int end = 0; if (page.getPageCount() - start < 6) { end = page.getPageCount(); } else { end = start + pageCountView; } for (int i = start + 1; i <= end; i++) { if (curPage == i) { buf.append("<span class=\"on\">" + i + "</span>\n"); } else { buf.append("<a name=\"pageHref\" href=\"" + url + i + "\">" + i + "</a>\n"); } } if (page.getPageCount() - curPage > 6) { //-pageCountView buf.append("<a name=\"pageHref\" href=\"" + url + (curPage + pageCountView) + "\" title=\"\">...</a>\n"); } if (curPage == page.getPageCount()) { buf.append("<a href=\"javascript:void(0);\" class=\"page_behind\" title=\"\"></a>\n"); buf.append("<a href=\"javascript:void(0);\" class=\"page_end\" title=\"\"></a>\n"); } else { buf.append("<a name=\"pageHref\" href=\"" + url + page.getNextPage() + "\" class=\"page_behind\" title=\"\"></a>\n"); buf.append("<a name=\"pageHref\" href=\"" + url + page.getPageCount() + "\" class=\"page_end\" title=\"\"></a>\n"); } buf.append(" " + page.getRsCount() + " " + page.getCurPage() + "/" + page.getPageCount() + " \n"); buf.append("</div>"); } catch (Exception e) { log.error("PageTag.getGreenPageinfo:" + e.getMessage()); e.printStackTrace(); } } /** * * ,, * @param buf */ public void getBluePageinfo(StringBuffer buf) { //request HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); try { // int curPage = page.getCurPage(); String path = request.getContextPath(); // int pageCountView = 5; //url? if (url.indexOf("?") != -1) { url = path + "/" + url + "&curPage="; } else { url = path + "/" + url + "?curPage="; } buf.append("<div class=\"page_body\">\n"); if (curPage == 1) { //buf.append("<a href=\"javascript:void(0);\" class=\"page_home\" title=\"\"></a>\n"); buf.append("<a href=\"javascript:void(0);\" class=\"page_forward\" title=\"\"></a>\n"); } else { //buf.append("<a name=\"pageHref\" href=\"" + url + "1\" class=\"page_home\" title=\"\"></a>\n"); buf.append("<a name=\"pageHref\" href=\"" + url + page.getProPage() + "\" class=\"page_forward\" title=\"\"></a>\n"); } if (curPage > pageCountView) { //curPagepageCountView //buf.append("<a name=\"pageHref\" href=\"" + url + (curPage-pageCountView) + "\" title=\"\"></a>\n"); } // int start = (curPage / pageCountView) * pageCountView; if (curPage % pageCountView == 0) { start = (curPage / pageCountView - 1) * pageCountView; } else { start = (curPage / pageCountView) * pageCountView; } // int end = 0; if (page.getPageCount() - start < pageCountView) { end = page.getPageCount(); } else { end = start + pageCountView; } buf.append("<script>" + "function changePage(url){" + "var num=document.getElementById('curPage').value;" + "location.href=url+num}" + "</script>"); for (int i = start + 1; i <= end; i++) { if (curPage == i) { buf.append("<span class=\"on\">" + i + "</span>\n"); } else { buf.append("<a name=\"pageHref\" href=\"" + url + i + "\">" + i + "</a>\n"); } } if (page.getPageCount() - curPage > 0 && (page.getPageCount() / pageCountView != curPage / pageCountView || curPage % pageCountView == 0)) { //-pageCountView //buf.append("<a name=\"pageHref\" href=\"" + url + (curPage + pageCountView) + "\" title=\"\"></a>\n"); } if (curPage == page.getPageCount()) { buf.append("<a href=\"javascript:void(0);\" class=\"page_behind\" title=\"\"></a>\n"); //buf.append("<a href=\"javascript:void(0);\" class=\"page_end\" title=\"\"></a>\n"); } else { buf.append("<a name=\"pageHref\" href=\"" + url + page.getNextPage() + "\" class=\"page_behind\" title=\"\"></a>\n"); //buf.append("<a name=\"pageHref\" href=\"" + url + page.getPageCount() + "\" class=\"page_end\" title=\"\"></a>\n"); } buf.append("<span> :</span>\n"); buf.append( "<input type=\"text\" name=\"curPage\" size=\"4\" id=\"curPage\" class=\"input\" onkeyup=\"this.value=this.value.replace(/[^\\d]/g,'')\"/><label style=\"display:none\">" + url + "</label>\n"); buf.append("<span></span>\n"); buf.append( "<input type=\"button\" name=\"skipBtn\" id=\"skipBtn\" value=\"GO\" class=\"page_button\" onclick=\"changePage('" + url + "')\" /> " + page.getPageSize() + " " + page.getRsCount() + " ," + page.getCurPage() + "/" + page.getPageCount() + " \n"); buf.append("</div>"); } catch (Exception e) { log.error("PageTag.getBluePageinfo:" + e.getMessage()); e.printStackTrace(); } } /** * (ajax) * ,, * @param page */ public String getBluePageinfoAjax(int curPage, int pageSize, int rsCount) { StringBuilder buf = new StringBuilder(); try { int pageCountView = 5; // PageAttribute page = new PageAttribute(curPage, pageSize); page.setRsCount(rsCount); curPage = page.getCurPage(); buf.append("<div class=\"page_body\">\n"); if (curPage == 1) { buf.append( "<a curPage=\"1\" href=\"javascript:void(0);\" class=\"page_home\" title=\"\"></a>\n"); buf.append("<a curPage=\"" + page.getProPage() + "\" href=\"javascript:void(0);\" class=\"page_forward\" title=\"\"></a>\n"); } else { buf.append( "<a name=\"pageHref\" curPage=\"1\" href=\"1\" class=\"page_home\" title=\"\"></a>\n"); buf.append("<a name=\"pageHref\" curPage=\"" + page.getProPage() + "\" href=\"" + page.getProPage() + "\" class=\"page_forward\" title=\"\"></a>\n"); } if (curPage > pageCountView) { //curPagepageCountView buf.append("<a name=\"pageHref\" curPage=\"" + (curPage - pageCountView) + "\" href=\"" + (curPage - pageCountView) + "\" title=\"\"></a>\n"); } // int start = (curPage / pageCountView) * pageCountView; if (curPage % pageCountView == 0) { start = (curPage / pageCountView - 1) * pageCountView; } else { start = (curPage / pageCountView) * pageCountView; } // int end = 0; if (page.getPageCount() - start < pageCountView) { end = page.getPageCount(); } else { end = start + pageCountView; } for (int i = start + 1; i <= end; i++) { if (curPage == i) { buf.append("<span class=\"on\">" + i + "</span>\n"); } else { buf.append("<a name=\"pageHref\" curPage=\"" + i + "\" href=\"" + i + "\">" + i + "</a>\n"); } } if (page.getPageCount() - curPage > 0 && (page.getPageCount() / pageCountView != curPage / pageCountView || curPage % pageCountView == 0)) { //-pageCountView buf.append("<a name=\"pageHref\" curPage=\"" + (curPage + pageCountView) + "\" href=\"" + (curPage + pageCountView) + "\" title=\"\"></a>\n"); } if (curPage == page.getPageCount()) { buf.append("<a curPage=\"" + page.getNextPage() + "\" href=\"javascript:void(0);\" class=\"page_behind\" title=\"\"></a>\n"); buf.append("<a curPage=\"" + page.getPageCount() + "\" href=\"javascript:void(0);\" class=\"page_end\" title=\"\"></a>\n"); } else { buf.append("<a name=\"pageHref\" curPage=\"" + page.getNextPage() + "\" href=\"" + page.getNextPage() + "\" class=\"page_behind\" title=\"\"></a>\n"); buf.append("<a name=\"pageHref\" curPage=\"" + page.getPageCount() + "\" href=\"" + page.getPageCount() + "\" class=\"page_end\" title=\"\"></a>\n"); } buf.append("<span> :</span>\n"); buf.append( "<input type=\"text\" name=\"curPage\" size=\"5\" id=\"curPage\" class=\"input\" onkeyup=\"this.value=this.value.replace(/[^\\d]/g,'')\"/><label style=\"display:none\">url</label>\n"); buf.append("<span></span>\n"); buf.append( "<input type=\"button\" name=\"skipBtn\" id=\"skipBtn\" value=\"GO\" class=\"page_button\" /> " + page.getPageSize() + " " + page.getRsCount() + " " + page.getCurPage() + "/" + page.getPageCount() + " \n"); buf.append("</div>"); } catch (Exception e) { log.error("PageTag.getBluePageinfoAjax:" + e.getMessage()); e.printStackTrace(); } return buf.toString(); } /** * (ajax) * ,, * @param page */ public String getBluePageinfoAjax(int curPage, int pageSize, int rsCount, String pageType) { StringBuilder buf = new StringBuilder(); try { int pageCountView = 10; // PageAttribute page = new PageAttribute(curPage, pageSize); page.setRsCount(rsCount); curPage = page.getCurPage(); buf.append("<div class=\"page_body\">\n"); if (curPage == 1) { buf.append( "<a curPage=\"1\" href=\"javascript:void(0);\" class=\"page_home\" title=\"\"></a>\n"); buf.append("<a curPage=\"" + page.getProPage() + "\" href=\"javascript:void(0);\" class=\"page_forward\" title=\"\"></a>\n"); } else { buf.append("<a name=\"page" + pageType + "Href\" curPage=\"1\" href=\"1\" class=\"page_home\" title=\"\"></a>\n"); buf.append("<a name=\"page" + pageType + "Href\" curPage=\"" + page.getProPage() + "\" href=\"" + page.getProPage() + "\" class=\"page_forward\" title=\"\"></a>\n"); } if (curPage > pageCountView) { //curPagepageCountView buf.append("<a name=\"page" + pageType + "Href\" curPage=\"" + (curPage - pageCountView) + "\" href=\"" + (curPage - pageCountView) + "\" title=\"\">...</a>\n"); } // int start = (curPage / pageCountView) * pageCountView; if (curPage % pageCountView == 0) { start = (curPage / pageCountView - 1) * pageCountView; } else { start = (curPage / pageCountView) * pageCountView; } // int end = 0; if (page.getPageCount() - start < pageCountView) { end = page.getPageCount(); } else { end = start + pageCountView; } for (int i = start + 1; i <= end; i++) { if (curPage == i) { buf.append("<span class=\"on\">" + i + "</span>\n"); } else { buf.append("<a name=\"page" + pageType + "Href\" curPage=\"" + i + "\" href=\"" + i + "\">" + i + "</a>\n"); } } if (page.getPageCount() - curPage > 0 && (page.getPageCount() / pageCountView != curPage / pageCountView || curPage % pageCountView == 0)) { //-pageCountView buf.append("<a name=\"page" + pageType + "Href\" curPage=\"" + (curPage + pageCountView) + "\" href=\"" + (curPage + pageCountView) + "\" title=\"\">...</a>\n"); } if (curPage == page.getPageCount()) { buf.append("<a curPage=\"" + page.getNextPage() + "\" href=\"javascript:void(0);\" class=\"page_behind\" title=\"\"></a>\n"); buf.append("<a curPage=\"" + page.getPageCount() + "\" href=\"javascript:void(0);\" class=\"page_end\" title=\"\"></a>\n"); } else { buf.append("<a name=\"page" + pageType + "Href\" curPage=\"" + page.getNextPage() + "\" href=\"" + page.getNextPage() + "\" class=\"page_behind\" title=\"\"></a>\n"); buf.append("<a name=\"page" + pageType + "Href\" curPage=\"" + page.getPageCount() + "\" href=\"" + page.getPageCount() + "\" class=\"page_end\" title=\"\"></a>\n"); } buf.append("<span> :</span>\n"); buf.append("<input type=\"text\" size=\"5\" name=\"cur" + pageType + "Page\" id=\"cur" + pageType + "Page\" class=\"input\" onkeyup=\"this.value=this.value.replace(/[^\\d]/g,'')\"/><label style=\"display:none\">url</label>\n"); buf.append("<span></span>\n"); buf.append("<input type=\"button\" name=\"skip" + pageType + "Btn\" id=\"skip" + pageType + "Btn\" value=\"GO\" class=\"page_button\" /> " + page.getPageSize() + " " + page.getRsCount() + " " + page.getCurPage() + "/" + page.getPageCount() + " \n"); buf.append("</div>"); } catch (Exception e) { log.error("PageTag.getBluePageinfoAjax:" + e.getMessage()); e.printStackTrace(); } return buf.toString(); } }