每次我从 Java 脚本调用小程序的函数时,它都会抛出未定义的异常。 我的谷歌搜索根本没有帮助我。
这是我现在托管的网站的链接: Host Site
这是我嵌入 Applet 的 html:
<object type="application/x-java-applet"
id="ClientApp" name="ClientApp"
archive="Cal.jar"
width="100" height="100">
<param name="code" value="Calendar_Algorithm" />
<param name="mayscript" value="true" />
</object>
这是我的 java 脚本代码:
function test(){
document.writeln("<p> "+"Test"+" </p>");
try{
var s=document.ClientApp.getGreeting();
document.writeln("<p> First: "+s+" </p>");
}catch(err){
document.writeln("<p>Error Caught 1: "+err.description+"</p>");
}
try{
var s=document.getElementById('ClientApp').getGreeting();
document.writeln("<p> Second: "+s+" </p>");
}catch(err){
document.writeln("<p>Error Caught 2: "+err.description+"</p>");
}
document.close();
}
我知道它会加载小程序,因为我可以看到 gui,如果它有帮助,那就是我的 init 函数
public void init() {
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JLabel lbl = new JLabel(getGreeting());
add(lbl);
}
});
} catch (Exception e) {
System.err.println("createGUI didn't complete successfully");
}
}
这里还有我的完整代码的链接 Code
我感觉这个错误非常明显,但我就是看不到它。
任何帮助都会很棒!
附注 Applet 类文件现在位于已签名的 jar 文件中。
此外,这将被放置在 tomcat 服务器的 webapps 文件夹中,但我当前正在将其作为本地文件访问。
最佳答案
来自http://www.w3.org/TR/html401/struct/objects.html#h-13.4 、关于对象属性:
“该属性命名包含小程序状态的序列化表示的资源。” 我预测这不是您想要的。
此外,如果您使用的是 Firefox Mac,则需要 Mayscript 参数(JS-2-Java 交互)LiveConnect 才能工作。
一种有效的部署小程序的方法:
<object type="application/x-java-applet" width="100" height="100">
<param name="codebase" value="/applet_dir" />
<param name="code" value="Calendar_Algorithm" />
<param name="mayscript" value="true" />
</applet>
如果您没有启用 Java 控制台,那么您绝对应该这样做。它在 Java 控制面板高级设置下启用。
顺便说一句,在 Chrome Linux 中它可以工作!在 Firefox Linux 中则不然。 Firefox 不喜欢同时指定对象和代码参数并且类名不同。
LiveConnect 存在缺陷,尤其是在 Mac 上。有关概述,请查看:applets-missing-information-about-liveconnect-and-deployment基本上,您需要知道 LiveConnect 的哪些部分可以使用,哪些部分不可以。
关于java - 从 Javascript 调用 Applet 函数时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5371059/