?
Pager類:

?

package
?com.zeroidea.common;

?


public
?
class
?Pager?
{

???????????????????????
private
?
int
?totalRows;???
//
?總行數(shù)????
???????????????????????
private
?
int
?pageSize;????
//
?單頁(yè)數(shù)據(jù)項(xiàng)
???????????????????????
private
?
int
?currentPage;
//
?當(dāng)前頁(yè)號(hào)
???????????????????????
private
?
int
?totalPages;?
//
?總頁(yè)數(shù)
???????????????????????
private
?
int
?startRows;??
//
?當(dāng)前頁(yè)在數(shù)據(jù)庫(kù)中記錄位置
???????????????????????


???????????????????????
public
?Pager()
{??

???????????????????????}
???????????????????????


???????????????????????
public
?Pager(
int
?totalRows)
{?
//
?指定總行數(shù)據(jù)
????????????????????????
this
.setPageSize(
10
);???
//
?每頁(yè)?10條數(shù)據(jù)?
????????????????????????
this
.setTotalRows(totalRows);???

?

????????????????????????
int
?pages?
=
?totalRows
/
pageSize;


????????????????????????
if
?((totalRows
%
pageSize)?
>
?
0
)
{

????????????????????????????pages
++
;

????????????????????????}
????????????????????????
this
.setTotalPages(pages);

????????????????????????

????????????????????????
this
.setStartRows(
0
);

????????????????????????
this
.setCurrentPage(
1
);?

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?
@return
?the?currentPage

????????????????????????
*/
???????????????????????
public
?
int
?getCurrentPage()?
{

????????????????????????
return
?currentPage;

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?
@param
?currentPage?the?currentPage?to?set

????????????????????????
*/
???????????????????????
public
?
void
?setCurrentPage(
int
?currentPage)?
{

????????????????????????
this
.currentPage?
=
?currentPage;

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?
@return
?the?pageSize

????????????????????????
*/
???????????????????????
public
?
int
?getPageSize()?
{

????????????????????????
return
?pageSize;

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?
@param
?pageSize?the?pageSize?to?set

????????????????????????
*/
???????????????????????
public
?
void
?setPageSize(
int
?pageSize)?
{

????????????????????????
this
.pageSize?
=
?pageSize;

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?
@return
?the?startRows

????????????????????????
*/
???????????????????????
public
?
int
?getStartRows()?
{

????????????????????????
return
?startRows;

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?
@param
?startRows?the?startRows?to?set

????????????????????????
*/
???????????????????????
public
?
void
?setStartRows(
int
?startRows)?
{

????????????????????????
this
.startRows?
=
?startRows;

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?
@return
?the?totalPages

????????????????????????
*/
???????????????????????
public
?
int
?getTotalPages()?
{

????????????????????????
return
?totalPages;

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?
@param
?totalPages?the?totalPages?to?set

????????????????????????
*/
???????????????????????
public
?
void
?setTotalPages(
int
?totalPages)?
{

????????????????????????
this
.totalPages?
=
?totalPages;

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?
@return
?the?totalRows

????????????????????????
*/
???????????????????????
public
?
int
?getTotalRows()?
{

????????????????????????
return
?totalRows;

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?
@param
?totalRows?the?totalRows?to?set

????????????????????????
*/
???????????????????????
public
?
void
?setTotalRows(
int
?totalRows)?
{

????????????????????????
this
.totalRows?
=
?totalRows;

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?第一頁(yè)

????????????????????????
*/
???????????????????????
public
?
void
?fristPage()
{

????????????????????????
this
.setCurrentPage(
1
);

????????????????????????
this
.setStartRows(
0
);

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?前一頁(yè)

????????????????????????
*/
???????????????????????
public
?
void
?previousPage()
{

????????????????????????
int
?nowPage?
=
?
this
.getCurrentPage();?


????????????????????????
if
?(nowPage?
==
?
1
)
{

????????????????????????????
return
;

????????????????????????}
???????????????????????
this
.setCurrentPage(
--
nowPage);

????????????????????????
this
.setStartRows((nowPage?
-
?
1
)?
*
?
this
.getPageSize());

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?后一頁(yè)

????????????????????????
*/
???????????????????????
public
?
void
?nextPage()
{

????????????????????????
int
?nowPage?
=
?
this
.getCurrentPage();


????????????????????????
if
?(nowPage?
==
?
this
.getTotalPages())
{

????????????????????????????
this
.lastPage();

????????????????????????????
return
;

????????????????????????}
????????????????????????
this
.setCurrentPage(nowPage?
+
?
1
);

????????????????????????
this
.setStartRows(nowPage?
*
?
this
.getPageSize());

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?最后一頁(yè)

????????????????????????
*/
???????????????????????
public
?
void
?lastPage()
{

????????????????????????
this
.setCurrentPage(
this
.getTotalPages());

????????????????????????
this
.setStartRows((
this
.getTotalPages()?
-
?
1
)?
*
?
this
.getPageSize());

???????????????????????}
???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?刷新頁(yè)

????????????????????????*?
@param
?currentPage

????????????????????????
*/
???????????????????????
public
?
void
?refreshPage(
int
?currentPage)
{

????????????????????????
this
.setCurrentPage(currentPage);???

????????????????????????
this
.setStartRows((currentPage?
-
?
1
)?
*
?
this
.getPageSize());


????????????????????????
if
?(currentPage?
>
?
this
.getTotalPages())
{

????????????????????????????
this
.lastPage();

????????????????????????}
???????????????????????}
}
?

?

數(shù)據(jù)查詢:

?

???????????????????????


???????????????????????
/**?*/
/**
????????????????????????*?取得總數(shù)據(jù)行??:有更簡(jiǎn)單的方法請(qǐng)分享

????????????????????????
*/
???????????????????????
public
?
int
?selectDataRows()?
{

????????????????????????
int
?rows?
=
?
0
;

????????????????????????List?list?
=
?
null
;

????????????????????????Session?session?
=
?
null
;

????????????????????????Transaction?transaction?
=
?
null
;

????????????????????????Query?query?
=
?
null
;


????????????????????????
try
?
{

????????????????????????????session?
=
?SessionFactory.currentSession();

????????????????????????????transaction?
=
?session.beginTransaction();

????????????????????????????query?
=
?session

????????????????????????????????????.createQuery(
"
select?msg?from?Msg?as?msg
"
);

????????????????????????????list?
=
?query.list();

????????????????????????????rows?
=
?list.size();

????????????????????????????transaction.commit();

????????????????????????????session.close();


????????????????????????}
?
catch
?(Exception?excp)?
{

????????????????????????????session.close();

????????????????????????}
?

????????????????????????
return
?rows;

???????????????????????}
???????????????????????

?

?

?


???????????????????????
/**?*/
/**
????????????????????????*?分頁(yè)查詢數(shù)據(jù)

????????????????????????*?

????????????????????????*?
@param
?pager?

????????????????????????*?
@return
?0?1?0:?Success?1:?Fail

????????????????????????
*/
???????????????????????
public
?List?selectData(Pager?pager)?
{

????????????????????????List?list?
=
?
null
;

????????????????????????Session?session?
=
?
null
;

????????????????????????Transaction?transaction?
=
?
null
;

????????????????????????Query?query?
=
?
null
;

????????????????????????


????????????????????????
try
?
{

????????????????????????????session?
=
?SessionFactory.currentSession();

????????????????????????????transaction?
=
?session.beginTransaction();

????????????????????????????query?
=
?session.createQuery(
"
select?msg?from?Msg?as?msg?order?by?msg.msgId?desc
"
);

????????????????????????????query.setFirstResult(pager.getStartRows());?
//
?設(shè)置起始行
????????????????????????????query.setMaxResults(pager.getPageSize());???
//
?設(shè)置取數(shù)據(jù)行數(shù)
????????????????????????????list?
=
?query.list();

????????????????????????????transaction.commit();

????????????????????????????session.close();


????????????????????????}
?
catch
?(Exception?excp)?
{

????????????????????????????session.close();

????????????????????????}
????????????????????????
return
?list;

???????????????????????}
?

?

?

Action?控制:

?


???????????????????????
/**?*/
/**
????????????????????????*?查詢?方法,,按分頁(yè)查詢

????????????????????????*?

????????????????????????*?
@param
?mapping

????????????????????????*?
@param
?form

????????????????????????*?
@param
?request

????????????????????????*?
@param
?response

????????????????????????*?
@return
????????????????????????
*/
???????????????????????
public
?
void
?selectData(ActionMapping?mapping,?ActionForm?form,


????????????????????????????HttpServletRequest?request,?HttpServletResponse?response)?
{

????????????????????????List?list?
=
?
null
;

????????????????????????
int
?currentPage?
=
?
1
;

????????????????????????MsgForm?msgForm?
=
?(MsgForm)?form;

????????????????????????

????????????????????????String?pageMethod?
=
?request.getParameter(
"
pageMethod
"
);?????????
//
?存放請(qǐng)求頁(yè)方法
????????????????????????String?currentPageTemp?
=
?request.getParameter(
"
currentPage
"
);???
//
?存放當(dāng)前頁(yè)
????????????????????????

????????????????????????OperTable?operTable?
=
?
new
?OperTable();??????????????????????????
//
?操作表類
????????????????????????Pager?pager?
=
?
new
?Pager(operTable.selectDataRows());????????????
//
?頁(yè)面控制類
????????????????????????

????????????????????????


????????????????????????
if
?(currentPageTemp?
==
?
null
)
{

????????????????????????????pager.setCurrentPage(
1
);


????????????????????????}
?
else
?
{

????????????????????????????currentPage?
=
?Integer.parseInt(currentPageTemp);

????????????????????????}
????????????????????????

????????????????????????pager.setCurrentPage(currentPage);

????????????????????????


????????????????????????
if
?(pageMethod?
==
?
null
)
{

????????????????????????????pager.fristPage();??????
//
?到首頁(yè)
????????????????????????????System.out.println(
"
首頁(yè)
"
);


????????????????????????}
else
{


????????????????????????????
if
?(pageMethod.equals(
"
previousPage
"
))
{

????????????????????????????????pager.previousPage();???
//
?到前一頁(yè)
????????????????????????????????System.out.println(
"
前一頁(yè)
"
);


????????????????????????????}
?
else
?
if
?(pageMethod.equals(
"
nextPage
"
))
{

????????????????????????????????pager.nextPage();???????
//
?到后一頁(yè)
????????????????????????????????System.out.println(
"
后一頁(yè)
"
);


????????????????????????????}
?
else
?
if
?(pageMethod.equals(
"
lastPage
"
))
{

????????????????????????????????pager.lastPage();???????
//
?到尾頁(yè)
????????????????????????????????System.out.println(
"
尾頁(yè)
"
);


????????????????????????????}
else
?
if
?(pageMethod.equals(
"
refreshPage
"
))
{

????????????????????????????????pager.refreshPage(currentPage);

????????????????????????????????System.out.println(currentPage);

????????????????????????????????System.out.println(
"
刷新頁(yè)
"
);


????????????????????????????}
?
else
?
{

????????????????????????????????pager.fristPage();??????
//
?到首頁(yè)
????????????????????????????????System.out.println(
"
首頁(yè)
"
);

????????????????????????????}
????????????????????????}
????????????????????????

????????????????????????request.setAttribute(
"
pager
"
,?pager);

????????????????????????request.setAttribute(
"
msgdata
"
,?list);

????????????????????????request.setAttribute(
"
info
"
,?
"
已查詢數(shù)據(jù)
"
);

????????????????????????msgForm.reset(mapping,?request);

???????????????????????}
?

?

JSP顯示分頁(yè):?href這樣用:

?

????????????????????????????????????
<
logic:present?name
=
"
pager
"
>
????????????????????????????????????????
<
a?href
=
"
msg.do?method=select&pageMethod=fristPage¤tPage=<bean:write?name='pager'?property='currentPage'/>
"
>
首頁(yè)
</
a
>
????????????????????????????????????????
<
a?href
=
"
msg.do?method=select&pageMethod=previousPage¤tPage=<bean:write?name='pager'?property='currentPage'/>
"
>
前一頁(yè)
</
a
>
????????????????????????????????????????
<
a?href
=
"
msg.do?method=select&pageMethod=nextPage¤tPage=<bean:write?name='pager'?property='currentPage'/>
"
>
后一頁(yè)
</
a
>
????????????????????????????????????????
<
a?href
=
"
msg.do?method=select&pageMethod=lastPage¤tPage=<bean:write?name='pager'?property='currentPage'/>
"
>
尾頁(yè)
</
a
>
????????????????????????????????????????共有
<
span?
class
=
"
font-normal-red
"
><
bean:write?name
=
"
pager
"
?property
=
"
totalRows
"
/></
span
>
條記錄,當(dāng)前第
<
span?
class
=
"
font-normal-red
"
><
bean:write?name
=
"
pager
"
?property
=
"
currentPage
"
/></
span
>
頁(yè),共有
<
span?
class
=
"
font-normal-red
"
><
bean:write?name
=
"
pager
"
?property
=
"
totalPages
"
/></
span
>
?頁(yè)

????????????????????????????????????
</
logic:present
>
?

<
html:link
>
?這樣用

?

??????????????????
<
logic:present?name
=
"
pager
"
>
?????????????????????
<
html:link?page
=
"
/msg.do?method=select&pageMethod=fristPage
"
?paramId
=
"
currentPage
"
?paramName
=
"
pager
"
?paramProperty
=
"
currentPage
"
>
首頁(yè)
</
html:link
>
?????????????????????
<
html:link?page
=
"
/msg.do?method=select&pageMethod=previousPage
"
?paramId
=
"
currentPage
"
?paramName
=
"
pager
"
???paramProperty
=
"
currentPage
"
>
前一頁(yè)
</
html:link
>
?????????????????????
<
html:link?page
=
"
/msg.do?method=select&pageMethod=nextPage
"
????paramId
=
"
currentPage
"
?paramName
=
"
pager
"
???paramProperty
=
"
currentPage
"
>
下一頁(yè)
</
html:link
>
?????????????????????
<
html:link?page
=
"
/msg.do?method=select&pageMethod=lastPage
"
????paramId
=
"
currentPage
"
?paramName
=
"
pager
"
???paramProperty
=
"
currentPage
"
>
尾頁(yè)
</
html:link
>
?????????????????????共有

?????????????????????
<
span?
class
=
"
font-normal-red
"
><
bean:write?name
=
"
pager
"
?property
=
"
totalRows
"
?
/>
?????????????????????
</
span
>
條記錄,當(dāng)前第
<
span?
class
=
"
font-normal-red
"
><
bean:write?name
=
"
pager
"
?property
=
"
currentPage
"
?
/>
?????????????????????
</
span
>
頁(yè),共有
<
span?
class
=
"
font-normal-red
"
><
bean:write?name
=
"
pager
"
?property
=
"
totalPages
"
?
/>
?????????????????????
</
span
>
?頁(yè)

?????????????????????????????????????????????????????????????????????????????????????????????????
</
logic:present
>
posted on 2006-11-30 16:26
一手的小窩窩 閱讀(168)
評(píng)論(0) 編輯 收藏 所屬分類:
JAVA