TpageList
类
package
jfis.util;
import
java.util.List;
public
class
TPageList {
private
TPages
page
=
null
;
//分页对象
private
List
objectList
=
null
;
//对象集合
private
String
pageStr
=
""
;
//分页字符串
public
List getObjectList() {
return
objectList
;
}
public
void
setObjectList(List objectList) {
this
.
objectList
= objectList;
}
public
TPages getPage() {
return
page
;
}
public
void
setPage(TPages page) {
this
.
page
= page;
pageStr
=page.getLastPageBreak();
}
public
String getPageStr() {
return
pageStr
;
}
public
void
setPageStr(String pageStr) {
this
.
pageStr
= pageStr;
}
}
Tpages
类
package
jfis.util;
import
javax.servlet.http.HttpServletRequest;
public
class
TPages {
private
String
fileName
=
""
;
private
int
pageSize
=2;
//页大小
private
int
allPage
=1;
//总页数
private
int
cPage
=1;
//当前页
private
int
sPage
=1;
//当前页开始记录数
private
int
allRecord
=1;
//总记录数
private
String
lastPageBreak
=
""
;
//分页字符串
private
int
a
;
//分拆list结果集的变量(相当与游标)。可从list中的确定位置开始读数据
// String bian = request.getParameter("aaa")==null?"0":"5";
// this.a = new Integer(bian);
public
int
getA() {
return
a
;
}
public
void
setA(
int
a) {
this
.
a
= a;
}
public
TPages(){
}
public
TPages(String fileName,HttpServletRequest request){
this
.
fileName
=fileName;
//获得当前页
String cPageStr = request.getParameter(
"cpage"
)==
null
?
"1"
:request.getParameter(
"cpage"
);
this
.
a
= request.getParameter(
"aaa"
)==
null
? 0 :
new
Integer(request.getParameter(
"aaa"
));
int
cPageInt = 0;
try
{
cPageInt = Integer.parseInt(cPageStr);
}
catch
(Exception e){
System.
out
.println(
"com.s6.util.Pages = "
+e.getMessage());
cPageInt = 1;
}
finally
{
cPage
= cPageInt;
}
}
//
计算总页数
,
判断当前页的合法性,计算当前页的开始记录数,生成分页字符串
public
void
doPage(){
//计算总页数
this
.
allPage
=(
int
)(
this
.
allRecord
+
this
.
pageSize
-1)/
this
.
pageSize
;
//判断当前页的合法性
if
(
this
.
cPage
<0){
this
.
cPage
=1;
}
if
(
this
.
cPage
>
this
.
allPage
){
this
.
cPage
=
this
.
allPage
;
}
//
计算当前页开始记录数
this
.
sPage
=(
this
.
cPage
-1)*
this
.
pageSize
+1;
//
生成分页字符串
StringBuffer str=
new
StringBuffer();
//
判断是否有问号
“
?
”
if
(
this
.
fileName
.indexOf(
"?"
)==-1){
this
.
fileName
=
this
.
fileName
+
"?1=1"
;
}
str.append(
"<table width=/"100%/" height=/"100%/">"
);
//添加表单
str.append(
"<form name=/"pagelist/" method=/"post/" action=/""
);
str.append(
this
.
fileName
+
"/">"
);
str.append(
"<tr><td width=/"100%/" height=/"100%/" align=/"right/" valign=/"middle/">"
);
str.append(
"总纪录数"
+
this
.
allRecord
+
"条"
);
str.append(
" "
);
//首页
if
(
this
.
cPage
>1){
str.append(
"<a href="
+
this
.
fileName
+
"&cpage=1>首页</a>"
);
}
else
{
str.append(
"首页"
);
}
str.append(
" "
);
//上一页
if
(
this
.
cPage
>1){
str.append(
"<a href="
);
str.append(
this
.
fileName
+
"&cpage="
);
str.append(
this
.
cPage
-1);
str.append(
"&aaa="
);
str.append(
this
.
a
-5);
str.append(
">上一页</a>"
);
}
else
{
str.append(
"上一页"
);
}
str.append(
" "
);
//下一页
if
(
this
.
cPage
<
this
.
allPage
){
str.append(
"<a href="
);
str.append(
this
.
fileName
+
"&cpage="
);
str.append(
this
.
cPage
+1);
str.append(
"&aaa="
);
str.append(
this
.
a
+5);
str.append(
">下一页</a>"
);
}
else
{
str.append(
"下一页"
);
}
str.append(
" "
);
//末页
if
(
this
.
cPage
<
this
.
allPage
){
str.append(
"<a href="
);
str.append(
this
.
fileName
+
"&cpage="
);
str.append(
this
.
allPage
);
str.append(
">末页</a>"
);
}
else
{
str.append(
"末页"
);
}
str.append(
" "
);
//第几页
str.append(
"第"
);
str.append(
this
.
cPage
);
str.append(
"页"
);
str.append(
" "
);
//共几页
str.append(
"共"
);
str.append(
this
.
allPage
);
str.append(
"页"
);
str.append(
" "
);
//跳转页
for
(
int
i=1;i<=
this
.
allPage
;i++){
str.append(
" "
);
if
(i==
this
.
cPage
){
str.append(i);
}
else
{
str.append(
"<a href="
);
str.append(
this
.
fileName
+
"&cpage="
);
str.append(i);
str.append(
">"
+i+
"</a>"
);
}
}
str.append(
"</td></tr></form>"
);
str.append(
"</table>"
);
lastPageBreak
=str.toString();
}
public
int
getAllPage() {
return
allPage
;
}
public
void
setAllPage(
int
allPage) {
this
.
allPage
= allPage;
}
public
int
getCPage() {
return
cPage
;
}
public
void
setCPage(
int
page) {
cPage
= page;
}
public
String getFileName() {
return
fileName
;
}
public
void
setFileName(String fileName) {
this
.
fileName
= fileName;
}
public
String getLastPageBreak() {
return
lastPageBreak
;
}
public
void
setLastPageBreak(String lastPageBreak) {
this
.
lastPageBreak
= lastPageBreak;
}
public
int
getPageSize() {
return
pageSize
;
}
public
void
setPageSize(
int
pageSize) {
this
.
pageSize
= pageSize;
}
public
int
getSPage() {
doPage();
return
sPage
;
}
public
void
setSPage(
int
page) {
sPage
= page;
}
public
int
getAllRecord() {
return
allRecord
;
}
public
void
setAllRecord(
int
allRecord) {
this
.
allRecord
= allRecord;
}
}
DAO
里的方法
//
分页
public
TPageList pageSearchlist(TPages pages, List list) {
TPageList pageList =
new
TPageList();
//
记录集总行数
pages.setAllRecord(list.size());
// rs
用于保存从
list
中截取的记录
List rs =
new
ArrayList();
//
控制从
list
集合中的第几条开始读取
,
相当于游标
int
a = pages.getA();
// for
循环中
"5"
代表一次读出多少条数据
:"a"
控制从
list
集合中的第几条开始读取
,
相当于游标。
for
(
int
i = 0; i < 5 && a < list.size(); a++) {
rs.add(list.get(a));
i++;
}
Iterator iterator = rs.iterator();
//
得到分页字符串
pages.getSPage();
int
i = 0;
// 循环变量
// 判断记录总行数是否大于0,并循环创建USER对象
if
(pages.getAllRecord() > 0) {
do
{
i++;
}
while
(iterator.hasNext() && i < pages.getPageSize());
pageList.setPage(pages);
pageList.setObjectList(rs);
}
return
pageList;
}
Action
里的方法
public
ActionForward pageSearchList(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
TPages pages =
new
TPages(
"/JFIS_CompanyManager/client/outer.do?method=pageSearchList"
,
request);
pages.setPageSize(5);
TPageList pageList =
iouter
.pageSearchlist(pages,
iouter
.getAllOuter());
request.setAttribute(
"outers"
, pageList);
return
mapping.findForward(
"outer_list"
);
}
SSH
分页
说明:分页的两个类不变,只变Action和DAO。
Action
中
// 分页
public
ActionForward pageSearchList(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
Pages pages =
new
Pages(
"/JFIS_CompanyManager/client/outer.do?method=pageSearchList"
,request);
pages.setPageSize(10);
PageList pageList =
iouter
.outerList(pages);
request.setAttribute(
"outers"
, pageList);
return
mapping.findForward(
"outer_list"
);
}
DAO
中
// 分页
public
PageList outerList(Pages pages) {
PageList pageList =
new
PageList();
List list =
new
ArrayList();
/*开启一个HibernateSession对象*/
Session session =
this
.getSession();
Transaction t = session.beginTransaction();
/*
获得结果集
*/
Query query = session
.createQuery(
"from jfis.bean.JfisClientItem order by starttime desc"
);
/*
记录集总行数
*/
pages.setAllRecord(query.list().size());
query.setFirstResult(pages.getSPage() - 1);
query.setMaxResults(10);
/*设置每页显示的记录数*/
List rs = query.list();
Iterator iterator = rs.iterator();
/*得到分页字符串*/
pages.getSPage();
int
i = 0;
/*
循环变量
*/
/*
判断记录总行数是否大于
0
,并循环创建
USER
对象
*/
if
(pages.getAllRecord() > 0) {
do
{
list = rs;
i++;
}
while
(iterator.hasNext() && i < pages.getPageSize());
pageList.setPage(pages);
pageList.setObjectList(list);
t.commit();
/*事务提交*/
session.close();
}
return
pageList;
}