将参数从javascript传递到gwt?

时间:2014-07-10 10:48:22

标签: java javascript gwt

我是Gwt的新手,我实现了一个webclipper项目所以我的任务是从javascript文件发送一些参数到Gwt,这样我就可以与我的couchdb数据库建立连接,但是我在传递参数时遇到了问题标题,网址和摘要从网页到Gwt n然后couchdb。以下代码是我的javascript代码: -

function onPageInfo(o)  { 
    document.getElementById('title').value = o.title;         
    document.getElementById('url').value = o.url; 
    document.getElementById('summary').innerText = o.summary; 
}  

    // Global reference to the status display SPAN
    var statusDisplay = null;

    // POST the data to the server using XMLHttpRequest
    function addBookmark() {
    // Cancel the form submit
    event.preventDefault();

    // The URL to POST our data to
    var postUrl = "http://127.0.0.1:8888/practice.html?      gwt.codesvr=127.0.0.1:9997&gwt.codesvr=127.0.0.1:9997/?title=1&url=2&summary=3";

    // Set up an asynchronous AJAX POST request
    var xhr = new XMLHttpRequest();
    xhr.open('POST', postUrl, true);

    // Prepare the data to be POSTed
    var title = encodeURIComponent(document.getElementById('title').value);
    var url = encodeURIComponent(document.getElementById('url').value);
    var summary = encodeURIComponent(document.getElementById('summary').value);
    var tags = encodeURIComponent(document.getElementById('tags').value);

    var params = 'title=' + title + 
             '&url=' + url + 
             '&summary=' + summary +
             '&tags=' + tags;

    // Replace any instances of the URLEncoded space char with +
    params = params.replace(/%20/g, '+');

    // Set correct header for form data 
    xhr.setRequestHeader('Content-type', 'application/json');

    // Handle request state change events
    xhr.onreadystatechange = function() { 
    // If the request completed
    if (xhr.readyState == 4) {
        statusDisplay.innerHTML = '';
        if (xhr.status == 200) {
            // If it was a success, close the popup after a short delay
            statusDisplay.innerHTML = 'Saved!';
            window.setTimeout(window.close, 1000);
        } else {// Show what went wrong
            statusDisplay.innerHTML = 'Error saving: ' + xhr.statusText;
        }
    }
   };

    // Send the request and set status

    xhr.send(params);
    statusDisplay.innerHTML = 'Saving...';
    }

    // When the popup HTML has loaded
    window.addEventListener('load', function(evt) {
    // Handle the bookmark form submit event with our addBookmark function
    document.getElementById('addbookmark').addEventListener('submit', addBookmark);


    // Cache a reference to the status display SPAN
    statusDisplay = document.getElementById('status-display');
    // Call the getPageInfo function in the background page, injecting content_script.js 


   // into the current HTML page and passing in our onPageInfo function as the callback
   chrome.extension.getBackgroundPage().getPageInfo(onPageInfo);
 });

感谢.....

1 个答案:

答案 0 :(得分:0)

您可以通过导出该函数来调用java文件(GWT客户端模块)中定义的函数。让我们假设有一个A.java类,它也是你的入口点类。这个类包含someMethod(),您需要通过javascript传递一些参数来调用它。 A类的内容类似于

public class A implements EntryPoint {
        public static functionExported = false;
        public void onModuleLoad() {
            ExportToBeCalledFromJs();

            // other code goes here
        }

    public static native void ExportToBeCalledFromJs() /*-{
        $wnd.toBeCalledFromJs = $entry(function(s1, s2) {
            return @com.practice.gwt.client.A::someFunction();
        });
        @com.practice.gwt.client.A:functionExported = true;
        }-*/;
    }

以上代码导出该函数并使其可用于javascript。您可以简单地从您的js调用BeCalledFromJs(param1,param2),其中param1将替换s1,param2将替换s2。如果你想添加更多参数,可以在上面的代码中修改$ entry(function(s1,s2)。