The new tutorial,
"Building a GoogleSearch Web Service Portlet", outlines how to use the Google Search web service bundled with Java Studio Creator. For simplicity, the tutorial focuses on using the
doSpellingSuggestion method to return a spelling suggestion for the input text using the Google Search API. This example can be expanded to use the more popular search feature of the Google APIs (
doSearch).
The
PortletPage1 can be re-used for the most part. Change the button to say "Search". Change session property to "query". Instead of calling the web service on page 1 and binding the result to the session bean, we will bind the query request that is input into the text field and pass it to page 2. On page 2 in
init, we call
doGoogleSearch, passing your Google license key and the query string from page 1. We can populate an "Object Array Data Provider" with the array of results the method returns. For instance,
try {
GoogleSearchResult result =
this.getGoogleSearchClient1().doGoogleSearch(
"your_Google_key", // Replace with your Google Web APIs
// license key, in quotes
getSessionBean1().getQuery(),
0, 10, false, null, true, null, null, null);
objectArrayDataProvider1.setArray(result.getResultElements());
} catch (Exception e) {
error(e.getMessage());
log("Page2 failure with GoogleSearch web service", e);
}
|
To learn more about all the required parameters on the
doGoogleSearch method, see the
Google Search API reference.
Then in the JSP, we can use the
objectArrayDataProvider1 to populate a table of our results. For instance,
<ui:tablerowgroup binding="#{PortletPage2.tableRowGroup1}" id="tableRowGroup1" rows="3" sourcedata="#{PortletPage2.<b>objectArrayDataProvider1</b>}" sourcevar="currentRow"> <ui:tablecolumn binding="#{PortletPage2.tableColumn1}" headertext="Link" id="tableColumn1"> <ui:hyperlink binding="#{PortletPage2.hyperlink1}" id="hyperlink1" text="#{currentRow.value['<b>title</b>']}" url="#{currentRow.value['<b>URL</b>']}"> </ui:hyperlink> <ui:tablecolumn binding="#{PortletPage2.tableColumn2}" headertext="About" id="tableColumn2"> <ui:statictext binding="#{PortletPage2.staticText2}" id="staticText2" text="#{currentRow.value['<b>snippet</b>']}"> </ui:statictext> </ui:tablecolumn> </ui:tablecolumn> </ui:tablerowgroup> |
The table will display a hyperlink component constructed using the
URL and
title fields from each
ResultElement and a static text component with the
snippet description. Here, we have set the
maxResults at 10 (could be set via a preference). And you will probably want to enable pagination on the table (context menu, Table Layout -> Options). For a search of "Studio Creator", your results page might look like:

There are other fields returned for each
ResultElement that may be interesting to add to your results page. Read up on the
Google Search API reference to learn more.
Trackback URL: http://blogs.sun.com/gregz/entry/googlesearch_portlet
Posted by Peter Kasson on March 09, 2006 at 09:57 AM MST #
Posted by GregZ on March 10, 2006 at 11:24 AM MST #