Implement pagination in struts

Source: Internet
Author: User

 

I can see it online and learn it.

1. iformpages interface. Each form that requires paging inherits this interface:
Package com. Fellow. Pub. servlet;

Public interface iformpages {
Int getcurrentpage ();
Void setcurrentpage (INT currentpage );
Int getitemperpage ();
Void setitemperpage (INT itemperpage );
}

2. pagination computing pages:
Package com. Fellow. Pub. util;

Import java. util. vector;
Import java. util. collection;
Import com. Fellow. Pub. servlet. iformpages;

Public class pages {
Protected int itemcount;
Protected int itemperpage;
Protected int currentpage;

Public pages (){
This. itemcount = 0;
This. itemperpage = 0;
This. currentpage = 1;
}

Public pages (INT itemcount, int itemperpage) throws exception {
This (itemcount, itemperpage, 1 );
}

Public pages (INT itemcount, int itemperpage, int currentpage) throws exception {
This. itemcount = (itemcount <= 0 )? 0: itemcount;
This. itemperpage = (itemperpage <= 0 )? 20: itemperpage;
Int curpage = (currentpage <= 0 )? 1: currentpage;

Int pagecount = (itemcount % itemperpage = 0 )? (Itemcount/itemperpage) :( itemcount/itemperpage + 1 );
If (curpage> pagecount) curpage = pagecount;
This. currentpage = curpage;
}

Public pages (Collection collection, iformpages formpages) throws exception {
This (collection. Size (), formpages. getitemperpage (), formpages. getcurrentpage ());
}

Public int getitemcount (){
Return this. itemcount;
}

Public void setitemcount (INT itemcount ){
This. itemcount = itemcount;
}

Public int getitemperpage (){
Return this. itemperpage;
}

Public void setitemperpage (INT itemperpage ){
This. itemperpage = itemperpage;
}

Public int getcurrentpage (){
Return this. currentpage;
}

Public void setcurrentpage (INT currentpage ){
This. currentpage = currentpage;
}

Public int getprepage (){
Return this. currentpage-1;
}

Public int getnextpage (){
Return this. currentpage + 1;
}

Protected void asserterrors () throws exception {
If (itemperpage <= 0 |
Currentpage <= 0 |
Itemcount <itemperpage * (currentpage-1 )){
Throw new exception ("pages object calculated error ");
}
}

Public int getpagecount () throws exception {
If (itemcount <= 0) return 0;
Asserterrors ();
If (itemcount % itemperpage = 0 ){
Return itemcount/itemperpage;
}
Else {
Return itemcount/itemperpage + 1;
}
}

Public int getpageitemcount () throws exception {
If (itemcount <= 0) return 0;
Int pagecount = getpagecount ();

If (pagecount = currentpage ){
Return itemcount-itemperpage * (currentpage-1 );
}
Else {
Return itemperpage;
}
}

Public int getpageitemcount (INT currentpage) throws exception {
If (itemcount <= 0) return 0;
Int pagecount = getpagecount ();

If (pagecount = currentpage ){
Return itemcount-itemperpage * (currentpage-1 );
}
Else {
Return itemperpage;
}
}

Public Boolean isfirstpage () throws exception {
Return (currentpage = 1 );
}

Public Boolean islastpage () throws exception {
Int pagecount = getpagecount ();
Return (pagecount = currentpage );
}

Public Collection getindexlist () throws exception {
Int pagecount = getpagecount ();
Vector result = new vector (pagecount );
For (INT I = 0; I <pagecount; I ++ ){
Result. Add (New INTEGER (I + 1 ));
}
Return result;
}
}

3. the pagination collection class collectionpages inherits pages:
Package com. Fellow. Pub. util;

Import java. util .*;

Import com. Fellow. Pub. servlet. iformpages;

Public class collectionpages extends pages {
Protected collection = NULL;
Protected collection collectionpage = NULL;
Public collectionpages (Collection collection,
Iformpages formpages) throws exception {
Super (collection, formpages );
This. Collection = collection;
Buildcollectionpage ();
}

Public Collection getcollection (){
Return this. collection;
}

Public void setcollection (Collection collection ){
This. Collection = collection;
}

Public Collection getcollectionpage (){
Return this. collectionpage;
}

Public Collection buildcollectionpage () throws exception {
If (Collection = NULL ){
Throw new exception ("collection object is null ");
}

Collectionpage = new vector (itemperpage );
Collectionutil. Copy (collection, collectionpage,
(Currentpage-1) * itemperpage,
(Currentpage) * itemperpage );
Return collectionpage;
}
}

4. Initialization in the action:
Collectionpages = new collectionpages (collection, formpages );
Collection is the set to be paged.

5. Save to request
Setattribute (name, collectionpages );

6. Use the collectionpages object on the page. The collectionpages object has two sub-objects:
I. collectionpage is the set after pagination.
<Bean: Define id = "collectionlist" name = "collectionpages" property = "collectionpage" Scope = "request"/>
Ii. indexlist is the page number (because I don't know how to process the page number in a simple way, I have to get such an object to iterate)
<Bean: Define id = "indexlist" name = "collectionpages" property = "indexlist" Scope = "request"/>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.