Portlets, Tools, AJAX, and more...
GregZ Weblog
Archives
« December 2009
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
  
       
Today
XML
Search

Links
 

Today's Page Hits: 4

All | Everything
« Portal Server 7... | Main | AJAXPortlet Revisite... »
20060117 Tuesday January 17, 2006
GoogleSearch Portlet
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.

posted by gregz Jan 17 2006, 05:29:06 PM MST Permalink Comments [2]

Trackback URL: http://blogs.sun.com/gregz/entry/googlesearch_portlet
Comments:

Nice article. Where is the source/project for the Google portlet ? Thanks. Peter

Posted by Peter Kasson on March 09, 2006 at 09:57 AM MST #

There is not any source available for download with this tutorial, but if you walk through the steps in the original tutorial (http://developers.sun.com/prodtech/javatools/jscreator/learning/tutorials/2/googleportlet.html) it is really easy to re-create the sample. You can then use these steps on the blog to modify the sample to do Google search.

Posted by GregZ on March 10, 2006 at 11:24 AM MST #

Post a Comment:

Name:
E-Mail:
URL:

Your Comment:

HTML Syntax: NOT allowed