GWT JSNI调用小程序方法
我想将 Java 小程序添加到 GWT 页面并调用该小程序的一些方法。这可以在 javascript 中通过执行以下操作来实现:
document.applet_id.someAppletMethod("value");
但是,当我尝试在 GWT 中使用 JSNI 本机函数实现相同的想法时,它失败了。基本上它找不到applet对象。这是 JSNI 代码:
public native void callStringMethod(String methodName, String arg) /*-{
var temp = "document." + [email protected]_project.AppletWrapper::appletName + "." + methodName + "(\"" + arg + "\");";
eval(temp); //<----- FAIL
//SOME TEST CODE
$doc.applet_id.someAppletMethod("test value") //<----- FAIL as well
alert(typeof $doc.applet_id); //Undefined
alert(typeof document.applet_id); //Undefined
alert(typeof $wnd.applet_id); //Undefined
}-*/;
注意 1:我知道“document”不是 JSNI 使用的有效名称,您可以使用 $doc 代替(解释)。我不太知道如何在 eval() 语句中对其进行编码,因此编译器将 $doc 替换为正确的引用,并且生成的 javascript 包含用户指定的方法名称和参数。您可能知道,不可能仅混合输入 Java 变量和 Javascript (说明)
注 2:以下 JavaScript 从 Web 浏览器地址栏运行,
javascript:document.applet_id.someAppletMethod("asdf")
因此小程序位于页面上的文档对象下,我可以从 Javascript 访问它。 JSNI 不太行得通。
Note3:我通过子类化 GWT 的 HTML 类来将实际的小程序标记添加到面板中。大致意思是:
public AppletWrapper(String appletName, String jarName, String className) {
StringBuilder applet = new StringBuilder();
applet.append("<applet archive=\"").append(jarName).append("\" ");
applet.append("code=\"").append(className).append("\" ");
applet.append("name=\"").append(appletName).append("\" ");
applet.append("id=\"").append(appletName).append("\" ");
applet.append("width=\"100%\" height=\"450\">");
applet.append("Browser doesn't support Java");
applet.append("</applet>");
this.setHTML(applet.toString());
}
感谢您为实现此目的提供的任何帮助。
I want to add a Java applet to a GWT page and call some of the applet's methods. This is possible in javascript by doing:
document.applet_id.someAppletMethod("value");
However when I try to implement the same idea using JSNI native function in GWT it fails. Basically it cannot find the applet object. Here's the JSNI code:
public native void callStringMethod(String methodName, String arg) /*-{
var temp = "document." + [email protected]_project.AppletWrapper::appletName + "." + methodName + "(\"" + arg + "\");";
eval(temp); //<----- FAIL
//SOME TEST CODE
$doc.applet_id.someAppletMethod("test value") //<----- FAIL as well
alert(typeof $doc.applet_id); //Undefined
alert(typeof document.applet_id); //Undefined
alert(typeof $wnd.applet_id); //Undefined
}-*/;
Note1: I know "document" is not a valid name to be used from JSNI, you use $doc instead (explanation). I don't quite know how to encode this in eval() statement so the compiler replaces $doc with proper reference, and also the javascript generated contains the user specified method name and argument. As you may be aware it's not possible to just mix input Java variables and Javascript (explanation)
Note2: The following JavaScript runs from the web browser address bar
javascript:document.applet_id.someAppletMethod("asdf")
So the applet is there on the page, under document object and I can access it from Javascript. It's just not quite working from JSNI.
Note3: I'm adding the actual applet tag to a panel by subclassing GWT's HTML class. Along the lines of:
public AppletWrapper(String appletName, String jarName, String className) {
StringBuilder applet = new StringBuilder();
applet.append("<applet archive=\"").append(jarName).append("\" ");
applet.append("code=\"").append(className).append("\" ");
applet.append("name=\"").append(appletName).append("\" ");
applet.append("id=\"").append(appletName).append("\" ");
applet.append("width=\"100%\" height=\"450\">");
applet.append("Browser doesn't support Java");
applet.append("</applet>");
this.setHTML(applet.toString());
}
Thanks for any help on getting this working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
mayscript="mayscript"
添加到callStringMethod()
?mayscript="mayscript"
to the<applet>
tag.callStringMethod()
called after the applet is added to the page?