简体   繁体   English

如何在GWT JSNI中将返回值从JavaScript转换为Java?

[英]How to cast a return value from javascript to java in GWT JSNI?

The JSNI method does not accept any parameters but return a Java Object type: JSNI方法不接受任何参数,但返回Java Object类型:

 public static native String nativeJSFuncGwt() /*-{
        $wnd.console.log($wnd.someJSFunc());
        return "" + $wnd.someJSFunc() + "" ;
    }-*/;


//someJSFunc returns { abc:xcv, def:asd}

I can see the value getting printed in the javascript console but java side is not able to understand the casting. 我可以看到该值已在javascript控制台中打印出来,但Java方面无法理解转换。

Is it because the native method does not accept any parameters ? 是否因为本机方法不接受任何参数?

String tokenFromNativeJS = nativeJSFuncGwt(); // String value is null 

The documentation also is not clear enough in GWT. GWT中的文档也不够清楚。

Step one, avoid JSNI, you are better off defining a JsInterop method which provides the same API access. 第一步,避免使用JSNI,最好定义一个提供相同API访问权限的JsInterop方法。 JSNI will still work in GWT2 but JsInterop is the way forward for GWT3, and is often much easier to read and write. JSNI仍然可以在GWT2中使用,但是JsInterop是GWT3的前进之路,并且通常更容易读写。 This would look something like this: 看起来像这样:

@JsMethod(name = "someJSFunc", namespace = JsPackage.GLOBAL)
public static native String someJSFunc();

Step two, define a Java type which fits your expected JS return value. 第二步,定义一个适合您期望的JS返回值的Java类型。 This will work with either JSNI or JsInterop. 这将与JSNI或JsInterop一起使用。 In JSNI you would make a JavaScriptObject subclass, and provide methods which access the fields (see http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html and other docs for more detail, but as per step one, I'm not going to go into more depth on this here). 在JSNI中,您将创建一个JavaScriptObject子类,并提供访问字段的方法(有关更多详细信息,请参见http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html和其他文档,但是按照第一步,我将我不会在这里对此进行更深入的探讨)。 For your example object, this would look like this in JsInterop: 对于您的示例对象,在JsInterop中看起来像这样:

@JsType(isNative = true, name = "Object", namespace = JsPackage.GLOBAL)
public class SomeReturnObject {
    public String abc;
    public double def;
}

Obviously replace the field names and types with whatever is appropriate in your own project. 显然,用您自己项目中合适的名称替换字段名称和类型。 Give this new type with the placeholder name, here's what your global someJsFunc would look like: 给此新类型加上占位符名称,这就是您的全局someJsFunc的外观:

@JsMethod(name = "someJSFunc", namespace = JsPackage.GLOBAL)
public static native SomeReturnObject someJSFunc();

And you would use it like you expect in plain Java - no need to write JSNI any more: 您将像使用纯Java一样使用它-不再需要编写JSNI了:

SomeReturnObject object = someJSFunc();
DomGlobal.console.log(object.abc + ": " + object.def);

I am posting here what finally worked for due to GWT version(2.4) constraint 我在这里发布了由于GWT版本(2.4)约束而最终成功的结果

From GWT Doc: 从GWT Doc:

Outgoing Java type: 外发Java类型:

Any other Java Object (including arrays) 任何其他Java对象(包括数组)

What must be passed: 必须通过什么:

Java Object of the correct type that must have originated in Java code; 必须源自Java代码的正确类型的Java对象; Java objects cannot be constructed from “thin air” in JavaScript Java对象无法从JavaScript中“稀薄地”构造

My code with modification would like: 我的修改后的代码想要:

public static native MyObject nativeJSFuncGwt(MyObject obj) /*-{

   var xyz = $wnd.someJsFunc();

   obj.@package.name::setter1(Ljava/lang/String;)(xyz);

   return obj;

 }-*/;

I wish documentation could have been more clear. 我希望文档可以更加清晰。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM