1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package net.smartlab.web.page;
24
25 import java.io.IOException;
26 import java.util.Collection;
27 import javax.servlet.http.HttpServletRequest;
28 import javax.servlet.jsp.JspException;
29 import javax.servlet.jsp.JspTagException;
30 import javax.servlet.jsp.PageContext;
31 import org.apache.struts.taglib.TagUtils;
32
33
34
35
36
37
38
39
40
41 public class PaginateTag extends AbstractTag {
42
43
44
45
46
47 public final static String PARAMETER = "net.smartlab.web.page";
48
49
50
51
52
53
54 private Paginator paginator = null;
55
56
57
58
59 private String name = null;
60
61
62
63
64 private String property = null;
65
66
67
68
69 private String parameter;
70
71
72
73
74 private String href = "";
75
76
77
78
79 private String form = null;
80
81
82
83
84 private int size = Paginator.UNLIMITED_ITEMS;
85
86
87
88
89 private int pages = Paginator.UNLIMITED_PAGES;
90
91
92
93
94 private boolean filter = false;
95
96
97
98
99
100 public int doStartTag() throws JspException {
101 this.parameter = PARAMETER + '.' + name;
102 if (property != null) {
103 this.parameter += '.' + property;
104 }
105 String index = ((HttpServletRequest)context.getRequest()).getParameter(parameter);
106 Object attribute = TagUtils.getInstance().lookup(context, name, property, "request");
107 if (attribute instanceof Paginator) {
108 this.paginator = (Paginator)attribute;
109 } else if (attribute instanceof Collection) {
110 this.paginator = new CollectionPaginator((Collection)attribute);
111 context.setAttribute(name, this.paginator, PageContext.PAGE_SCOPE);
112 }
113 context.setAttribute("paginator", this.paginator, PageContext.PAGE_SCOPE);
114 if (size == Paginator.UNLIMITED_ITEMS) {
115 paginator.setPageSize(paginator.getCount());
116 } else {
117 paginator.setPageSize(size);
118 }
119 paginator.setPages(pages);
120 if (index != null && index.length() > 0) {
121 paginator.setPage(Integer.parseInt(index));
122 } else {
123 paginator.setPage(1);
124 }
125 if (form != null) {
126 try {
127 context.getOut().println("<input type='hidden' name='" + parameter + "'/>");
128 context.getOut().println("<script type=\"text/javascript\">");
129 context.getOut().println("//<![CDATA[");
130 context.getOut().println(" function paginate(page) {");
131 context.getOut().println(
132 " document.forms['" + form + "'].elements['" + parameter + "'].value = page;");
133 context.getOut().println(" document.forms['" + form + "'].action = '';");
134 context.getOut().println(" document.forms['" + form + "'].submit();");
135 context.getOut().println(" }");
136 context.getOut().println("//]]>");
137 context.getOut().println("</script>");
138 } catch (IOException ioe) {
139 throw new JspTagException(ioe);
140 }
141 }
142 return EVAL_BODY_INCLUDE;
143 }
144
145
146
147
148
149
150
151
152
153 public void setName(String name) {
154 this.name = name;
155 }
156
157
158
159
160
161
162
163
164
165
166
167 public void setProperty(String property) {
168 this.property = property;
169 }
170
171
172
173
174
175
176 protected String getHref() {
177 if (form == null) {
178 String query = ((HttpServletRequest)context.getRequest()).getQueryString();
179 if (query != null) {
180 int start = query.indexOf(parameter + '=');
181 int end = query.length();
182 if (start > -1) {
183 end = query.indexOf("&", start);
184 if (end > -1) {
185 query = query.substring(0, start) + query.substring(end + 1) + '&' + parameter + '=';
186 } else {
187 query = query.substring(0, start + parameter.length() + 1);
188 }
189 } else {
190 query = query + '&' + parameter + '=';
191 }
192 } else {
193 query = parameter + '=';
194 }
195 if (filter) {
196 return TagUtils.getInstance().filter(href + '?' + query);
197 } else {
198 return href + '?' + query;
199 }
200 } else {
201 return href.substring(1);
202 }
203 }
204
205
206
207
208
209
210
211
212
213
214
215 public void setHref(String href) {
216 this.href = href;
217 }
218
219
220
221
222
223
224
225
226
227 public void setForm(String form) {
228 this.form = form;
229 }
230
231
232
233
234
235
236
237
238 public void setSize(int size) {
239 this.size = size;
240 }
241
242
243
244
245
246
247
248
249 public void setPages(int pages) {
250 this.pages = pages;
251 }
252
253
254
255
256 protected Paginator getPaginator() {
257 return paginator;
258 }
259
260
261
262
263
264
265
266
267
268 protected boolean isForm() {
269 return form != null;
270 }
271
272
273
274
275
276
277
278
279
280 public void setFilter(boolean filter) {
281 this.filter = filter;
282 }
283 }