1 14 15 package com.jdon.strutsutil.taglib; 16 17 import java.io.IOException ; 18 import javax.servlet.http.HttpServletResponse ; 19 import javax.servlet.jsp.JspException ; 20 import javax.servlet.jsp.JspWriter ; 21 import javax.servlet.jsp.tagext.TagSupport ; 22 23 public class IndexTag extends TagSupport { 24 25 private boolean disp = false; 26 27 private String displayCount; 28 29 public int doStartTag() throws JspException { 30 31 String dispStrs = (String ) pageContext.getAttribute(MPageTag.DISP); 32 if ((dispStrs != null) && (!dispStrs.equals(""))) { 33 if (dispStrs.equals("on")) 34 disp = true; 35 else if (dispStrs.equals("off")) 36 disp = false; 37 } 38 39 String startStrs = (String ) pageContext.getAttribute(MPageTag.START); 40 int start = Integer.parseInt(startStrs); 41 String url = (String ) pageContext.getAttribute(MPageTag.URLNAME); 42 String countStrs = (String ) pageContext.getAttribute(MPageTag.COUNT); 43 int count = Integer.parseInt(countStrs); 44 45 String allCountStrs = (String ) pageContext.getAttribute(MPageTag.ALLCOUNT); 46 int allCount = Integer.parseInt(allCountStrs); 47 48 StringBuffer buf = new StringBuffer (100); 49 50 int numPages = 0; 51 if (allCount != count) { 52 numPages = (int) Math.ceil((double) allCount / (double) count); 53 } else { 54 numPages = 1; 55 } 56 57 int currentPage = 1; 61 if (count > 0) { 62 currentPage = (start / count) + 1; 63 } 64 65 if ((displayCount == null) || (displayCount.length() == 0)) 66 this.displayCount = "5"; int dispCount = Integer.parseInt(displayCount); 68 int lo = currentPage - dispCount; 69 if (lo <= 0) { 70 lo = 1; 71 } 72 int hi = currentPage + dispCount; 73 74 if (lo > 2) { 76 buf.append("<a HREF=\"").append(url); 77 buf.append("\" class=\"paginator_href\" title=\"Go to the first page\">1</a> ... "); 78 } 79 80 while (lo < currentPage) { 82 buf.append("<a HREF=\"").append(url); 83 buf.append("&start="); 84 buf.append(((lo - 1) * count)); 85 buf.append("\" class=\"paginator_href\">"); 86 buf.append("<b>"); 87 buf.append(lo); 88 buf.append("</b></a> "); 89 lo++; 90 } 91 92 buf.append("<b><span class=\"paginator_currentPage\">"); 94 buf.append(currentPage); 95 buf.append("</span></b>"); 96 97 currentPage++; 98 99 while ((currentPage <= hi) && (currentPage <= numPages)) { 101 buf.append(" "); 102 buf.append("<a HREF=\"").append(url); 103 buf.append("&start="); 104 buf.append(((currentPage - 1) * count)); 105 buf.append("\" class=\"paginator_href\">"); 106 buf.append("<b>"); 107 buf.append(currentPage); 108 buf.append("</b></a>"); 109 currentPage++; 110 } 111 112 if (currentPage <= numPages){ 113 buf.append(" ... "); 114 buf.append("<a HREF=\"").append(url); 115 buf.append("&start="); 116 buf.append(((numPages - 1) * count)); 117 buf.append("\" class=\"paginator_href\" title=\"Go to the last page\">"); 118 buf.append(numPages); 119 } 120 121 HttpServletResponse response = (HttpServletResponse ) pageContext.getResponse(); 122 JspWriter writer = pageContext.getOut(); 123 try { 124 if (disp) 125 writer.print(buf.toString()); 126 } catch (IOException e) { 127 throw new JspException ("PrevTag error"); 128 } 129 130 return (EVAL_BODY_INCLUDE); 131 132 } 133 134 140 public int doEndTag() throws JspException { 141 142 return (EVAL_PAGE); 143 144 } 145 146 149 public String getDisplayCount() { 150 return displayCount; 151 } 152 153 156 public void setDisplayCount(String displayCount) { 157 this.displayCount = displayCount; 158 } 159 } 160 | Popular Tags |