KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jdon > strutsutil > taglib > IndexTag


1 /**
2  * Copyright 2003-2006 the original author or authors. Licensed under the Apache
3  * License, Version 2.0 (the "License"); you may not use this file except in
4  * compliance with the License. You may obtain a copy of the License at
5  *
6  * http://www.apache.org/licenses/LICENSE-2.0
7  *
8  * Unless required by applicable law or agreed to in writing, software
9  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
10  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
11  * License for the specific language governing permissions and limitations under
12  * the License.
13  */

14
15 package com.jdon.strutsutil.taglib;
16
17 import java.io.IOException JavaDoc;
18 import javax.servlet.http.HttpServletResponse JavaDoc;
19 import javax.servlet.jsp.JspException JavaDoc;
20 import javax.servlet.jsp.JspWriter JavaDoc;
21 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
22
23 public class IndexTag extends TagSupport JavaDoc {
24
25     private boolean disp = false;
26
27     private String JavaDoc displayCount;
28
29     public int doStartTag() throws JspException JavaDoc {
30
31         String JavaDoc dispStrs = (String JavaDoc) 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 JavaDoc startStrs = (String JavaDoc) pageContext.getAttribute(MPageTag.START);
40         int start = Integer.parseInt(startStrs);
41         String JavaDoc url = (String JavaDoc) pageContext.getAttribute(MPageTag.URLNAME);
42         String JavaDoc countStrs = (String JavaDoc) pageContext.getAttribute(MPageTag.COUNT);
43         int count = Integer.parseInt(countStrs);
44
45         String JavaDoc allCountStrs = (String JavaDoc) pageContext.getAttribute(MPageTag.ALLCOUNT);
46         int allCount = Integer.parseInt(allCountStrs);
47
48         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(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         // Calculate the starting point & end points (the count of pages to
58
// display)
59
// 5表示,��显示数字5个
60
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"; //default 5
67
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         // print out a link to the first page if we're beyond that page
75
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         // Print the page numbers before the current page
81
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>&nbsp;");
89             lo++;
90         }
91
92         // Print the current page
93
buf.append("<b><span class=\"paginator_currentPage\">");
94         buf.append(currentPage);
95         buf.append("</span></b>");
96
97         currentPage++;
98
99         // Print page numbers after the current page
100
while ((currentPage <= hi) && (currentPage <= numPages)) {
101             buf.append("&nbsp;");
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 JavaDoc response = (HttpServletResponse JavaDoc) pageContext.getResponse();
122         JspWriter JavaDoc writer = pageContext.getOut();
123         try {
124             if (disp)
125                 writer.print(buf.toString());
126         } catch (IOException JavaDoc e) {
127             throw new JspException JavaDoc("PrevTag error");
128         }
129
130         return (EVAL_BODY_INCLUDE);
131
132     }
133
134     /**
135      * Render the end of the hyperlink.
136      *
137      * @exception JspException
138      * if a JSP exception has occurred
139      */

140     public int doEndTag() throws JspException JavaDoc {
141
142         return (EVAL_PAGE);
143
144     }
145
146     /**
147      * @return Returns the displayCount.
148      */

149     public String JavaDoc getDisplayCount() {
150         return displayCount;
151     }
152
153     /**
154      * @param displayCount The displayCount to set.
155      */

156     public void setDisplayCount(String JavaDoc displayCount) {
157         this.displayCount = displayCount;
158     }
159 }
160
Popular Tags