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:
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="">Prev</a> #end #foreach( $index in $search.slip ) #if( $index == $search.index ) <b>$search.pageNumber</b> #else <a href="">$!search.getPageNumber($index)</a> #end #end #if( $search.nextIndex ) <a href="">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:Showing 1-5 of 8
1. foo
2. bar
3. blah
4. woogie
5. baz
1 2 Next
Example toolbox.xml configuration:
<tools> <toolbox scope="request"> <tool class="com.foo.tools.MySearchTool"/> </toolbox> </tools>