<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    DWR學習心得(一)

    DWR是一個可以允許你去創建AJAX WEB 站點的JAVA 開源庫。

    它可以讓你在瀏覽器中的Javascript代碼調用Web服務器上的Java 代碼,就像在Java代碼就在瀏覽器中一樣。

        DWR包含 2個主要部分:

            一個運行在服務器端的Java Servlet,它處理請求并且向瀏覽器發回響應。

            一個運行在瀏覽器端的JavaScript,它發送請求而且還能動態更新網頁。

     

    DWR工作原理是通過動態把Java類生成為Javascript。它的代碼就像Ajax魔法一樣,你感覺調用就像發

    生在瀏覽器端,但是實際上代碼調用發生在服務器端,DWR負責數據的傳遞和轉換。這種從Java 到

    JavaScript的遠程調用功能的方式使DWR用起來有種非常像RMI或者SOAP的常規RPC機制,而且DWR

    的優點在于不需要任何的網頁瀏覽器插件就能運行在網頁上。

     

    Java從根本上講是同步機制,然 AJAX卻是異步的。所以你調用遠程方法時,當數據已經從網絡上返回

    的時候,你要提供有反調 (callback) 功能的DWR。


    第 1個 DWR 例子:Hello World

      1) 從官方網站下載dwr.jar包。然后將它放在你 webapp 的 WEB-INF/lib目錄下。

      2) 修改web.xml,如下

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app PUBLIC
        
    "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
        
    "http://java.sun.com/dtd/web-app_2_3.dtd">

    <web-app id="dwr">

      
    <display-name>DWR (Direct Web Remoting)</display-name>
      
    <description>A Simple Demo DWR</description>

      
    <servlet>
        
    <servlet-name>dwr-invoker</servlet-name>
       
    <servlet-class>org.directwebremoting.servlet.DwrServlet</servlet-class>
        
    <init-param>
          
    <param-name>debug</param-name>
          
    <param-value>true</param-value>
        
    </init-param>
     
    </servlet>

      
    <servlet-mapping>
        
    <servlet-name>dwr-invoker</servlet-name>
        
    <url-pattern>/dwr/*</url-pattern>
      </servlet-mapping>
    </web-app>


      3) 在WEB-INF下新建dwr.xml,內容如下

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd">

    <dwr>

      
    <allow>
      
    <create creator="new" javascript="service">
          
    <param name="class"      
           value
    ="helloworld.Service"/>
       
    </create>
      
    </allow>
    </dwr>

     4) 啟動web服務器,訪問http://localhost/工程/dwr ,頁面結果顯示為

     

    Classes known to DWR:
        
    * service (helloworld.Service)


     5) 點擊進入 *service,看到提示....

    Methods For: service (helloworld.Service)

    To use 
    this class in your javascript you will need the following script includes:

      
    <script type='text/javascript' src='/TestDWR/dwr/interface/service.js'></script>
      
    <script type='text/javascript' src='/TestDWR/dwr/engine.js'></script>

    In addition there is an optional utility script:

      
    <script type='text/javascript' src='/TestDWR/dwr/util.js'></script>

    Replies from DWR are shown with a yellow background 
    if they are simple or in an alert box otherwise.
    The inputs are evaluated as Javascript so strings must be quoted before execution.

    There are 
    10 declared methods:

        
    * sayHello( );
        
    * hashCode( );
          (Warning: hashCode() is excluded: Methods defined in java.lang.Object are not accessible. See below)
        
    * getClass( );
          (Warning: No Converter 
    for java.lang.Class. See below)
          (Warning: getClass() is excluded: Methods defined in java.lang.Object are not accessible. See below)
        
    * wait( );
          (Warning: overloaded methods are not recommended. See below)
          (Warning: wait() is excluded: Methods defined in java.lang.Object are not accessible. See below)
        
    * wait( , );
          (Warning: overloaded methods are not recommended. See below)
          (Warning: wait() is excluded: Methods defined in java.lang.Object are not accessible. See below)
        
    * wait( );
          (Warning: overloaded methods are not recommended. See below)
          (Warning: wait() is excluded: Methods defined in java.lang.Object are not accessible. See below)
        
    * equals( );
          (Warning: No Converter 
    for java.lang.Object. See below)
          (Warning: equals() is excluded: Methods defined in java.lang.Object are not accessible. See below)
        
    * notify( );
          (Warning: notify() is excluded: Methods defined in java.lang.Object are not accessible. See below)
        
    * notifyAll( );
          (Warning: notifyAll() is excluded: Methods defined in java.lang.Object are not accessible. See below)
        
    * toString( );
          (Warning: toString() is excluded: Methods defined in java.lang.Object are not accessible. See below)


      6)創建jsp,內容如下

    <%@ page language="java" pageEncoding="UTF-8"%> 
     
    <html> 
      
    <head> 
       
      
    <script type='text/javascript' src='dwr/interface/service.js'></script>
      
    <script type='text/javascript' src='dwr/engine.js'></script>
      
    <script type='text/javascript' src='dwr/util.js'></script>

    </script> 
      
    <script type="text/javascript"> 
      function firstDwr(){ 
        service.sayHello(
    " Test ",callBackHello); 
      } 
      function callBackHello(data){ 
        alert(data); 
      } 
      
    </script> 
      
    </head> 
      
      
    <body> 
      
    <input type="button" name="button" value="測試" onclick="firstDwr()"> 
      
    </body> 
    </html> 


    當點擊"測試"時,就出現結果了.

    總結:

      1.在web.xml中加入dwr servlet

      2.在drw.xml中指明你要調用的類,并指明生成的javascript名

         <create creator="new" javascript="service">

          <param name="class" value="helloworld.Service"/>

        </create>

      3.完成對應的類

      4.在html/jsp中寫javascript函數,調用java服務器端的方法,并寫處理結果的回調函數

     



    posted on 2007-06-01 22:39 想飛就飛 閱讀(3038) 評論(1)  編輯  收藏 所屬分類: J2EE

    評論

    # re: DWR學習心得(一) 2008-01-25 22:58 爸爸

    不錯好娃!  回復  更多評論   

    公告


    導航

    <2007年6月>
    272829303112
    3456789
    10111213141516
    17181920212223
    24252627282930
    1234567

    統計

    常用鏈接

    留言簿(13)

    我參與的團隊

    隨筆分類(69)

    隨筆檔案(68)

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲国产高清视频| 国产午夜亚洲不卡| 亚洲乱码一区av春药高潮| 久久狠狠躁免费观看2020| 午夜亚洲AV日韩AV无码大全| 大地资源中文在线观看免费版| 亚洲一区二区三区影院 | 国产又黄又爽胸又大免费视频| 亚洲人成人无码网www国产| 一边摸一边爽一边叫床免费视频 | 亚洲第一区香蕉_国产a| 2019中文字幕在线电影免费 | 亚洲AV综合色区无码另类小说| 暖暖免费日本在线中文| 亚洲理论片中文字幕电影| 国产免费不卡v片在线观看| 亚洲综合偷自成人网第页色| 日本免费网站观看| 国产精品九九久久免费视频| 亚洲AV第一页国产精品| 久久久久国产精品免费免费搜索 | 国外亚洲成AV人片在线观看| 在线观看免费无码视频| 亚洲网红精品大秀在线观看| 成人免费无码大片a毛片软件| 精品视频免费在线| 亚洲国产精品久久久久网站 | 伊人久久五月丁香综合中文亚洲| 国产色婷婷精品免费视频| jyzzjyzz国产免费观看| 亚洲精品视频专区| 免费国产成人午夜私人影视| 免费日本一区二区| 亚洲女子高潮不断爆白浆| 亚洲日本一区二区三区在线| 99热在线精品免费全部my| 人妖系列免费网站观看| 亚洲国产综合在线| 国产亚洲精品国看不卡| 成年美女黄网站色大免费视频| eeuss草民免费|