细心!用心!耐心!
吾非文人,乃市井一俗人也,读百卷书,跨江河千里,故申城一游; 一两滴辛酸,三四年学业,五六点粗墨,七八笔买卖,九十道人情。
BlogJava
联系
聚合
管理
1 Posts :: 196 Stories :: 10 Comments :: 0 Trackbacks
常用链接
我的随笔
我的评论
我的参与
最新评论
留言簿
(5)
给我留言
查看公开留言
查看私人留言
随笔分类
网关编程
设计模式
文章分类
AJAX技术(13)
ANT的使用(3)
Behavioral 模式 (11)
core java中的一些数据结构的处理(15)
Creational 模式(7)
I/O机制的编程
JPA(6)
liferay portal(19)
Oracle BPM专题(1)
SSH框架编程(1)
Structural 模式 (10)
webservice编程
事务编程(2)
任务调度器(1)
多執行緒模式(8)
多线程编程(5)
如何debug(2)
常用javascript(7)
数据库编程(5)
服务器编程(5)
网关编程(6)
网络协议编程(2)
面向对象的一些难点问题
项目框架的设想(21)
文章档案
2014年7月 (5)
2014年1月 (1)
2012年10月 (3)
2012年9月 (4)
2012年6月 (7)
2008年11月 (8)
2008年5月 (1)
2007年11月 (2)
2007年10月 (2)
2007年7月 (7)
2007年5月 (42)
2007年4月 (58)
2007年3月 (9)
2007年2月 (8)
2007年1月 (7)
收藏夹
Doug Lea关于util.concurrent并发工具包的讲座(3)
搜索
最新评论
1. re: createNativeQuery原生-命名查询[未登录]
query.getResultList() 这个返回的值 用什么实体 类 接受 呢?
--111
2. re: JPA本地查询(Native Query)(二)[未登录]
fdfdf
--abc
3. re: JPA EntityManager详解(一)
版主,如果利用entityManager进行查询,如何在sql里边传参,防注入的那种
--谢谢
4. re: dwr简介--一个例子(一)
天通苑
--67
5. re: 图片自动生成器
wq ery 2we wei 3ik w3
-- kplie
前台分页
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
posted on 2007-03-16 15:25
张金鹏
阅读(494)
评论(0)
编辑
收藏
所属分类:
core java中的一些数据结构的处理
新用户注册
刷新评论列表
只有注册用户
登录
后才能发表评论。
网站导航:
博客园
IT新闻
Chat2DB
C++博客
博问
管理
相关文章:
截取字符串(针对字符串插入数据库,长度问题)
常用工具类
杂议java知识
发送邮件
文件上传
文件下载
前台分页
字符串变数组
去除所有引号的操作
与数据库操作相关的工具类
Powered by:
BlogJava
Copyright © 张金鹏