【发布时间】:2016-05-10 13:35:05
【问题描述】:
我有一个 GWT 项目,它使用 Google JavaScript Chart API 来显示 Java 对象的图表。我将我的 Java 对象转换为 JSON 并通过 JSNI 将它们传递给 JavaScript 图表 API,效果很好。
但现在我想更进一步,走另一条路:当我在图表内部单击时(我可以通过 JavaScript 侦听器检测到),我想将该 Java 对象发送回我的 Java 代码。
我想我已经完成了 JSNI 指南中的所有内容,但是当我单击图表时,我在 JavaScript 控制台中收到一个错误,我什至不知道如何阅读:
SCRIPT5022: Exception thrown and not caught
File: eval code (21), Line: 5, Column: 7
__gwt_javaInvokes[2] =
function(thisObj, dispId,p0,p1) {
var result = __static(dispId, thisObj,p0,p1);
if (result[0]) {
throw result[1];
} else {
return result[1];
}
} function(thisObj, dispId,p0,p1) { var result = __static(dispId, thisObj,p0,p1); if (result[0]) { throw result[1]; } else { return result[1]; } }
这是我的 html 页面:
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<link type="text/css" rel="stylesheet" href="GwtTest.css">
<title>GWT Test</title>
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" language="javascript" src="gwttest/gwttest.nocache.js"></script>
</head>
<body>
</body>
</html>
唯一有趣的是我添加了 Google Chart API 脚本标签。
这是我试图从 Java 传递到 JavaScript,然后返回到 Java 的 Java 对象:
package com.example.client;
public class ChartData {
public String title = "test";
public String labelOne = "thingOne";
public String labelTwo = "thingTwo";
public String labelThree = "thingThree";
public int valueOne = 100;
public int valueTwo = 200;
public int valueThree = 300;
}
最后,这是我的 GWT 客户端类:
package com.example.client;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.JsArray;
import com.google.gwt.core.client.JsonUtils;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.RootLayoutPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.VerticalPanel;
public class GwtTest implements EntryPoint {
public void onModuleLoad() {
RootLayoutPanel rootPanel = RootLayoutPanel.get();
SimplePanel chartPanel = new SimplePanel();
chartPanel.getElement().setId("chartDiv");
chartPanel.setSize("500px", "500px");
Button button = new Button("Click Me");
button.addClickHandler(new ClickHandler(){
@Override
public void onClick(ClickEvent event) {
ChartData chartData = new ChartData();
String json = getChartJson(chartData);
JsArray<?> chartArrayData = JsonUtils.safeEval(json);
drawChart(chartArrayData, chartData);
}
});
VerticalPanel vp = new VerticalPanel();
vp.add(button);
vp.add(chartPanel);
rootPanel.add(vp);
loadChartLibrary();
}
private String getChartJson(ChartData chartData){
String json = "[";
json += "[\"time\", \"" + chartData.title + "\"]";
json += ",";
json += "[\"" + chartData.labelOne + "\"," + chartData.valueOne + "]";
json += ",";
json += "[\"" + chartData.labelTwo + "\"," + chartData.valueTwo + "]";
json += ",";
json += "[\"" + chartData.labelThree + "\"," + chartData.valueThree + "]";
json += "]";
return json;
}
private native void loadChartLibrary()/*-{
$wnd.google.charts.load('current', {packages: ['corechart']});
}-*/;
private native void drawChart(JsArray<?> dataArray, ChartData chartData)/*-{
var data = $wnd.google.visualization.arrayToDataTable(dataArray);
var chart = new $wnd.google.visualization.ColumnChart($wnd.document.getElementById('chartDiv'));
chart.draw(data, null);
$wnd.google.visualization.events.addListener(chart, 'select', selectHandler);
function selectHandler(e) {
var selectionArray = chart.getSelection();
if(selectionArray.length == 0){
}
else{
var row = selectionArray[0].row;
console.log(row);
this.@com.example.client.GwtTest::printChartData(Lcom/example/client/ChartData;I)(chartData, row);
console.log("here");
}
}
}-*/;
public void printChartData(ChartData chartData, int i){
System.out.println(i + ": " + chartData.valueThree);
}
}
如果您运行此代码,您应该会看到一个显示 Click Me 的按钮。单击该按钮以显示图表。然后单击图表中的条形。我希望这会在 printChartData() 函数中触发我的 Java 代码,但我在 JavaScript 控制台中收到错误。
我已将问题缩小到这段代码:
var row = selectionArray[0].row;
console.log(row);
this.@com.example.client.GwtTest::printChartData(Lcom/example/client/ChartData;I)(chartData, row);
console.log("here");
该行已正确打印到控制台,因此我知道我的 JavaScript 正在运行。但是"here" 永远不会打印到控制台,所以我知道错误在我的 JSNI 行中。
我的 JSNI 设置是否遗漏了什么? JavaScript 错误是什么意思?
【问题讨论】:
标签: javascript java gwt google-visualization jsni