2010-10-25 26 views
1

使用DJ Native Swing可以在Java應用程序中顯示網頁。當你這樣做時,也可以使用「命令」協議從瀏覽器與java運行時環境進行通信。該文件有一個代碼段這表明它的用法:DJ Native Swing javascript命令問題


function sendCommand(command){ 
    var s = 'command://' + encodeURIComponent(command); 

    for(var i = 1; i < arguments.length; s+= '&' + encodeURIComponent(arguments[i++])); 
     window.location = s; 
} 

,因爲它看起來這似乎是一個普通的GET請求使用命令協議而不是HTTP網址。雖然當我創建和圖像,腳本標籤或只是和Ajax獲取請求沒有響應和Java運行時斷點不觸發。

我不想設置window.location的,因爲我不想要導航從我目前在頁面了。使用鏈接導航到命令url雖然可以工作,但它也可以從當前頁面導航。該頁面使用OpenLayers和dojo。 (我也嘗試dojo.io.script

回答

2

一些工作後,我發現了一個非常簡潔的方式與Java運行時溝通不觸發每次有通信時間的頁的刷新。它的靈感來源於JSONP在大多數瀏覽器中解決跨域限制的方式。因爲iFrame也會觸發一個command:// url,所以可以使用這種技術來做一個類似JSONP的動作。在客戶端側的代碼(瀏覽器):


dojo.provide("nmpo.io.java"); 
dojo.require("dojo.io.script"); 

nmpo.io.java = dojo.delegate(dojo.io.script, { 
    attach: function(/*String*/id, /*String*/url, /*Document?*/frameDocument){ 
     // summary: 
     //  creates a new tag pointing to the specified URL and 
     //  adds it to the document. 
     // description: 
     //  Attaches the script element to the DOM. Use this method if you 
     //  just want to attach a script to the DOM and do not care when or 
     //  if it loads.   
     var frame = dojo.create("iframe", { 
      id: id, 
      frameborder: 0, 
      framespacing: 0 
     }, dojo.body()); 

     dojo.style(frame, { display: "none" }); 
     dojo.attr(frame, { src: url }); 
     return frame; 
    }, 

    _makeScriptDeferred: function(/*Object*/args){ 
     //summary: 
     //  sets up a Deferred object for an IO request. 
     var dfd = dojo._ioSetArgs(args, this._deferredCancel, this._deferredOk, this._deferredError); 

     var ioArgs = dfd.ioArgs; 
     ioArgs.id = dojo._scopeName + "IoScript" + (this._counter++); 
     ioArgs.canDelete = false; 

     //Special setup for jsonp case 
     ioArgs.jsonp = args.callbackParamName || args.jsonp; 

     if(ioArgs.jsonp){ 
      //Add the jsonp parameter. 
      ioArgs.query = ioArgs.query || ""; 
      if(ioArgs.query.length > 0){ 
       ioArgs.query += "&"; 
      } 
      ioArgs.query += ioArgs.jsonp 
       + "=" 
       + (args.frameDoc ? "parent." : "") 
       + "nmpo.io.java.jsonp_" + ioArgs.id + "._jsonpCallback"; 

      ioArgs.frameDoc = args.frameDoc; 

      //Setup the Deferred to have the jsonp callback. 
      ioArgs.canDelete = true; 
      dfd._jsonpCallback = this._jsonpCallback; 
      this["jsonp_" + ioArgs.id] = dfd; 
     } 
     return dfd; // dojo.Deferred 
    } 
}); 

當一個請求被髮送到Java運行時一個回調參數將被提供,並且可以被執行的webBrowser.executeJavascript(callbackName + "(" + json + ");");動作而觸發在瀏覽器中的回調。

用法示例性客戶端:


dojo.require("nmpo.io.java"); 
nmpo.io.java.get({ 
    // For some reason the first paramater (the one after the '?') is never in the 
    // paramater array in the java runtime. As a work around we stick in a dummy. 
    url: "command://sum?_", 
    callbackParamName: "callback", 
    content: { 
     numbers: [ 1, 2, 3, 4, 5 ].join(",") 
    }, 
    load: function(result){ 
     console.log("A result was returned, the sum was [ " + result.result + " ]"); 
    } 
}); 

使用例的java:


webBrowser.addWebBrowserListener(new WebBrowserAdapter() { 
    @Override 
    public void commandReceived(WebBrowserCommandEvent e) { 
     // Check if you have the right command here, left out for the example 
     // Parse the paramaters into a Hashtable or something, also left out for the example 
     int sum = 0; 
     for(String number : arguments.get("numbers").split(",")){ 
      sum += Integer.parseInt(number); 
     } 

     // Execute the javascript callback like would happen with a regular JSONP call. 
     webBrowser.executeJavascript(arguments.get("callback") + "({ result: " + sum + " });"); 
    } 
}); 
與IE在幀I可以強烈建議使用螢火精簡版

此外,開發者工具的IE不可用。