GWT:如何从JSNI访问java变量
我有这个方法:GWT:如何从JSNI访问java变量,gwt,jsni,Gwt,Jsni,我有这个方法: public void testJSNI2(){ String x = "test"; } 我可以这样访问此方法: helloJsni.@com.jsni.client.HelloJSNIImpl::testJSNI2(Ljava/lang/String;) 但是我如何访问字符串x,它是在一个方法中定义的?您不能访问变量x,因为它在该方法的范围内,就像您不能在Java代码中访问它一样。答案不正确。JavaScript和Java的作用并不相同。 在JSNI的帮助下,用户可以
public void testJSNI2(){
String x = "test";
}
我可以这样访问此方法:
helloJsni.@com.jsni.client.HelloJSNIImpl::testJSNI2(Ljava/lang/String;)
但是我如何访问字符串
x
,它是在一个方法中定义的?您不能访问变量x
,因为它在该方法的范围内,就像您不能在Java代码中访问它一样。答案不正确。JavaScript和Java的作用并不相同。
在JSNI的帮助下,用户可以从js访问任何字段:
public class JSNIExample {
String myInstanceField;
static int myStaticField;
void instanceFoo(String s) {
// use s
}
static void staticFoo(String s) {
// use s
}
public native void bar(JSNIExample x, String s) /*-{
// Call instance method instanceFoo() on this
this.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);
// Call instance method instanceFoo() on x
x.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s);
// Call static method staticFoo()
@com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);
// Read instance field on this
var val = this.@com.google.gwt.examples.JSNIExample::myInstanceField;
// Write instance field on x
x.@com.google.gwt.examples.JSNIExample::myInstanceField = val + " and stuff";
// Read static field (no qualifier)
@com.google.gwt.examples.JSNIExample::myStaticField = val + " and stuff";
}-*/;
}
你可以在这里看到:为什么会有人否决这个问题。这个网站是不是只为那些对他们所问的主题有专业知识的人而设?如果他们有专业知识,他们甚至需要问吗?别高兴了。