gpt4 book ai didi

java - 可以从 javascript 调用非静态 gwt java 方法

转载 作者:行者123 更新时间:2023-12-02 07:42:07 28 4
gpt4 key购买 nike

总体目标:从 javascript 调用一些非静态 java 方法

描述:在Java Applet代码中找到gwt在DOM上的小部件并调用它的java方法(非静态)


JSObject win = JSObject.getWindow(this);
JSObject doc = (JSObject) win.getMember("document");
JSObject gwtWidget = (JSObject) doc.call("getElementsByName", widgetName);

//todo: have possibility to call `exported` java method over here, smth like:
//Object[] params = new Object[1];
//params[0] = widgetName;
//Object result = gwtWidget.call("exportedJavaMethod", params);

//todo: or just call global ($wnd) static JSNI bridge method:
//Object result = win.call("exportedJavaMethod", params);
//

问题是:我可以通过小部件的 id 找到而不是小部件,但它是 DivElement,它没有任何导出的实例方法。

我的小部件类是可导出的(gwt-export):


@Export(value="somewidget")
public class SomeWidget extends SimplePanel implements ..., Exportable {
private final String id = "id_some_widget_" + count++;
private static int count = 0;

public SomeWidget () {
getElement().setAttribute("name", id);
getElement().setId(id);
}
...

public static Element getInstanceById(String elementId) {
Element someWidget= DOM.getElementById(elementId);
return someWidget;
}

public String getSomeInstancedData() {
return "useful_inner_data" + this.id;
}

例如,我想找到添加到 DOM 的具体小部件并在 JavaScript 中调用非静态方法 getSomeInstancedData() 。这可能吗?

假设类似:


var someWidget = document.getElementById(widgetId);
alert(someWidget.getSomeInstancedData());

//or:
var someWidgetExported = com.mypackage.widgets.somewidget.getInstanceById(listenerId);
alert(someWidgetExported.getSomeInstancedData());

在基本模块中我写:


ExporterUtil.exportAll();

有一个包含此小部件的 View (ViewWithSomeWidget.ui.xml):

...base:Formbase:SomeWidget ui:field="someWidget" ....../base:SomeWidget...

When SomeWidget does not implement Exportable, project runs well, but I couldn't call nonstatic methods of found widget's DIV element.

By the time, to solve the problem SomeWidget implements Exportable, but progect doesn't show View with the SomeWidget well because of ClassCastException using deferred binding:


ClassCastException: com.mypackage.widgets.SomeWidgetExporterImpl cannot be cast to com.mypackage.widgets.SomeWidget

那么,可能还有其他方法来查找小部件的 javascript 对象并调用它导出的 java 方法吗?无论如何,任何想法都值得赞赏。

最佳答案

您可以声明一个 JavaScript 函数,它将触发非静态方法。

例如:

package com.example;
public class Layout extends VLayout() {
private String nonStaticVar = "nonStaticVar";
public Layout() {
declareMethod(this);
}

//Method which declares non-static method in javascript
public native void declareMethod(Layout layout) /*-{
var layout = layout;
$wnd.doSomething = function(someString) {
layout.@com.example.Layout::doSomething(Ljava/lang/String;)(someString);
}
}-*/;

//Non static method which is being called
public String doSomething(String someString) {
//Do something, e.g. set var in this instantiated class, or output stuff
this.nonStaticVar = someString;
Window.alert(someString);
}
}

现在,打电话 做某事(“bla”);来自 javascript 将调用您的非静态方法。

关于java - 可以从 javascript 调用非静态 gwt java 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10448252/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com