类 AbstractSearchTool
Abstract view tool for doing "searching" and robust pagination of search results. The goal here is to provide a simple and uniform API for "search tools" that can be used in velocity templates (or even a standard Search.vm template). In particular, this class provides good support for result pagination and some very simple result caching.
Usage:
To use this class, you must extend it and implement
the executeQuery(Object) method.
The setCriteria(Object) method takes an Object in order to allow the search criteria to meet your needs. Your criteria may be as simple as a single string, an array of strings, or whatever you like. The value passed into this method is that which will ultimately be passed into executeQuery(Object) to perform the search and return a list of results. A simple implementation might be like:
protected List executeQuery(Object crit)
{
return MyDbUtils.getFooBarsMatching((String)crit);
}
Here's an example of how your subclass would be used in a template:
<form name="search" method="get" action="$link.setRelative('search.vm')">
<input type="text"name="find" value="$!search.criteria">
<input type="submit" value="Find">
</form>
#if( $search.hasItems() )
Showing $!search.pageDescription<br>
#set( $i = $search.index )
#foreach( $item in $search.page )
${i}. $!item <br>
#set( $i = $i + 1 )
#end
<br>
#if ( $search.pagesAvailable > 1 )
#set( $pagelink = $link.setRelative('search.vm').addQueryData("find",$!search.criteria).addQueryData("show",$!search.itemsPerPage) )
#if( $search.prevIndex )
<a href="$pagelink.addQueryData('index',$!search.prevIndex)">Prev</a>
#end
#foreach( $index in $search.slip )
#if( $index == $search.index )
<b>$search.pageNumber</b>
#else
<a href="$pagelink.addQueryData('index',$!index)">$!search.getPageNumber($index)</a>
#end
#end
#if( $search.nextIndex )
<a href="$pagelink.addQueryData('index',$!search.nextIndex)">Next</a>
#end
#end
#elseif( $search.criteria )
Sorry, no matches were found for "$!search.criteria".
#else
Please enter a search term
#end
The output of this might look like:
<form method="get" action="">
<input type="text" value="foo">
<input type="submit" value="Find">
</form>
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="com.foo.tools.MySearchTool"/>
</toolbox>
</tools>
- 从以下版本开始:
- VelocityTools 2.0
- 版本:
- $Revision$ $Date$
- 作者:
- Nathan Bubna
-
嵌套类概要
嵌套类修饰符和类型类说明static classSimple utility class to hold a criterion and its result list. -
字段概要
字段修饰符和类型字段说明static final Stringprotected static final Stringthe key under which StoredResults are kept in session从类继承的字段 org.apache.velocity.tools.view.PagerTool
DEFAULT_INDEX_KEY, DEFAULT_ITEMS_PER_PAGE, DEFAULT_ITEMS_PER_PAGE_KEY, DEFAULT_NEW_ITEMS_KEY, DEFAULT_SLIP_SIZE, DEFAULT_SLIP_SIZE_KEY, session, STORED_ITEMS_KEY从类继承的字段 org.apache.velocity.tools.generic.SafeConfig
LOCK_CONFIG_KEY, log, LOGGER_NAME_KEY, SAFE_MODE_KEY, USE_CLASS_LOGGER_KEY -
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected abstract ListexecuteQuery(Object criteria) Executes a query for the specified criteria.Return the criteria object for this request.getItems()Gets the results for the given criteria either in memory or by performing a new query for them.protected ListRetrieves stored search items (if any) from the user's session attributes.protected AbstractSearchTool.StoredResultsRetrieves stored search results (if any) from the user's session attributes.voidreset()Sets the criteria and results to null, page index to zero, and items per page to the default.voidsetCriteria(Object criteria) Sets the criteria for this search.voidsetCriteriaKey(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.protected voidStores current search results 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 criteria *if* it is set in the request parameters.从类继承的方法 org.apache.velocity.tools.view.PagerTool
getCreateSession, getFirstIndex, getIndex, getIndexKey, getItemsPerPage, getItemsPerPageKey, getLastIndex, getNewItemsKey, getNextIndex, getPage, getPageDescription, getPageNumber, getPageNumber, getPagesAvailable, getPrevIndex, getSlip, getSlipSize, getSlipSizeKey, getTotal, hasItems, setCreateSession, setIndex, setIndexKey, setItems, setItemsPerPage, setItemsPerPageKey, setNewItemsKey, setRequest, setSlipSize, setSlipSizeKey从类继承的方法 org.apache.velocity.tools.generic.SafeConfig
configure, configure, getLog, initLogger, isConfigLocked, isSafeMode, setLockConfig, setSafeMode
-
字段详细资料
-
DEFAULT_CRITERIA_KEY
- 另请参阅:
-
STORED_RESULTS_KEY
the key under which StoredResults are kept in session
-
-
构造器详细资料
-
AbstractSearchTool
public AbstractSearchTool()
-
-
方法详细资料
-
setup
public void setup(jakarta.servlet.http.HttpServletRequest request) Sets the criteria *if* it is set in the request parameters. -
setCriteriaKey
-
getCriteriaKey
-
reset
public void reset()Sets the criteria and results to null, page index to zero, and items per page to the default. -
setCriteria
Sets the criteria for this search.- 参数:
criteria- - the criteria used for this search
-
getCriteria
Return the criteria object for this request. (for a simple search mechanism, this will typically be just a java.lang.String)- 返回:
- criteria object
-
getItems
Gets the results for the given criteria either in memory or by performing a new query for them. If the criteria is null, an empty list will be returned. -
getStoredItems
从类复制的说明:PagerToolRetrieves stored search items (if any) from the user's session attributes.- 覆盖:
getStoredItems在类中PagerTool- 返回:
- the
Listretrieved from memory
-
setStoredItems
从类复制的说明:PagerToolStores 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().
- 覆盖:
setStoredItems在类中PagerTool- 参数:
items- theListto be stored
-
executeQuery
Executes a query for the specified criteria.This method must be implemented! A simple implementation might be something like:
protected List executeQuery(Object crit) { return MyDbUtils.getFooBarsMatching((String)crit); }- 参数:
criteria- search criteria- 返回:
- a
Listof results for this query
-
getStoredResults
Retrieves stored search results (if any) from the user's session attributes.- 返回:
- the
AbstractSearchTool.StoredResultsretrieved from memory
-
setStoredResults
Stores current search results in the user's session attributes (if one currently exists) in order to do efficient result pagination.Override this to store search results somewhere besides the HttpSession or to prevent storage of results across requests. In the former situation, you must also override getStoredResults().
- 参数:
results- theAbstractSearchTool.StoredResultsto be stored
-