The ability to search a website can be invaluable to visitors. Even if you have a website that searches a product database, your visitors might want to search the contents of your entire site. This article helps you set up such a service. It is excerpted from chapter 10 of the book Better, Faster, Lighter Java, written by Bruce A. Tate and Justin Gehtland (O'Reilly; ISBN: 0596006764).
Adding a Search Service to a Java Application - Registering Our New Class with jPetStore (Page 4 of 4 )
Finally, we alert jPetStore to the new controller’s existence. IfjPetStoreis not coded for extensibility, we have to modify the application code in order to get it to work. For instance, if there are methods ofjPetStorethat create instances ofSearchProductsControllerdirectly, we must change each of those lines to create aSearchPagesControllerinstead.
It turns out, however, thatjPetStoreis quite ready for extensibility—partly because it is based on the Spring framework. In order to telljPetStoreabout our new controller, we modify a single configuration file (petstore-servlets.xml). This file tells Spring what objects to create and how to wire them together to make a sensible application. Now, we just need to find the configuration setting used to launch theSearchProductsControllerand point it to our newSearchPagesControllerinstead.
We’re telling the application to map requests for “/shop/searchProducts.do” to a new instance ofSearchPagesController. At the same time, we tell it, provide theSearchPagesControllerwith the current instance ofpetStore(in a property calledpetStore).
Principles in action
Keep it simple: the controller logic is a simple invocation of Spider; the controller interface is very simple (one method)
Choose the right tools: Spring and the Spider
Do one thing and do it well: since the Spider is so well-encapsulated, it’s easy to add to an existing service; the controller deals with invoking the Spider and the JSP only needs to display the results—MVC pattern well-demonstrated
Strive for transparency: the site doesn’t care how it is indexed; it can easily switch between data-driven and HTML-driven search technologies
Allow for extension: we quickly expanded our search capabilities by adding a new tool with minimal code; the configuration abilities of jPetStore allow for no-code recognition of new service
Please check back next week for the continuation of this article.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.