【发布时间】:2011-06-30 13:14:14
【问题描述】:
我一直在努力将 Java 对象通过 JSNI(gwt-exporter 生成)从 Java 传递到 Java 中,想知道是否有人可以提供帮助?
我正在用 Java(“Person”)创建一个对象,将它传递给一个 JSNI 方法(“displayPerson”),该方法调用一个使用 gwt-exporter 公开的 Java 方法(“CommonService.displayPerson”);但是最后阶段的参数变为空。
如果我传递一个字符串,它可以正常工作;我遇到问题的只是 my 个对象。
Person 在由其他 GWT 应用程序继承的 GWT 应用程序 JAR 中定义。
感谢收看,
迈克
GWT 应用程序
package com.anstis.pluginserver.client;
import com.anstis.plugincommon.shared.Person;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
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.RootPanel;
public class PluginServer implements EntryPoint {
public void onModuleLoad() {
GWT.create(CommonService.class);
onLoadImpl();
RootPanel.get("container").add(getButton());
}
private native void onLoadImpl() /*-{
if ($wnd.jscOnLoad && typeof $wnd.jscOnLoad == 'function') $wnd.jscOnLoad();
}-*/;
private Button getButton() {
Button btn = new Button("Click!");
btn.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
Person p = new Person();
p.setName("Smurf");
p.setAge(500);
displayPerson(p);
}
});
return btn;
}
private native void displayPerson(Person person) /*-{
// The below displays shows 'person' is not null
alert("PluginServer.displayPerson.person is " + (person != null ? "not " : "") + "null");
try {
var pluginServer = new $wnd.com.anstis.pluginserver.CommonService();
// The below displays shows 'pluginServer' is not null
alert("PluginServer.displayPerson.pluginServer = " + pluginServer);
pluginServer.displayPerson(person);
} catch(err) {
alert(err);
}
}-*/;
}
CommonService.java
package com.anstis.pluginserver.client;
import org.timepedia.exporter.client.Export;
import org.timepedia.exporter.client.Exportable;
import com.anstis.plugincommon.shared.Person;
import com.anstis.plugincommon.shared.PluginCallback;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
@Export
public class CommonService implements Exportable {
public void displayPerson(Person person) {
//The below shows 'person' *IS* null
Window.alert("CommonService.displayPerson.person is "
+ (person != null ? "not " : "") + "null");
Window.alert("Name=" + person.getName());
}
}
Person.java
package com.anstis.plugincommon.shared;
import org.timepedia.exporter.client.Exportable;
public class Person implements Exportable {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
【问题讨论】:
-
我读到您想将实例从一个 GWT 模块传递到另一个。我正在为此构建一个小项目。现在它仅限于接口,但无论如何都可以使用。在code.google.com/p/gwt-remote
-
顺便说一句:我的 stackoverflow 个人资料包括我的电子邮件。
标签: gwt jsni gwt-exporter