Java对象可以通过GWT中的Javascript访问吗? [英] Can Java objects be accessed from Javascript in GWT?

查看:81
本文介绍了Java对象可以通过GWT中的Javascript访问吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目标是直接从JavaScript启动RPC调用。我想出了一些假回调方法(由于RPC的异步性质),但我无法弄清楚如何将自定义对象转换为javascript。

所以,我创建了一个名为Interop的类,并静态创建了我感兴趣的服务(必须使用静态,因为这是我所能工作的一切,我不认为它现在是相关的):

  public class Interop {
private static final GreetingServiceAsync service = GWT .create(GreetingService.class);
...
}

然后创建一个函数,异步调用和处理响应:

$ p $ public static void greetServer(final String success,final String failure){
service .greetServer(
Homer,
新的AsyncCallback< String>(){
public void onFailure(Throwable caught){
callback(failure,caught.toString());

$ b public void onSuccess(String result){
callback(success,result);
}
}
);





$ b然后我创建了一个JSNI函数将这个函数导出为javascript, onModuleLoad():

  public static native void export()/ *  -  {
$ wnd.greetServer = $条目(@ package.Interop :: greetServer(Ljava /郎/字符串; Ljava /郎/字符串;));
} - * /;

另外创建另一个JSNI函数来处理回调:

  public static native callback(String func,String response)/ *  -  {
$ wnd [func](response);
} - * /;

为了成功和失败,最初传递给greetServer()的函数名称由JSNI调用作为回调。当处理字符串或(我假设)一个原始类型时,这一切都很好。但是,当我尝试使用自定义类型执行此操作时(请注意已更改的Custom类型参数):

  public static native void callback(String func,Custom response)/ *  -  {
$ wnd [func](response);
} - * /;

然后,javascript中的内容不起作用。它似乎是一个带有层叠数组的javascript对象,并且这些方法都不可用。



所以,问题是,Java初始化对象不是基本的或基元可以从JavaScript(而不是JSNI)中访问?从我可以告诉JavaScriptObject需要源于JavaScript,但在我的情况下,我的对象来自Java。我可以做什么?

我也研究过gwt-exporter,它展示了如何从javascript实例化java的东西,但不知道如何访问java发起的东西javascript。



我知道这有点令人困惑,所以如果您有任何问题,请让我知道。

解决方案

使用gwt-exporter这可能是您的代码:

  //创建并导出用于包装javascript回调的闭包
@ExportClosure
public static interface InteropCallback extends Exportable {
void exec(String message) ;
}

//在其中创建Interop类的可导出和导出方法
@ExportPackage(foo)
@Export
public static class Interop实现Exportable {
最终静态GreetingServiceAsync服务= GWT.create(GreetingService.class);

public static void greeting(String message,$ b $ final InteropCallback success,
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);
}
});



在你的onModuleLoad中,你必须让gwt-exporter导出你的东西
@Override public void onModuleLoad(){
ExporterUtil .exportAll();
...
}

最后从手写javascript

  window.foo.Interop.greeting(Hello,
function(s){console.log(s) },
函数(s){console.log(s)}
);


My goal is to initiate RPC calls directly from javascript. 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.

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):

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():

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:

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. 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. It seems to be a javascript object with cascading arrays and none of the methods are available.

So, the question is, how can Java-originated objects that aren't basic or primitives be accessed from within javascript (not JSNI)? From what I can tell JavaScriptObject needs to originate in javascript, but in my case, my objects are originating in 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.

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:

// 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

window.foo.Interop.greeting("Hello", 
                            function(s){console.log(s)},
                            function(s){console.log(s)}
                            );

这篇关于Java对象可以通过GWT中的Javascript访问吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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