SearchFormHandler
executes its search query when the handleSearch
method is called. Typically, you associate the form handler’s search
property with a submit button, as in this example:
<dsp:input bean="/myCatalog/SearchForm.search" value="Go" type="submit"/>
After executing the query, SearchFormHandler
makes the search results available in two different properties, which contain the same information but organize it differently:
The
searchResults
property is a Collection of all catalog items that satisfied the search criteria. If you search for multiple item types (such as categories and products) all items returned by the search appear in the list regardless of their type.The
searchResultsByItemType
property is a HashMap containing one key/value pair for each item type you searched for. The key is the item type name (the value specified in the form handler’sitemTypes
property), and the value is a Collection of items of that type that satisfied the search criteria.
For example, if you search for categories and products in the standard catalog schema, the searchResultsByItemType
property will have a key called category
whose value is a Collection of matching categories, and another key called product
whose value is a Collection of matching products. The searchResults
property will have a Collection in which some of the items are categories and some of the items are products.
Within each Collection, the items are not sorted, but reflect the order they were retrieved from the database. You can use the sorting capabilities of a servlet bean (such as ForEach
) to control the order in which the items are displayed.
The following example uses ForEach
with searchResultsByItemType
to display only the products returned by the search, sorted by display name:
Your search returned the following products: <dsp:droplet name="ForEach"> <dsp:param value="CatalogSearch.searchResultsByItemType.product" name="array"/> <dsp:param value="+displayName" name="sortProperties"/> <dsp:oparam name="output"> <li><dsp:valueof param="element.displayName">Unknown product</dsp:valueof> </dsp:oparam> <dsp:oparam name="empty"> <p>No matching products were found. </dsp:oparam> </dsp:droplet>