??1
package
?com.stt.doss.common.util;
??2
??3
import
?java.io.IOException;
??4
import
?java.util.ArrayList;
??5
import
?java.util.Collection;
??6
import
?java.util.Enumeration;
??7
??8
import
?javax.servlet.http.HttpServletRequest;
??9
import
?javax.servlet.jsp.JspException;
?10
import
?javax.servlet.jsp.JspWriter;
?11
import
?javax.servlet.jsp.PageContext;
?12
?13
import
?org.apache.log4j.Logger;
?14
?15
/**?*/
/**
?16
?*?<p>
?17
?*?Title:
?18
?*?</p>
?19
?*?<p>
?20
?*?Description:實現前臺分頁
?21
?*?</p>
?22
?*?<p>
?23
?*?Copyright:?Copyright?(c)?2006
?24
?*?</p>
?25
?*?<p>
?26
?*?Company:?stt
?27
?*?</p>
?28
?*?
?29
?*?
@author
?william
?30
?*?
@version
?1.0
?31
?
*/
?32
public
?
class
?FrontSplitPageUtil?
{
?33
?34
????
private
?
static
?Logger?log?
=
?Logger.getLogger(FrontSplitPageUtil.
class
);
?35
?36
????
private
?
static
?String?PAGING_URL?
=
?
"
PAGING_URL
"
;
?37
?38
????
public
?
final
?
static
?
int
?getCurrentPage(PageContext?pageContext,
?39
????????????String?collectionName,?
int
?maxResults)?
throws
?IOException,
?40
????????????JspException?
{
?41
????????HttpServletRequest?request?
=
?(HttpServletRequest)?pageContext
?42
????????????????.getRequest();
?43
????????Collection?collList?
=
?
new
?ArrayList();
?44
????????
if
?(request.getAttribute(collectionName)?
!=
?
null
)?
{
?45
????????????
//
?get?collection?data?by?request
?46
????????????collList?
=
?(Collection)?request.getAttribute(collectionName);
?47
????????}
?
else
?
if
?(request.getSession().getAttribute(collectionName)?
!=
?
null
)?
{
?48
????????????
//
?get?collection?data?by?request
?49
????????????collList?
=
?(Collection)?request.getSession().getAttribute(
?50
????????????????????collectionName);
?51
????????}
?52
?53
????????request.getSession().setAttribute(collectionName,?collList);
?54
?55
????????
int
?currentPage?
=
?
1
;
?56
?57
????????
int
?count?
=
?collList.size();
?58
????????
int
?pageCount?
=
?(count?
%
?maxResults?
==
?
0
)?
?
?count?
/
?maxResults?:?(count
?59
????????????????
/
?maxResults?
+
?
1
);
?60
????????
if
(pageCount
==
0
)
{
?61
????????????pageCount
=
1
;
?62
????????}
?63
????????
if
?(request.getParameter(
"
currentPage
"
)?
!=
?
null
)?
{
?64
????????????currentPage?
=
?Integer.valueOf(request.getParameter(
"
currentPage
"
))
?65
????????????????????.intValue();
?66
????????????
if
?(currentPage?
>
?pageCount)?
{
?67
????????????????currentPage?
=
?pageCount?
==
?
0
?
?
?
1
?:?pageCount;
?68
????????????}
?69
????????}
?70
?71
????????
return
?currentPage;
?72
?73
????}
?
//
?end?getCurrentPage
?74
?75
????
public
?
final
?
static
?
int
?getCurrentOffset(PageContext?pageContext,
?76
????????????String?collectionName,?
int
?maxResults)?
throws
?IOException,
?77
????????????JspException?
{
?78
?79
????????
int
?currentPage?
=
?getCurrentPage(pageContext,?collectionName,
?80
????????????????maxResults);
?81
????????
return
?(currentPage?
-
?
1
)?
*
?maxResults;
?82
?83
????}
?
//
?end?getCurrentOffset
?84
?85
????
public
?
final
?
static
?
void
?showPage(PageContext?pageContext,
?86
????????????String?collectionName,?
int
?maxResults)?
throws
?IOException,
?87
????????????JspException?
{
?88
?89
????????HttpServletRequest?request?
=
?(HttpServletRequest)?pageContext
?90
????????????????.getRequest();
?91
?92
????????
int
?currentPage?
=
?getCurrentPage(pageContext,?collectionName,
?93
????????????????maxResults);
?94
????????Collection?collList?
=
?
new
?ArrayList();
?95
????????
if
?(request.getAttribute(collectionName)?
!=
?
null
)?
{
?96
????????????
//
?get?collection?data?by?request
?97
????????????collList?
=
?(Collection)?request.getAttribute(collectionName);
?98
????????}
?
else
?
if
?(request.getSession().getAttribute(collectionName)?
!=
?
null
)?
{
?99
????????????
//
?get?collection?data?by?request
100
????????????collList?
=
?(Collection)?request.getSession().getAttribute(
101
????????????????????collectionName);
102
????????}
103
????????
int
?count?
=
?collList.size();
104
????????JspWriter?out?
=
?pageContext.getOut();
105
????????
//
?set?url
106
????????String[]?urlArray?
=
?getHttpUrls(request,?count,?currentPage,?maxResults);
107
108
????????
//
?generate?html?string
109
????????String?sbTR?
=
?getOutputString(pageContext,?count,?currentPage,
110
????????????????maxResults,?urlArray);
111
????????out.println(sbTR);?
//
?output?html
112
113
????}
?
//
?end?showPage
114
115
????
private
?
static
?String[]?getHttpUrls(HttpServletRequest?request,?
int
?count,
116
????????????
int
?currentPage,?
int
?maxResults)?
{
117
118
????????
int
?pageCount?
=
?(count?
%
?maxResults?
==
?
0
)?
?
?count?
/
?maxResults?:?(count
119
????????????????
/
?maxResults?
+
?
1
);
120
????????
if
?(pageCount?
==
?
0
)?
{
121
????????????pageCount?
=
?
1
;
122
????????}
123
????????String?urlString?
=
?request.getContextPath()
124
????????????????
+
?(String)?request.getSession().getAttribute(PAGING_URL);
125
126
????????Enumeration?enumParamNames?
=
?request.getParameterNames();
127
128
????????
//
?build?url,add?firstRequest?and?maxResults?parameters?to?url.
129
????????StringBuffer?firstPageURL?
=
?
new
?StringBuffer(urlString);
130
????????StringBuffer?previousPageURL?
=
?
new
?StringBuffer(urlString);
131
????????StringBuffer?nextPageURL?
=
?
new
?StringBuffer(urlString);
132
????????StringBuffer?lastPageURL?
=
?
new
?StringBuffer(urlString);
133
????????StringBuffer?jumpToPageURL?
=
?
new
?StringBuffer(urlString);
134
135
????????firstPageURL.append(
"
?currentPage=1
"
);
136
137
????????previousPageURL.append(
"
?currentPage=
"
).append(
138
????????????????(currentPage?
-
?
1
)?
<=
?
0
?
?
?
1
?:?(currentPage?
-
?
1
));
139
140
????????nextPageURL.append(
"
?currentPage=
"
).append(
141
????????????????(currentPage?
+
?
1
)?
>=
?pageCount?
?
?pageCount?:?(currentPage?
+
?
1
));
142
143
????????lastPageURL.append(
"
?currentPage=
"
).append(pageCount);
144
145
????????jumpToPageURL.append(
"
?currentPage={currentPageParameter}
"
);
146
147
????????
//
?append?old?parameters?to?url
148
????????
while
?(enumParamNames.hasMoreElements())?
{
149
????????????String?paramName?
=
?(String)?enumParamNames.nextElement();
150
????????????
if
?(
!
paramName.equals(
"
currentPage
"
)
151
????????????????????
&&
?
!
paramName.equals(
"
maxResults
"
))?
{
152
????????????????String?paramValue?
=
?(String)?request.getParameter(paramName);
153
????????????????previousPageURL.append(
"
&
"
).append(paramName).append(
"
=
"
)
154
????????????????????????.append(paramValue);
155
????????????????nextPageURL.append(
"
&
"
).append(paramName).append(
"
=
"
).append(
156
????????????????????????paramValue);
157
????????????????firstPageURL.append(
"
&
"
).append(paramName).append(
"
=
"
).append(
158
????????????????????????paramValue);
159
????????????????lastPageURL.append(
"
&
"
).append(paramName).append(
"
=
"
).append(
160
????????????????????????paramValue);
161
????????????????jumpToPageURL.append(
"
&
"
).append(paramName).append(
"
=
"
).append(
162
????????????????????????paramValue);
163
????????????}
164
????????}
165
166
????????String[]?urlArray?
=
?
{?firstPageURL.toString(),
167
????????????????previousPageURL.toString(),?nextPageURL.toString(),
168
????????????????lastPageURL.toString(),?jumpToPageURL.toString()?}
;
169
170
????????
return
?urlArray;
171
????}
?
//
?end?getHttpUrls
172
173
????
private
?
static
?String?getOutputString(PageContext?pageContext,?
int
?count,
174
????????????
int
?currentPage,?
int
?maxResults,?String[]?urlArray)
175
????????????
throws
?JspException?
{
176
177
????????
int
?pageCount?
=
?(count?
%
?maxResults?
==
?
0
)?
?
?count?
/
?maxResults?:?(count
178
????????????????
/
?maxResults?
+
?
1
);
179
????????
if
?(pageCount?
==
?
0
)?
{
180
????????????pageCount?
=
?
1
;
181
????????}
182
????????
//
?generate?html?string
183
????????StringBuffer?sbTR?
=
?
new
?StringBuffer();
184
185
????????String?firstPageURL?
=
?urlArray[
0
];
186
????????String?previousPageURL?
=
?urlArray[
1
];
187
????????String?nextPageURL?
=
?urlArray[
2
];
188
????????String?lastPageURL?
=
?urlArray[
3
];
189
????????String?jumpToPageURL?
=
?urlArray[
4
];
190
191
????????
//
?String[]?firstArg?=?{?firstPageURL?};
192
????????
//
?String[]?preArg?=?{?previousPageURL?};
193
????????
//
?String[]?nextArg?=?{?nextPageURL?};
194
????????
//
?String[]?lastArg?=?{?lastPageURL?};
195
196
????????
//
?parameter?for?paging?string
197
198
????????
//
?string?buffer?append
199
????????sbTR.append(
"
<td>
"
);
200
????????sbTR.append(
"
[<a?href=\
""
?+?firstPageURL?+?
"
\
"
?>首頁</a>]
"
);
201
????????sbTR.append(
"
[<a?href=\
""
?+?previousPageURL?+?
"
\
"
?>上一頁</a>]
"
);
202
????????sbTR.append(
"
[<a?href=\
""
?+?nextPageURL?+?
"
\
"
?>下一頁</a>]
"
);
203
????????sbTR.append(
"
[<a?href=\
""
?+?lastPageURL?+?
"
\
"
?>末頁</a>]
"
);
204
????????sbTR.append(
"
</td>
"
);
205
????????sbTR.append(
"
<td>
"
);
206
????????sbTR.append(
"
當前第?
"
?
+
?currentPage?
+
?
"
頁?/?共
"
?
+
?pageCount?
+
?
"
頁?
"
);
207
????????sbTR.append(
"
</td>
"
);
208
????????sbTR.append(
"
<td>
"
);
209
????????sbTR
210
????????????????.append(
"
轉到<input?id=\
"
jumpToPage\
"
?name=\
"
jumpToPage\
"
?type=\
"
text\
"
?class=\
"
bd\
"
?size=\
"
4
\
"
?/>頁
"
);
211
????????sbTR
212
????????????????.append(
"
<input?name=\
"
?GO?\
"
?type=\
"
button\
"
?class=\
"
bk\
"
?value=\
"
?GO?\
"
?onclick=\
"
GoToURL2(
'
"
213
????????????????????????
+
?jumpToPageURL
214
????????????????????????
+
?
"
',document.all.jumpToPage.value)\
"
/>
"
);
215
????????sbTR.append(
"
</td>
"
);
216
217
????????
//
?return
218
????????
return
?sbTR.toString();
219
????}
?
//
?end?getOutputString
220
221
????
public
?
final
?
static
?
void
?showPage(PageContext?pageContext,
222
????????????String?collectionName,?
int
?maxResults,?String?target)
223
????????????
throws
?IOException,?JspException?
{
224
225
????????HttpServletRequest?request?
=
?(HttpServletRequest)?pageContext
226
????????????????.getRequest();
227
228
????????
int
?currentPage?
=
?getCurrentPage(pageContext,?collectionName,
229
????????????????maxResults);
230
????????Collection?collList?
=
?
new
?ArrayList();
231
????????
if
?(request.getAttribute(collectionName)?
!=
?
null
)?
{
232
????????????
//
?get?collection?data?by?request
233
????????????collList?
=
?(Collection)?request.getAttribute(collectionName);
234
????????}
?
else
?
if
?(request.getSession().getAttribute(collectionName)?
!=
?
null
)?
{
235
????????????
//
?get?collection?data?by?request
236
????????????collList?
=
?(Collection)?request.getSession().getAttribute(
237
????????????????????collectionName);
238
????????}
239
????????
int
?count?
=
?collList.size();
240
????????JspWriter?out?
=
?pageContext.getOut();
241
????????
//
?set?url
242
????????String[]?urlArray?
=
?getHttpUrls(request,?count,?currentPage,?maxResults);
243
244
????????
//
?generate?html?string
245
????????String?sbTR?
=
?getOutputString(pageContext,?count,?currentPage,
246
????????????????maxResults,?urlArray,?target);
247
????????out.println(sbTR);?
//
?output?html
248
249
????}
?
//
?end?showPage
250
251
????
private
?
static
?String?getOutputString(PageContext?pageContext,?
int
?count,
252
????????????
int
?currentPage,?
int
?maxResults,?String[]?urlArray,?String?target)
253
????????????
throws
?JspException?
{
254
255
????????
int
?pageCount?
=
?(count?
%
?maxResults?
==
?
0
)?
?
?count?
/
?maxResults?:?(count
256
????????????????
/
?maxResults?
+
?
1
);
257
????????
if
?(pageCount?
==
?
0
)?
{
258
????????????pageCount?
=
?
1
;
259
????????}
260
????????
//
?generate?html?string
261
????????StringBuffer?sbTR?
=
?
new
?StringBuffer();
262
263
????????String?firstPageURL?
=
?urlArray[
0
];
264
????????String?previousPageURL?
=
?urlArray[
1
];
265
????????String?nextPageURL?
=
?urlArray[
2
];
266
????????String?lastPageURL?
=
?urlArray[
3
];
267
????????String?jumpToPageURL?
=
?urlArray[
4
];
268
269
????????
//
?parameter?for?paging?string
270
271
????????
//
?string?buffer?append
272
????????sbTR.append(
"
<td>
"
);
273
????????sbTR.append(
"
[<a?href=\
""
?+?firstPageURL?+?
"
\
"
?target=\
""
?+?target
274
????????????????
+
?
"
\
"
>
首頁
</
a
>
]
"
);
275
????????sbTR.append(
"
[<a?href=\
""
?+?previousPageURL?+?
"
\
"
?target=\
""
?+?target
276
????????????????
+
?
"
\
"
>
上一頁
</
a
>
]
"
);
277
????????sbTR.append(
"
[<a?href=\
""
?+?nextPageURL?+?
"
\
"
?target=\
""
?+?target
278
????????????????
+
?
"
\
"
>
下一頁
</
a
>
]
"
);
279
????????sbTR.append(
"
[<a?href=\
""
?+?lastPageURL?+?
"
\
"
?target=\
""
?+?target
280
????????????????
+
?
"
\
"
>
末頁
</
a
>
]
"
);
281
????????sbTR.append(
"
</td>
"
);
282
????????sbTR.append(
"
<td>
"
);
283
????????sbTR.append(
"
當前第?
"
?
+
?currentPage?
+
?
"
頁?/?共
"
?
+
?pageCount?
+
?
"
頁?
"
);
284
????????sbTR.append(
"
</td>
"
);
285
????????sbTR.append(
"
<td>
"
);
286
????????sbTR
287
????????????????.append(
"
轉到<input?id=\
"
jumpToPage\
"
?name=\
"
jumpToPage\
"
?type=\
"
text\
"
?class=\
"
bd\
"
?size=\
"
4
\
"
?/>頁
"
);
288
????????sbTR.append(
"
?[<a?href=\
"
#\
"
?onclick=\
"
javascript:GoToURL(
this
,
'
"
289
????????????????
+
?jumpToPageURL?
+
?
"
',document.all.jumpToPage.value,'
"
?
+
?target
290
????????????????
+
?
"
')\
"
?target
=
\
""
?
+
?target?
+
?
"
\
"
>
?GO?
</
a
>
]
"
);
291
????????sbTR.append(
"
</td>
"
);
292
293
????????
//
?return
294
????????
return
?sbTR.toString();
295
????}
?
//
?end?getOutputString
296
}
?
//
?end?FrontSplitPageUtil
297

??2

??3

??4

??5

??6

??7

??8

??9

?10

?11

?12

?13

?14

?15


?16

?17

?18

?19

?20

?21

?22

?23

?24

?25

?26

?27

?28

?29

?30

?31

?32



?33

?34

?35

?36

?37

?38

?39

?40



?41

?42

?43

?44



?45

?46

?47



?48

?49

?50

?51

?52

?53

?54

?55

?56

?57

?58

?59

?60



?61

?62

?63



?64

?65

?66



?67

?68

?69

?70

?71

?72

?73

?74

?75

?76

?77



?78

?79

?80

?81

?82

?83

?84

?85

?86

?87



?88

?89

?90

?91

?92

?93

?94

?95



?96

?97

?98



?99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116



117

118

119

120



121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148



149

150

151



152

153

154

155

156

157

158

159

160

161

162

163

164

165

166



167

168

169

170

171

172

173

174

175



176

177

178

179



180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223



224

225

226

227

228

229

230

231



232

233

234



235

236

237

238

239

240

241

242

243

244

245

246

247

248

249

250

251

252

253



254

255

256

257



258

259

260

261

262

263

264

265

266

267

268

269

270

271

272

273

274

275

276

277

278

279

280

281

282

283

284

285

286

287

288

289

290

291

292

293

294

295

296

297
