Creating a User Interface for a Search Service
(Page 1 of 4 )
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).
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:
<%@ include file="IncludeTop.jsp" %>
<table align="left" bgcolor="#008800" border="0" cellspacing="2" cellpadding="2">
<tr>
<td bgcolor="#FFFF88">
<a href="<c:url value="/shop/index.do"/>">
<b><font color="BLACK" size="2"> << Main Menu</font></b>
</a>
</td>
</tr>
</table>
<table align="center" bgcolor="#008800" border="0" cellspacing="2"
cellpadding="3">
<tr bgcolor="#CCCCCC"> <td><b>URL</b></td> <td><b>Rank</b></td> </tr>
<c:forEach var="page" items="${hits}">
<tr bgcolor="#FFFF88">
<td><a href="<c:out value="${page.url}"/>">
<c:out value="${page.url}"/></a>
</td>
<td>
<c:out value="${page.score}"/>
</td>
</tr>
</c:forEach>
</table>
<%@ include file="IncludeBottom.jsp" %>
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.
Next: Changes to the Original Code to Fit the JSP >>
More Java Articles
More By O'Reilly Media
|
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). Check it out today at your favorite bookstore. Buy this book now.
|
|