dyerac  
          dyerac In Java
          公告

          日歷
          <2007年7月>
          24252627282930
          1234567
          891011121314
          15161718192021
          22232425262728
          2930311234
          統計
          • 隨筆 - 36
          • 文章 - 10
          • 評論 - 94
          • 引用 - 0

          導航

          常用鏈接

          留言簿(5)

          隨筆分類(49)

          隨筆檔案(36)

          文章分類(11)

          文章檔案(10)

          相冊

          dyerac

          搜索

          •  

          積分與排名

          • 積分 - 79498
          • 排名 - 705

          最新隨筆

          最新評論

          閱讀排行榜

          評論排行榜

           


          題記:
             其實我這篇文章純粹是拋磚引玉之意
             Google Ajax Search 的api使用起來并不困難,如果有高手對此不屑一顧的話,不妨回答一下我的真正用意,那就是一個出色的web api該如何設計呢? 它的體系架構是什么? 我對此有個初步的想法, 前端開發自己的js庫, 調用遠端的服務. 但是具體實施該如何呢? 傳輸方式該是如何? JSON? 自定義XML? 還是SOAP? 現在很火的REST對 web api的設計有什么影響. 還望各位高手賜教 ^_^

          回到正題,看看如何用google api構建自己的ajax 搜索.

          先來看看官方介紹:

          The Google AJAX Search API is a Javascript library that allows you to embed Google Search in your web pages and other web applications. To use the API, you will first need to sign up for an API key, and then follow the instructions below.

          The Google AJAX Search API provides simple web objects that perform inline searches over a number of Google services (Web Search, Local Search, Video Search, Blog Search, News Search, and Book SearchNew!). If your web page is designed to help users create content (e.g. message boards, blogs, etc.), the API is designed to support these activities by allowing them to copy search results directly into their messages.

          This API is new, and its feature set is largely determined by active involvement of its customers. Please join us in helping to shape the API and features by participating in the Google AJAX Search API discussion group to give feedback and discuss the API.


          也就是說, google ajax search api是一個js的庫,用戶可以很容易的用該庫創造google search,并嵌入到自己的網頁中
          其中, 顯示的搜索結果是使用ajax方式局部刷新,這就是 ajax search.

          另外該庫提供了非常豐富的方法用于各種類型的搜索: 搜索網頁 新聞 博客 視頻 Local 書籍等等.

          注意: 使用之前 你必須得到一個google的許可(本文例子中的許可是無效的)

          下面是使用的一個示范:
          snapshot:




          code:
          <%@ page language="java" pageEncoding="utf8"%>
          <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
          <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>

          <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
          <html>
              
          <head>
                  
          <meta http-equiv="content-type" content="text/html; charset=utf-8" />
                  
          <title>Hello World - Google AJAX Search API Sample</title>
                  
          <link href="http://www.google.com/uds/css/gsearch.css" type="text/css"
                      rel
          ="stylesheet" />
                  
          <style type="text/css">

              body 
          {
                background-color
          : white;
                color
          : black;
                font-family
          : Arial, sans-serif;
                font-size
          : small;
                margin
          : 15px;
              
          }


              .gsc-control 
          { width : 400px; }


              
          </style>
                  
          <script
                      
          src=http://www.google.com/uds/api?file=uds.js&v=1.0&source=uds-nbw&key=ABQIAAAA7z8-MRw3_j1ARxWWDiTXVhSrOPdqeecPZI9yWXVvOSTDE_N24BSQU0934cBfpy8j36zzYzSBH
                      type
          ="text/javascript"></script>
                  
          <script type="text/javascript">
              
          //<![CDATA[

              
          function OnLoad() {
                
          // Create a search control
                var searchControl = new GSearchControl();

                
          // Add in a full set of searchers
                var localSearch = new GlocalSearch();
                
          // searchControl.addSearcher(localSearch);
                
                
          //限制搜索的起始點 即站內搜索
                var siteSearch = new GwebSearch();
                siteSearch.setUserDefinedLabel(
          "xuanhua.org");
                siteSearch.setUserDefinedClassSuffix(
          "siteSearch");
                siteSearch.setSiteRestriction(
          "xuanhua.org");
                searchControl.addSearcher(siteSearch);
                
                searchControl.addSearcher(
          new GwebSearch());
                searchControl.addSearcher(
          new GvideoSearch());
                searchControl.addSearcher(
          new GblogSearch());
                searchControl.addSearcher(
          new GnewsSearch());
                searchControl.addSearcher(
          new GbookSearch());

                
          // Set the Local Search center point
                // localSearch.setCenterPoint("New York, NY");

                
          // tell the searcher to draw itself and tell it where to attach
               // create a drawOptions object
                var drawOptions = new GdrawOptions();

               
          // tell the searcher to draw itself in linear mode
                drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
                searchControl.draw(document.getElementById(
          "searchcontrol"), drawOptions);

                
          // execute an inital search
                searchControl.execute("bsbs");
              }

              GSearch.setOnLoadCallback(OnLoad);

              
          //]]>
              
          </script>

              
          </head>

              
          <body>
                  
          <f:view>
                  This is my JSF JSP page. 
          <br>
                      
          <div id="searchcontrol" align="justify">
                          Loading
                      
          </div>
                  
          </f:view>
              
          </body>
          </html>



          如上, 頁面加載的時候,將調用onLoad()方法, 該方法設置了搜索組件的屬性并且在頁面渲染.
          見onLoad()方法
          GSearchControl 是核心類,用來管理各種類型的搜索.
          每種搜索對應不同的類 如上就使用了
                searchControl.addSearcher(new GwebSearch());
                searchControl.addSearcher(new GvideoSearch());
                searchControl.addSearcher(new GblogSearch());
                searchControl.addSearcher(new GnewsSearch());
                searchControl.addSearcher(new GbookSearch());
          分別加載了網頁 視頻 博客 新聞 和書籍的搜索
          這些搜索子組件通過addSearcher方法加載到searchControl中
          (可以想像searchControl是一個JFrame 其他的是swing別的組件)

          每個搜索子組件可以進行屬性設置,在這里,我們初始化了一個頁面搜索子組件
          自定義其顯示標簽為"xuanhua.org" 同時限制其搜索范圍在xuanhua.org中(即做站內搜索)

                var siteSearch = new GwebSearch();
                siteSearch.setUserDefinedLabel("xuanhua.org");
                siteSearch.setUserDefinedClassSuffix("siteSearch");
                siteSearch.setSiteRestriction("xuanhua.org");
                searchControl.addSearcher(siteSearch);

          另外 也可以設置整個搜索組件的樣式 即searchControl的顯示方式
          設置方法是通過傳入一個drawOptions
          以下方法將searchControl顯示成tabbedPane裝(默認是linear),并把它繪制在名為searchcontrol的div中

                var drawOptions = new GdrawOptions();

               // tell the searcher to draw itself in TABBED mode
                drawOptions.setDrawMode(GSearchControl.DRAW_MODE_TABBED);
                searchControl.draw(document.getElementById("searchcontrol"), drawOptions);

          同時 可以通過api調用讓搜索組件進行自動查詢:
               查詢字段為"bsbs"
              searchControl.execute("bsbs");

          最后記得設置
              GSearch.setOnLoadCallback(OnLoad);
              這樣頁面加載的時候會自動調用onLoad()方法.


          Google Ajax Search 還有許多強大的設置 這里就不多說了 詳細的可以看官方網站.
          http://code.google.com/apis/ajaxsearch/documentation/

          順便說一下 當初我看這個只是為了做站內搜索 找了半天發現只有ajax搜索 沒有普通搜索的api
          最后發現.....其實寫一個表格就完事了...

          <form method="get" action="http://www.google.com/search">
                          
          <table bgcolor="#FFFFFF">
                              
          <tr>
                                  
          <td>
                                      
          <href="http://www.google.com/"> <img
                                              
          src="http://www.google.com/logos/Logo_40wht.gif" border="0"
                                              alt
          ="Google"> </a>
                                  
          </td>
                                  
          <td>
                                      
          <input type=text name=q size=31 maxlength=255 value="" />
                                      
          <input type=hidden name=ie value=GB2312 />
                                      
          <input type=hidden name=oe value=GB2312 />
                                      
          <input type=hidden name=hl value=zh-CN />
                                      
          <input type=submit name=btnG value="Google 搜索" />
                                      
          <font size="-1"> <input type=hidden name=domains
                                              
          value="www.xuanhua.org" /> <input type=radio name=sitesearch
                                              
          value="www.xuanhua.org" checked>www.xuanhua.org </font>
                                  
          </td>
                              
          </tr>
                          
          </table>
                      
          </form>


          posted on 2007-07-25 17:52 dyerac in java... 閱讀(1940) 評論(0)  編輯  收藏 所屬分類: ajax 、原創文章 、JavaEE
           
          Copyright © dyerac in java... Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 镇赉县| 赫章县| 辽宁省| 宣汉县| 临泽县| 五家渠市| 峨眉山市| 剑阁县| 惠来县| 西城区| 明光市| 深圳市| 稻城县| 虹口区| 临湘市| 重庆市| 邢台县| 云和县| 准格尔旗| 铅山县| 奈曼旗| 绩溪县| 隆化县| 司法| 成都市| 内黄县| 龙井市| 体育| 小金县| 修水县| 萨迦县| 宜黄县| 天气| 蒙阴县| 江川县| 肃北| 宝应县| 米易县| 鲁甸县| 屯门区| 宜春市|