Home arrow Java arrow Page 2 - Creating a User Interface for a Search Service
JAVA

Creating a User Interface for a Search Service


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).

Author Info:
By: O'Reilly Media
Rating: 4 stars4 stars4 stars4 stars4 stars / 4
November 22, 2006
TABLE OF CONTENTS:
  1. · Creating a User Interface for a Search Service
  2. · Changes to the Original Code to Fit the JSP
  3. · Setting Up the Indexer
  4. · Making Use of the Configuration Service

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
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


blog comments powered by Disqus
JAVA ARTICLES

- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 2 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials