|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object org.apache.velocity.tools.view.PagerTool
@DefaultKey(value="pager") @InvalidScope(value={"application","session"}) public class PagerTool
View tool for doing request-based pagination of items in an a list.
Usage:
To use this class, you typically push a List of items to it
by putting it in the request attributes under the value returned by
getNewItemsKey()
(default is "new.items").
You can also set the list of items to be paged in a subclass
using the setItems(List) method, or you can always set the
item list at another point (even from within the template). This
need only happen once per session if a session is available, but the
item list can be (re)set as often as you like.
Here's an example of how your subclass would be used in a template:
#if( $pager.hasItems() ) Showing $!pager.pageDescription<br> #set( $i = $pager.index ) #foreach( $item in $pager.page ) ${i}. $!item <br> #set( $i = $i + 1 ) #end <br> #if ( $pager.pagesAvailable > 1 ) #set( $pagelink = $link.self.param("show",$!pager.itemsPerPage) ) #if( $pager.prevIndex ) <a href="$pagelink.param('index',$!pager.prevIndex)">Prev</a> #end #foreach( $index in $pager.slip ) #if( $index == $pager.index ) <b>$pager.pageNumber</b> #else <a href="$pagelink.param('index',$!index)">$!pager.getPageNumber($index)</a> #end #end #if( $pager.nextIndex ) <a href="$pagelink.param('index',$!pager.nextIndex)">Next</a> #end #end #else No items in list. #endThe output of this might look like:
Example tools.xml configuration:
<tools> <toolbox scope="request"> <tool class="org.apache.velocity.tools.view.PagerTool"/> </toolbox> </tools>
Field Summary | |
---|---|
private boolean |
createSession
|
static java.lang.String |
DEFAULT_INDEX_KEY
|
static int |
DEFAULT_ITEMS_PER_PAGE
the default number of items shown per page |
static java.lang.String |
DEFAULT_ITEMS_PER_PAGE_KEY
|
static java.lang.String |
DEFAULT_NEW_ITEMS_KEY
|
static int |
DEFAULT_SLIP_SIZE
the default max number of page indices to list |
static java.lang.String |
DEFAULT_SLIP_SIZE_KEY
|
private int |
index
|
private java.lang.String |
indexKey
|
private java.util.List |
items
|
private int |
itemsPerPage
|
private java.lang.String |
itemsPerPageKey
|
private java.lang.String |
newItemsKey
|
protected javax.servlet.http.HttpSession |
session
|
private int |
slipSize
|
private java.lang.String |
slipSizeKey
|
protected static java.lang.String |
STORED_ITEMS_KEY
the key under which items are stored in session |
Constructor Summary | |
---|---|
PagerTool()
|
Method Summary | |
---|---|
boolean |
getCreateSession()
|
java.lang.Integer |
getFirstIndex()
Returns the index of the first item on the current page of results (as determined by the current index, items per page, and the number of items). |
int |
getIndex()
Returns the current search result index. |
java.lang.String |
getIndexKey()
|
java.util.List |
getItems()
Returns the item list. |
int |
getItemsPerPage()
Returns the set number of items to be displayed per page of items |
java.lang.String |
getItemsPerPageKey()
|
java.lang.Integer |
getLastIndex()
Returns the index of the last item on the current page of results (as determined by the current index, items per page, and the number of items). |
java.lang.String |
getNewItemsKey()
|
java.lang.Integer |
getNextIndex()
Returns the index for the next page of items (as determined by the current index, items per page, and the number of items). |
java.util.List |
getPage()
Returns the current "page" of search items. |
java.lang.String |
getPageDescription()
Returns a description of the current page. |
java.lang.Integer |
getPageNumber()
Returns the "page number" for the current index. |
java.lang.Integer |
getPageNumber(int i)
Returns the "page number" for the specified index. |
int |
getPagesAvailable()
Returns the number of pages that can be made from this list given the set number of items per page. |
java.lang.Integer |
getPrevIndex()
Return the index for the previous page of items (as determined by the current index, items per page, and the number of items). |
java.util.List |
getSlip()
Returns a Sliding List of Indices for Pages of items. |
int |
getSlipSize()
Returns the number of result page indices getSlip()
will return per request (if available). |
java.lang.String |
getSlipSizeKey()
|
protected java.util.List |
getStoredItems()
Retrieves stored search items (if any) from the user's session attributes. |
int |
getTotal()
Returns the total number of items available. |
boolean |
hasItems()
Checks whether or not the result list is empty. |
void |
reset()
Sets the item list to null, page index to zero, and items per page to the default. |
void |
setCreateSession(boolean createSession)
|
void |
setIndex(int index)
Sets the index of the first result in the current page |
void |
setIndexKey(java.lang.String key)
|
void |
setItems(java.util.List items)
Sets the List to page through. |
void |
setItemsPerPage(int itemsPerPage)
Sets the number of items returned in a page of items |
void |
setItemsPerPageKey(java.lang.String key)
|
void |
setNewItemsKey(java.lang.String key)
|
void |
setRequest(javax.servlet.http.HttpServletRequest request)
Initializes this tool with the specified HttpServletRequest . |
void |
setSlipSize(int slipSize)
Sets the number of result page indices for getSlip() to list. |
void |
setSlipSizeKey(java.lang.String key)
|
protected void |
setStoredItems(java.util.List items)
Stores current search items in the user's session attributes (if one currently exists) in order to do efficient result pagination. |
void |
setup(javax.servlet.http.HttpServletRequest request)
Sets the index, itemsPerPage, and/or slipSize *if* they are set in the request parameters. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
public static final java.lang.String DEFAULT_NEW_ITEMS_KEY
public static final java.lang.String DEFAULT_INDEX_KEY
public static final java.lang.String DEFAULT_ITEMS_PER_PAGE_KEY
public static final java.lang.String DEFAULT_SLIP_SIZE_KEY
public static final int DEFAULT_ITEMS_PER_PAGE
public static final int DEFAULT_SLIP_SIZE
protected static final java.lang.String STORED_ITEMS_KEY
private java.lang.String newItemsKey
private java.lang.String indexKey
private java.lang.String itemsPerPageKey
private java.lang.String slipSizeKey
private boolean createSession
private java.util.List items
private int index
private int slipSize
private int itemsPerPage
protected javax.servlet.http.HttpSession session
Constructor Detail |
---|
public PagerTool()
Method Detail |
---|
public void setRequest(javax.servlet.http.HttpServletRequest request)
HttpServletRequest
.
This is required for this tool to operate and will throw a
NullPointerException if this is not set or is set to null
.
public void setup(javax.servlet.http.HttpServletRequest request)
getNewItemsKey()
.
request
- the current HttpServletRequestpublic void setNewItemsKey(java.lang.String key)
public java.lang.String getNewItemsKey()
public void setIndexKey(java.lang.String key)
public java.lang.String getIndexKey()
public void setItemsPerPageKey(java.lang.String key)
public java.lang.String getItemsPerPageKey()
public void setSlipSizeKey(java.lang.String key)
public java.lang.String getSlipSizeKey()
public void setCreateSession(boolean createSession)
public boolean getCreateSession()
public void reset()
public void setItems(java.util.List items)
items
- - the List
of items to be paged throughpublic void setIndex(int index)
index
- the result index to start the current page withpublic void setItemsPerPage(int itemsPerPage)
itemsPerPage
- the number of items to be returned per pagepublic void setSlipSize(int slipSize)
getSlip()
to list.
(for google-ish result page links).
slipSize
- - the number of result page indices to listgetSlip()
public int getItemsPerPage()
public int getSlipSize()
getSlip()
will return per request (if available).
getSlip()
will try to returnpublic int getIndex()
public boolean hasItems()
true
if the result list is not empty.public java.util.List getItems()
null
.
List
of all the itemspublic java.lang.Integer getLastIndex()
null
if none existspublic java.lang.Integer getNextIndex()
null
if none existspublic java.lang.Integer getFirstIndex()
getIndex()
in that it
is adjusted to fit the reality of the items available and is not a
mere accessor for the current, user-set index value.
null
if none existspublic java.lang.Integer getPrevIndex()
null
if none existspublic int getPagesAvailable()
public java.util.List getPage()
List
of items for the "current page"public java.lang.Integer getPageNumber(int i)
i
- the index that you want the page number for
null
if there are no itemspublic java.lang.Integer getPageNumber()
null
if there are no itemspublic int getTotal()
public java.lang.String getPageDescription()
Returns a description of the current page. This implementation displays a 1-based range of result indices and the total number of items. (e.g. "1 - 10 of 42" or "7 of 7") If there are no items, this will return "0 of 0".
Sub-classes may override this to provide a customized description (such as one in another language).
public java.util.List getSlip()
Essentially, this returns a list of item indices that correspond to available pages of items (as based on the set items-per-page). This makes it relativly easy to do a google-ish set of links to available pages.
Note that this list of Integers is 0-based to correspond with the
underlying result indices and not the displayed page numbers (see
getPageNumber(int)
).
List
of Integers representing the indices of result
pages or empty list if there's one or less pages availableprotected java.util.List getStoredItems()
List
retrieved from memoryprotected void setStoredItems(java.util.List items)
Override this to store search items somewhere besides the HttpSession or to prevent storage of items across requests. In the former situation, you must also override getStoredItems().
items
- the List
to be stored
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |