如何使用 JSNI 在 java 函数中传递 JSON 对象?
How to pass the JSON object in java function using JSNI?
我正在学习 GWT,我正在尝试以下示例,其中我试图在 java 函数中传递 JSON 对象。
public class HomeController implements EntryPoint {
public void onModuleLoad() {
createTestNativeFunction();
Presenter presenter = new PersenterImpl();
presenter.go(RootPanel.get());
}
public native void createTestNativeFunction()/*-{
parser: function() {
var that = this;
var jsonResult = JSON.parse({id:42,name:'yo'});
return this.@com.easylearntutorial.gwt.client.HomeController::onParse(Lorg/sgx/jsutil/client/JsObject;)(jsonResult);
}
void onParse(jsonResult){
System.out.println(jsonResult);
}
}
}-*/;
}
我收到以下错误:
Tracing compile failure path for type 'com.easylearntutorial.gwt.client.HomeController'
[ERROR] Errors in 'file:/C:/Users/ameen/workspace/Tutorial/src/com/easylearntutorial/gwt/client/HomeController.java'
[ERROR] Line 31: missing ; before statement
void onParse(jsonResult){
--------------------------------^
[ERROR] Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly
[WARN] Server class 'com.google.gwt.dev.shell.jetty.JDBCUnloader' could not be found in the web app, but was found on the system classpath
[WARN] Adding classpath entry 'file:/C:/Program%20Files/gwt-2.7.0/gwt-dev.jar' to the web app classpath for this session
For additional info see: file:/C:/Program%20Files/gwt-2.7.0/doc/helpInfo/webAppClassPath.html
System.out.println()
是一个java函数,你要找的是console.log()
.
native
的正文是 Java 脚本,而不是 Java。
您正在将变量 jsonResult 声明到您的解析器中:function(),jsonResult 仅存在于该函数中。所以系统才会这么说你
丢失的 ;声明前
因为您从未将变量声明到 createTestNativeFunction() 中。
加上 sjakubowski 是对的 System.out.println() 是一个 java 函数,你需要在 JavaScript.
上使用 console.log()
试试这个:
public native void createTestNativeFunction(){
var jsonResult = {};
parser: function() {
var that = this;
jsonResult = JSON.parse({id:42,name:'yo'});
return this.@com.easylearntutorial.gwt.client.HomeController::onParse(Lorg/sgx/jsutil/client/JsObject;)(jsonResult);
}
void onParse(jsonResult){
console.log(jsonResult);
}
}
我做了以下操作来解决我的错误。
public class HomeController implements EntryPoint {
public void onModuleLoad() {
createTestNativeFunction();
Presenter presenter = new PersenterImpl();
presenter.go(RootPanel.get());
}
// var jsonResult = JSON.parse({id:42,name:'yo'});
public native void createTestNativeFunction()/*-{
var that = this;
$wnd.testFunction = function(jsonResult) {
that.@com.easylearntutorial.gwt.client.HomeController::onParse(Lorg/sgx/jsutil/client/JsObject;)(jsonResult);
};
}-*/;
public void onParse(JsObject jsonResult){
int i =42;
}
}
你真的应该尽量避免使用 JSNI。你可能 99% 的代码根本不使用 JSNI。如果你真的需要它,你应该使用新的 JsInterop,文档还处于早期阶段,但你可以看到这个 documentation here.
如果你需要使用JsInterop或JSNI,通常是因为你需要包装一个JS库,所以先看看它是否已经包装好了。如果不是,您可以随时使用其他一些包装器库来学习如何包装您的 JS 库。
- OpenLayers JsInterop 包装器 https://github.com/TDesjardins/gwt-ol3
- OpenLayers JSNI 包装器(已弃用)https://github.com/geosdi/GWT-OpenLayers
- 或探索 github https://github.com/search?q=topic%3Agwt+topic%3Ajsinterop
我正在学习 GWT,我正在尝试以下示例,其中我试图在 java 函数中传递 JSON 对象。
public class HomeController implements EntryPoint {
public void onModuleLoad() {
createTestNativeFunction();
Presenter presenter = new PersenterImpl();
presenter.go(RootPanel.get());
}
public native void createTestNativeFunction()/*-{
parser: function() {
var that = this;
var jsonResult = JSON.parse({id:42,name:'yo'});
return this.@com.easylearntutorial.gwt.client.HomeController::onParse(Lorg/sgx/jsutil/client/JsObject;)(jsonResult);
}
void onParse(jsonResult){
System.out.println(jsonResult);
}
}
}-*/;
}
我收到以下错误:
Tracing compile failure path for type 'com.easylearntutorial.gwt.client.HomeController'
[ERROR] Errors in 'file:/C:/Users/ameen/workspace/Tutorial/src/com/easylearntutorial/gwt/client/HomeController.java'
[ERROR] Line 31: missing ; before statement
void onParse(jsonResult){
--------------------------------^
[ERROR] Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly
[WARN] Server class 'com.google.gwt.dev.shell.jetty.JDBCUnloader' could not be found in the web app, but was found on the system classpath
[WARN] Adding classpath entry 'file:/C:/Program%20Files/gwt-2.7.0/gwt-dev.jar' to the web app classpath for this session
For additional info see: file:/C:/Program%20Files/gwt-2.7.0/doc/helpInfo/webAppClassPath.html
System.out.println()
是一个java函数,你要找的是console.log()
.
native
的正文是 Java 脚本,而不是 Java。
您正在将变量 jsonResult 声明到您的解析器中:function(),jsonResult 仅存在于该函数中。所以系统才会这么说你 丢失的 ;声明前 因为您从未将变量声明到 createTestNativeFunction() 中。
加上 sjakubowski 是对的 System.out.println() 是一个 java 函数,你需要在 JavaScript.
上使用 console.log()试试这个:
public native void createTestNativeFunction(){
var jsonResult = {};
parser: function() {
var that = this;
jsonResult = JSON.parse({id:42,name:'yo'});
return this.@com.easylearntutorial.gwt.client.HomeController::onParse(Lorg/sgx/jsutil/client/JsObject;)(jsonResult);
}
void onParse(jsonResult){
console.log(jsonResult);
}
}
我做了以下操作来解决我的错误。
public class HomeController implements EntryPoint {
public void onModuleLoad() {
createTestNativeFunction();
Presenter presenter = new PersenterImpl();
presenter.go(RootPanel.get());
}
// var jsonResult = JSON.parse({id:42,name:'yo'});
public native void createTestNativeFunction()/*-{
var that = this;
$wnd.testFunction = function(jsonResult) {
that.@com.easylearntutorial.gwt.client.HomeController::onParse(Lorg/sgx/jsutil/client/JsObject;)(jsonResult);
};
}-*/;
public void onParse(JsObject jsonResult){
int i =42;
}
}
你真的应该尽量避免使用 JSNI。你可能 99% 的代码根本不使用 JSNI。如果你真的需要它,你应该使用新的 JsInterop,文档还处于早期阶段,但你可以看到这个 documentation here.
如果你需要使用JsInterop或JSNI,通常是因为你需要包装一个JS库,所以先看看它是否已经包装好了。如果不是,您可以随时使用其他一些包装器库来学习如何包装您的 JS 库。
- OpenLayers JsInterop 包装器 https://github.com/TDesjardins/gwt-ol3
- OpenLayers JSNI 包装器(已弃用)https://github.com/geosdi/GWT-OpenLayers
- 或探索 github https://github.com/search?q=topic%3Agwt+topic%3Ajsinterop