|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectorg.apache.velocity.tools.view.tools.AbstractPagerTool
org.apache.velocity.tools.view.tools.AbstractSearchTool
public abstract class 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 setup(HttpServletRequest) and executeQuery(Object)
methods.
The setup(HttpServletRequest) method ought to extract from the current request the search criteria, the current list index, and optionally, the number of items to display per page of results. Upon extracting these parameters, they should be set using the provided setCriteria(Object), setIndex(int), and setItemsPerPage(int) methods. A simple implementation would be:
public void setup(HttpServletRequest req) { ParameterParser pp = new ParameterParser(req); setCriteria(pp.getString("find")); setIndex(pp.getInt("index", 0)); setItemsPerPage(pp.getInt("show", DEFAULT_ITEMS_PER_PAGE)); }
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 #endThe output of this might look like:
Example toolbox.xml configuration:
<tool> <key>search</key> <scope>request</scope> <class>com.foo.tools.MySearchTool</class> </tool>
Nested Class Summary | |
---|---|
class |
AbstractSearchTool.StoredResults
Simple utility class to hold a criterion and its result list. |
Field Summary | |
---|---|
private java.lang.Object |
criteria
|
protected static java.lang.String |
STORED_RESULTS_KEY
the key under which StoredResults are kept in session |
Fields inherited from class org.apache.velocity.tools.view.tools.AbstractPagerTool |
---|
DEFAULT_ITEMS_PER_PAGE, DEFAULT_SLIP_SIZE, session, STORED_ITEMS_KEY |
Constructor Summary | |
---|---|
AbstractSearchTool()
|
Method Summary | |
---|---|
protected abstract java.util.List |
executeQuery(java.lang.Object criteria)
Executes a query for the specified criteria. |
java.lang.Object |
getCriteria()
Return the criteria object for this request. |
java.util.List |
getItems()
Gets the results for the given criteria either in memory or by performing a new query for them. |
java.util.List |
getResults()
Deprecated. Use AbstractPagerTool.getItems() . |
protected java.util.List |
getStoredItems()
Retrieves stored search items (if any) from the user's session attributes. |
protected AbstractSearchTool.StoredResults |
getStoredResults()
Retrieves stored search results (if any) from the user's session attributes. |
boolean |
hasResults()
Deprecated. Use AbstractPagerTool.hasItems() |
void |
reset()
Sets the criteria and results to null, page index to zero, and items per page to the default. |
void |
setCriteria(java.lang.Object criteria)
Sets the criteria for this search. |
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. |
protected void |
setStoredResults(AbstractSearchTool.StoredResults results)
Stores current search results in the user's session attributes (if one currently exists) in order to do efficient result pagination. |
Methods inherited from class org.apache.velocity.tools.view.tools.AbstractPagerTool |
---|
getFirstIndex, getIndex, getItemsPerPage, getLastIndex, getNextIndex, getPage, getPageDescription, getPageNumber, getPageNumber, getPagesAvailable, getPrevIndex, getSlip, getSlipSize, getTotal, hasItems, init, setIndex, setItems, setItemsPerPage, setSlipSize, setup |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Field Detail |
---|
protected static final java.lang.String STORED_RESULTS_KEY
private java.lang.Object criteria
Constructor Detail |
---|
public AbstractSearchTool()
Method Detail |
---|
public void reset()
reset
in class AbstractPagerTool
public void setCriteria(java.lang.Object criteria)
criteria
- - the criteria used for this searchpublic java.lang.Object getCriteria()
public boolean hasResults()
AbstractPagerTool.hasItems()
public java.util.List getResults()
AbstractPagerTool.getItems()
.
public java.util.List getItems()
getItems
in class AbstractPagerTool
List
of all items for the criteriaprotected java.util.List getStoredItems()
AbstractPagerTool
getStoredItems
in class AbstractPagerTool
List
retrieved from memoryprotected void setStoredItems(java.util.List items)
AbstractPagerTool
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
in class AbstractPagerTool
items
- the List
to be storedprotected abstract java.util.List executeQuery(java.lang.Object criteria)
This method must be implemented! A simple implementation might be something like:
protected List executeQuery(Object crit) { return MyDbUtils.getFooBarsMatching((String)crit); }
List
of results for this queryprotected AbstractSearchTool.StoredResults getStoredResults()
AbstractSearchTool.StoredResults
retrieved from memoryprotected void setStoredResults(AbstractSearchTool.StoredResults results)
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
- the AbstractSearchTool.StoredResults
to be stored
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |