I have always asked how to do Structs, I have accumulated some experience, the following is my implementation. My implementation is relatively flexible. If you have any better ideas, you are welcome to enlighten me. PS: There are some classes and methods injects, such as collectutil.copy (), in fact, the copy of the collection, you will understand, you can realize it yourself.
1. IFormPages interface, each requires a paging Form to inherit this interface: package com.fellow.pub.servlet;
Public interface iformpages {Int getCurrentPage (); void setCurrentPage (int CurrentPage); void setItemperpage (int itemperpage);});} (INT ITEMPARPAGE)
2. Pieces of calculation class 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 currentpage
Public pages () {this.itemcount = 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, IFLECPAGES 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 GETITEMPAGE () {Return this.ItemPage;
Public void setItemperPage (ITE.ItemPage) {this.ItemPage = itemPage;} public int getcurrentpage () {returnim
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 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 {= {ney (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 {ix (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 Import java.util. *; Import com.fellow.pub.servlet.iformpages; public class CollectionPages extends Pages {protected Collection 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 {this.collection = collection;} Public Collection getCollectionPage () {return this.collection; Public Collection BuildCollectionPage () THROWS Exception {if (Collection == Null) {throw new exception ("Collection Object Is Null); CollectionPage = New Vector (itemPage); Collection, Collection, CURRENTPAGE - 1) * ItemPage (CURRENTPAGE) * ITEMPERPAGE; RETURN CollectionPage;}} 4. Initialization inside: CollectionPages CollectionPages = New Collection, Forms); where Collection is a collection of paging 5. Save to RequestSetaTRibute (Name, CollectionPages);