[英]Can Java objects be accessed from Javascript in GWT?
My goal is to initiate RPC calls directly from javascript. 我的目标是直接从JavaScript启动RPC调用。 I have come up with ways to fake callbacks (because of the asynchronous nature of RPC) but I can't figure out how to get custom objects into javascript.
我已经想出了伪造回调的方法(由于RPC的异步特性),但我不知道如何将自定义对象添加到javascript中。
So, I've created a class named Interop and I statically create the service I'm interested in (had to use static as it was all I could get working, I don't think it's relevant right now): 因此,我创建了一个名为Interop的类,并静态创建了我感兴趣的服务(必须使用static,因为这是我可以使用的全部功能,我现在不认为它有意义):
public class Interop {
private static final GreetingServiceAsync service = GWT.create(GreetingService.class);
...
}
I then create a function that will do the async calls and handle the responses: 然后,我创建一个函数来执行异步调用并处理响应:
public static void greetServer(final String success, final String failure) {
service.greetServer(
"Homer",
new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
callback(failure, caught.toString());
}
public void onSuccess(String result) {
callback(success, result);
}
}
);
}
Then I create a JSNI function to export this function to javascript which I call from the onModuleLoad(): 然后,我创建一个JSNI函数以将此函数导出到我从onModuleLoad()调用的javascript中:
public static native void export() /*-{
$wnd.greetServer = $entry(@package.Interop::greetServer(Ljava/lang/String;Ljava/lang/String;));
}-*/;
And also create another JSNI function to deal with the callbacks: 并且还创建另一个JSNI函数来处理回调:
public static native void callback(String func, String response) /*-{
$wnd[func](response);
}-*/;
So that the function names I pass into greetServer() initially for success and failure are called by the JSNI as callbacks. 这样,我最初为成功和失败而传递给greetServer()的函数名称就由JSNI称为回调。 And this all works great when dealing with Strings or (I assume) a primitive type.
在处理字符串或(我假设)原始类型时,所有这些都很好用。 But when I try to do this with custom types (note the altered Custom type parameter):
但是,当我尝试使用自定义类型执行此操作时(请注意已更改的“自定义类型”参数):
public static native void callback(String func, Custom response) /*-{
$wnd[func](response);
}-*/;
Then what ends up in javascript doesn't work. 然后,以javascript结尾的内容不起作用。 It seems to be a javascript object with cascading arrays and none of the methods are available.
它似乎是一个具有级联数组的javascript对象,没有可用的方法。
So, the question is, how can Java-originated objects that aren't basic or primitives be accessed from within javascript (not JSNI)? 因此,问题是,如何从javascript(不是JSNI)中访问不是基本或原始类型的Java起源的对象? From what I can tell JavaScriptObject needs to originate in javascript, but in my case, my objects are originating in Java.
据我所知,JavaScriptObject需要起源于javascript,但就我而言,我的对象起源于Java。 What can I do?
我能做什么?
I've also looked into gwt-exporter and that shows how to instantiate java stuff from javascript, but not how to access java-originated stuff in javascript. 我也研究了gwt-exporter,它显示了如何从javascript实例化Java东西,但是没有如何在javascript中访问Java起源的东西。
I know this is a bit confusing so please let me know if you have any questions. 我知道这有点令人困惑,所以如果您有任何疑问,请告诉我。 Thanks!
谢谢!
With gwt-exporter this could be your code: 使用gwt-exporter,这可能是您的代码:
// Create and export a closure used to wrap javascript callbacks
@ExportClosure
public static interface InteropCallback extends Exportable {
void exec(String message);
}
// Make your Interop class exportable and export methods in it
@ExportPackage("foo")
@Export
public static class Interop implements Exportable {
final static GreetingServiceAsync service = GWT.create(GreetingService.class);
public static void greeting(String message,
final InteropCallback success,
final InteropCallback error) {
service.greetServer(message, new AsyncCallback<String>() {
public void onFailure(Throwable caught) {
error.exec(caught.getMessage());
}
public void onSuccess(String result) {
success.exec(result);
}
});
}
}
// In your onModuleLoad you have to make gwt-exporter export your stuff
@Override public void onModuleLoad() {
ExporterUtil.exportAll();
...
}
Finally call your java methods from handwritten javascript 最后从手写JavaScript调用Java方法
window.foo.Interop.greeting("Hello",
function(s){console.log(s)},
function(s){console.log(s)}
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.