Vaadin:如何从 javascript 方法中获取返回值? [英] Vaadin : how to get return value from javascript methods?

查看:37
本文介绍了Vaadin:如何从 javascript 方法中获取返回值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 JS 方法 showMessage(message)

I have one JS method showMessage(message)

function showMessage(message) {
 alert(message);
 return "Your message is " + message;
}

我可以从我的组件或 UI 中获取方法参数

I can get arguments of method from my component or UI as

    JavaScript.getCurrent().addFunction("showMessage", new JavaScriptFunction() {

        @Override
        public void call(final JSONArray arguments) throws JSONException {
            System.out.println("Getting call JS method " + arguments);

        }
    });
    Page.getCurrent().getJavaScript().execute("showMessage(Hello World !)");

那么,有没有办法在 Vaadin 中获取 JS 方法的返回值?如果是,我该如何解决?

So , has there anyway to get return values of JS methods in Vaadin ? If yes , how can I figure it out ?

推荐答案

不,你不能从那里回来

由于客户端和服务器之间通信的异步性质,无法将返回值发送回浏览器.https://vaadin.com/api/7.2.5/com/vaadin/ui/JavaScriptFunction.html#call(org.json.JSONArray)

Because of the asynchronous nature of the communication between client and server, no return value can be sent back to the browser. https://vaadin.com/api/7.2.5/com/vaadin/ui/JavaScriptFunction.html#call(org.json.JSONArray)

要获得返回值,您必须从客户端showMessage(浏览器/javascript)实际回调到服务器(您在服务器端定义的showMessage 方法).我认为,您在这里的主要问题是将它们命名为相同的(我不确定,如果 Vaadin 在这里应用一些基于包名称的前缀,但很可能您的 showMessaage 方法之一覆盖了另一个).

To get the return value, you have to actually callback to the server (your defined showMessage method on server side) from your client side showMessage (browser/javascript). I would assume, that your main problem here is naming them both the same (I am not sure, if Vaadin applies here some package name based prefix, but most likely one of your showMessaage methods overrides the other).

您应该将客户端的 showMessage 重命名为其他名称(下面的 requestMessage)并在其中调用服务器的 showMethod.

You should rename your client's showMessage to something else (requestMessage below) and call your server's showMethod in there.

// client/browser/javascript (remove/rename existing showMessage from your _client_ code)
function requestMessage(m) { showMessage("Requested: "+m) }
// server/vaadin/java
Page.current.javaScript.execute("requestMessage('Hello World')")

这篇关于Vaadin:如何从 javascript 方法中获取返回值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆