Last week, we started building a search service for a website. This week, we're going to cover the user interface and the indexer. This article is excerpted from chapter 10 of the book Better, Faster, Lighter Java, written by Bruce A. Tate and Justin Gehtland (O'Reilly; ISBN: 0596006764).
Creating a User Interface for a Search Service (Page 1 of 4 )
The User Interface (JSP)
The user interface is fairly straightforward. Instead of just dumping our results to the console or creating an XML document of the results (as in the web service implementation from Chapter 9), this time we need to write a JSP that iterates over the results and displays them as hyperlinks in a table.
The originaljPetStore search feature used aPagedListHolderfor its results because it displayed the image associated with each returned product in the table. Since the images were arbitrary in size,jPetStoredidn’t want to display too many entries on a given page since it might result in a lot of vertical scrolling. Our results consist of a hyperlink to the returned URL and the relative rank of the given result; therefore, we’ll use a simple table to display our results.
Again, we are faced with the rewrite-or-replace question. Just like last time, we have three questions to consider:
Do we have access to the original source? We must, since JSPs are just text files in both development and deployment mode.
Will we ever want to reuse the existing service? We do, but in this case, a JSP is so easy to recreate that it won’t make much difference.
Does the current version implement some standard interface? Not as such, since JSPs are just mixes of static HTML and dynamic content.
Because of the rather trivial nature of the changes and because JSPs are easily edited in place (no compilation necessary), we’ll just repurpose the existing SearchProducts.jsp file. This strategy saves us from having to change any more configuration settings:
The JSP files in the application have a standard header and footer defined in IncludeTop.jsp and IncludeBottom.jsp. All we have to do is render the results in between the include directives. Start by creating a JSP-styleforEachloop, with an enumerator calledpagepointing at each member of theHashMapcalled “hits.” For each hit, we render a table row containing the URL (the value of which is both the text to display and the HREF to point it to) and the relative rank of the hit. JSP handles hooking up the variables and properties using reflection. However, when implementing this page, we come across the first (and only) reason to change some of the original Spider code.