Creating a User Interface for a Search Service - Changes to the Original Code to Fit the JSP
(Page 2 of 4 )
JSP reflects on fields to hook up properties to <out> display tags instead of getters and setters. Unfortunately, our original implementation of HitBean marked all of its data private and only exposed getters and setters (normally, the appropriate strategy). Since we now have to have the fields exposed directly, we need to make a simple change to the Spider. The original class started with these declarations:
final String url;
final String title;
final float score;
It now has to become:
public final String url;
public final String title;
public final float score;
What if We Don’t Have the Spider Source?
It is instructive to examine what happens when we aren’t the original authors of either the application we are extending (jPetStore) or the service we are integrating (Simple Spider). If we don’t have access to the source code of either project, we can still make the extension we’ve been working on. For the jPetStore, all we did was modify a configuration file and a JSP (which we always have the source for) and add a new class.
If we don’t have access to the original source for theHitBeanclass, how can we make it work with the JSP? The answer is simple: write a wrapper class that exposes the correct properties (or just use the already exposed web service interface):
public class HitBeanWrapper {
private HitBean _hitbean;
public String url;
public String title;
public float score;
public HitBeanWrapper(HitBean hitbean)
{
_hitbean = hitbean;
url = hitbean.getUrl();
title = hitbean.getTitle();
score = hitbean.getScore();
}
public String getScoreAsString() {
return _hitbean.getScoreAsString();
}
}
This requires a change to thehandleRequestmethod of theSearchPagesController, as well:
HashMap hits = new HashMap(qb.getResults().length);
for(int i =0;i<qb.getResults().length;i++)
{
hits.put("hits", new HitBeanWrapper(qb.getResults()[i]));
}
return new ModelAndView("SearchProducts", hits);
That’s it. We’ve edited the Spider all we need to in order to incorporate it into thejPetStoreapplication.
Principles in Action
- Keep it simple: display the URL to result pages instead of complex rendering of product information; use simple table output instead of PagedListHolder (the need for it was gone)
- Choose the right tools: table, not PagedListHolder; JSP
- Do one thing, and do it well: JSP focuses on display of output, not search intricacies
- Strive for transparency:HitBean exposes simple data properties; use a wrapper forHitBeanif the source is not available
- Allow for extension: none
Next: Setting Up the Indexer >>
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.
|
|