类 PagerTool
- 直接已知子类:
AbstractSearchTool
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.
#end
The output of this might look like:
Showing 1-5 of 8<br> 1. foo<br> 2. bar<br> 3. blah<br> 4. woogie<br> 5. baz<br><br> <b>1</b> <a href="">2</a> <a href="">Next</a>
Example tools.xml configuration:
<tools>
<toolbox scope="request">
<tool class="org.apache.velocity.tools.view.PagerTool"/>
</toolbox>
</tools>
- 从以下版本开始:
- VelocityTools 2.0
- 版本:
- $Revision$ $Date$
- 作者:
- Nathan Bubna
-
字段概要
字段修饰符和类型字段说明static final Stringstatic final intthe default number of items shown per pagestatic final Stringstatic final Stringstatic final intthe default max number of page indices to liststatic final Stringprotected jakarta.servlet.http.HttpSessionprotected static final Stringthe key under which items are stored in session从类继承的字段 org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明booleanReturns 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).intgetIndex()Returns the current search result index.getItems()Returns the item list.intReturns the set number of items to be displayed per page of itemsReturns 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).Returns the index for the next page of items (as determined by the current index, items per page, and the number of items).getPage()Returns the current "page" of search items.Returns a description of the current page.Returns the "page number" for the current index.getPageNumber(int i) Returns the "page number" for the specified index.intReturns the number of pages that can be made from this list given the set number of items per page.Return the index for the previous page of items (as determined by the current index, items per page, and the number of items).getSlip()Returns a Sliding List of Indices for Pages of items.intReturns the number of result page indicesgetSlip()will return per request (if available).protected ListRetrieves stored search items (if any) from the user's session attributes.intgetTotal()Returns the total number of items available.booleanhasItems()Checks whether or not the result list is empty.voidreset()Sets the item list to null, page index to zero, and items per page to the default.voidsetCreateSession(boolean createSession) voidsetIndex(int index) Sets the index of the first result in the current pagevoidsetIndexKey(String key) voidSets the List to page through.voidsetItemsPerPage(int itemsPerPage) Sets the number of items returned in a page of itemsvoidsetItemsPerPageKey(String key) voidsetNewItemsKey(String key) voidsetRequest(jakarta.servlet.http.HttpServletRequest request) Initializes this tool with the specifiedHttpServletRequest.voidsetSlipSize(int slipSize) Sets the number of result page indices forgetSlip()to list.voidsetSlipSizeKey(String key) protected voidsetStoredItems(List items) Stores current search items in the user's session attributes (if one currently exists) in order to do efficient result pagination.voidsetup(jakarta.servlet.http.HttpServletRequest request) Sets the index, itemsPerPage, and/or slipSize *if* they are set in the request parameters.从类继承的方法 org.apache.velocity.tools.generic.SafeConfig
configure, configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
字段详细资料
-
DEFAULT_NEW_ITEMS_KEY
- 另请参阅:
-
DEFAULT_INDEX_KEY
- 另请参阅:
-
DEFAULT_ITEMS_PER_PAGE_KEY
- 另请参阅:
-
DEFAULT_SLIP_SIZE_KEY
- 另请参阅:
-
DEFAULT_ITEMS_PER_PAGE
public static final int DEFAULT_ITEMS_PER_PAGEthe default number of items shown per page- 另请参阅:
-
DEFAULT_SLIP_SIZE
public static final int DEFAULT_SLIP_SIZEthe default max number of page indices to list- 另请参阅:
-
STORED_ITEMS_KEY
the key under which items are stored in session -
session
protected jakarta.servlet.http.HttpSession session
-
-
构造器详细资料
-
PagerTool
public PagerTool()
-
-
方法详细资料
-
setRequest
public void setRequest(jakarta.servlet.http.HttpServletRequest request) Initializes this tool with the specifiedHttpServletRequest. This is required for this tool to operate and will throw a NullPointerException if this is not set or is set tonull.- 参数:
request- servlet request
-
setup
public void setup(jakarta.servlet.http.HttpServletRequest request) Sets the index, itemsPerPage, and/or slipSize *if* they are set in the request parameters. Likewise, this will set the item list to be paged *if* there is a list pushed into the request attributes under thegetNewItemsKey().- 参数:
request- the current HttpServletRequest
-
setNewItemsKey
-
getNewItemsKey
-
setIndexKey
-
getIndexKey
-
setItemsPerPageKey
-
getItemsPerPageKey
-
setSlipSizeKey
-
getSlipSizeKey
-
setCreateSession
public void setCreateSession(boolean createSession) -
getCreateSession
public boolean getCreateSession() -
reset
public void reset()Sets the item list to null, page index to zero, and items per page to the default. -
setItems
Sets the List to page through.- 参数:
items- - theListof items to be paged through
-
setIndex
public void setIndex(int index) Sets the index of the first result in the current page- 参数:
index- the result index to start the current page with
-
setItemsPerPage
public void setItemsPerPage(int itemsPerPage) Sets the number of items returned in a page of items- 参数:
itemsPerPage- the number of items to be returned per page
-
setSlipSize
public void setSlipSize(int slipSize) Sets the number of result page indices forgetSlip()to list. (for google-ish result page links).- 参数:
slipSize- - the number of result page indices to list- 另请参阅:
-
getItemsPerPage
public int getItemsPerPage()Returns the set number of items to be displayed per page of items- 返回:
- current number of items shown per page
-
getSlipSize
public int getSlipSize()Returns the number of result page indicesgetSlip()will return per request (if available).- 返回:
- the number of result page indices
getSlip()will try to return
-
getIndex
public int getIndex()Returns the current search result index.- 返回:
- the index for the beginning of the current page
-
hasItems
public boolean hasItems()Checks whether or not the result list is empty.- 返回:
trueif the result list is not empty.
-
getItems
Returns the item list. This is guaranteed to never returnnull.- 返回:
Listof all the items
-
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). If there is no current page, then null is returned.- 返回:
- index for the last item on this page or
nullif none exists - 从以下版本开始:
- VelocityTools 1.3
-
getNextIndex
Returns the index for the next page of items (as determined by the current index, items per page, and the number of items). If no "next page" exists, then null is returned.- 返回:
- index for the next page or
nullif none exists
-
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). If there is no current page, then null is returned. This is different thangetIndex()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.- 返回:
- index for the first item on this page or
nullif none exists - 从以下版本开始:
- VelocityTools 1.3
-
getPrevIndex
Return the index for the previous page of items (as determined by the current index, items per page, and the number of items). If no "next page" exists, then null is returned.- 返回:
- index for the previous page or
nullif none exists
-
getPagesAvailable
public int getPagesAvailable()Returns the number of pages that can be made from this list given the set number of items per page.- 返回:
- number of available pages
-
getPage
Returns the current "page" of search items.- 返回:
- a
Listof items for the "current page"
-
getPageNumber
Returns the "page number" for the specified index. Because the page number is used for the user interface, the page numbers are 1-based.- 参数:
i- the index that you want the page number for- 返回:
- the approximate "page number" for the specified index or
nullif there are no items
-
getPageNumber
Returns the "page number" for the current index. Because the page number is used for the user interface, the page numbers are 1-based.- 返回:
- the approximate "page number" for the current index or
nullif there are no items
-
getTotal
public int getTotal()Returns the total number of items available.- 返回:
- number of items
- 从以下版本开始:
- VelocityTools 1.3
-
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).
- 返回:
- a description of the current page
-
getSlip
Returns a Sliding List of Indices for Pages of items.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)).- 返回:
Listof Integers representing the indices of result pages or empty list if there's one or less pages available
-
getStoredItems
Retrieves stored search items (if any) from the user's session attributes.- 返回:
- the
Listretrieved from memory
-
setStoredItems
Stores current search items in the user's session attributes (if one currently exists) in order to do efficient result pagination.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- theListto be stored
-