Here goes a Bean I use for next and previous results in JSP. I also use it to implement numbered-page selections (the block parts of the Bean). I think it´ll help you:
/**
* This object holds a Vector of elements and can divide this list
* in pages and blocks of pages. Here is a code example of use in JSPs:
*
* <%!PageBlockBean pageBlock = null;%>
* <%
* try {
* pageBlock = (PageBlockBean) session.getAttribute("PageBlockBean");
* } catch (Exception exc) {
* }
* if (pageBlock == null) {
* pageBlock = new PageBlockBean((Vector) session.getAttribute("someVector"));
* pageBlock.setPageSize(10);
* pageBlock.setBlockSize(5);
* pageBlock.setCurrentPageNumber(0);
* session.setAttribute("PageBlockBean", pageBlock);
* }
* try {
* int currentPage = Integer.parseInt(request.getParameter("currentPage"));
* pageBlock.setCurrentPageNumber(currentPage);
* } catch (Exception exc) {
* }
* previous
* next
*
* @author Daniel Yukio Yokomiso
* @version 1.0 23/03/2001
*/
public class PageBlockBean extends java.util.Vector
implements java.io.Serializable {
/** Number of itens per page.*/
private int pageSize = 10;
/** Number of pages per block.*/
private int blockSize = 5;
/** Page in view.*/
private int currentPageNumber = 0;
/**
* Creates a PageBlockBean with elements from c.
*
* @param c some Collection
*/
public PageBlockBean(java.util.Collection c) {
super(c);
}
/**
* Creates a empty PageBlockBean.
*/
public PageBlockBean() {
}
/**
* Returns current page count.
*
* @return number of pages
*/
public int getPageCount() {
int fullPages = size() / getPageSize();
int partialItens = size() % getPageSize();
return fullPages + ((partialItens > 0) ? 1 : 0);
}
/**
* Returns the current block count.
*
* @return number of blocks
*/
public int getBlockCount() {
int fullBlocks = getPageCount() / getBlockSize();
int partialPages = getPageCount() % getBlockSize();
return fullBlocks + ((partialPages > 0) ? 1 : 0);
}
/**
* Return the page number of item, or -1 if is an invalid item index.
*
* @param item index of item
* @return page of item
*/
public int getPageOfItem(int item) {
if ((item >= 0) && (item < size())) {
return item / getPageSize();
} else {
return -1;
}
}
/**
* Return the block number of page, or -1 if is an invalid page index.
*
* @param page number of page
* @return block of page
*/
public int getBlockOfPage(int page) {
if ((page >= 0) && (page < getPageCount())) {
return page / getBlockSize();
} else {
return -1;
}
}
/**
* Returns the current page itens in a Vector.
*
* @return a Vector with the current page elements
*/
public java.util.Vector getCurrentPage() {
java.util.Vector vector = new java.util.Vector();
int pageBegin = getCurrentPageNumber() * getPageSize();
int pageEnd = (getCurrentPageNumber() + 1) * getPageSize();
for (int i = pageBegin; (i < pageEnd) && (i < size()); i++) {
vector.add(this.get(i));
}
return vector;
}
/**
* Return the number of itens per page.
*
* @return o value of pageSize
*/
public int getPageSize() {
return pageSize;
}
/**
* Defines the number of itens per page. Must be greater than zero, or
* nothing will happen.
*
* @param pageSize new value for pageSize
*/
public void setPageSize(int pageSize) {
if (pageSize > 0) {
this.pageSize = pageSize;
}
}
/**
* Return the number of pages per block.
*
* @return value of blockSize
*/
public int getBlockSize() {
return blockSize;
}
/**
* Defines the number of pages per block. Must be greater than zero, or
* nothing will happen.
*
* @param blockSize new value for blockSize
*/
public void setBlockSize(int blockSize) {
if (blockSize > 0) {
this.blockSize = blockSize;
}
}
/**
* Returns the number of the current viewed block. Information derived from
* current page number.
*
* @return the current block number
*/
public int getCurrentBlock() {
return getCurrentPageNumber() / getBlockSize();
}
/**
* Returns the current page number.
*
* @return value of currentPageNumber
*/
public int getCurrentPageNumber() {
return currentPageNumber;
}
/**
* Defines the current page number. Must be greater than zero, or nothing
* will happen.
*
* @param currentPageNumber new value for currentPageNumber
*/
public void setCurrentPageNumber(int currentPageNumber) {
if (currentPageNumber >= 0) {
this.currentPageNumber = currentPageNumber;
}
}
}