是否可以在 ie 中从 JavaScript 创建任意 Java 对象?
LiveConnect 是一种连接 Java 和 JavaScript 的 Mozilla 技术。令人惊讶的是,他们又开始为最新版本的 Firefox 维护它。在 Firefox 中,
var d = new java.util.Date();
如果它不是 java.something,我可以编写例如或使用 Packages.
命名空间
var d = new Packages.java.util.Date();
,否则我可能会疯狂地调用 swing 中的工厂方法,
Packages.javax.swing.Box.createVerticalBox();
轻松实例化任何 Java 对象。有没有可以在 ie 中运行的等效程序?
LiveConnect is a Mozilla technology that bridges Java and JavaScript. Amazingly, they've started maintaining it again for recent versions of Firefox. In Firefox I can write e.g.
var d = new java.util.Date();
or use the Packages.
namespace if it's not a java.something
var d = new Packages.java.util.Date();
or I could go crazy and call a factory method in swing
Packages.javax.swing.Box.createVerticalBox();
easily instantiating any Java object. Is there an equivalent that works in ie?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以在 Internet Explorer 中执行的操作是将小程序加载到页面中,该页面公开执行您想要执行的操作的方法。您获得对该小程序的引用,然后调用该引用上的方法。
在您的小程序中,您需要一个公共方法
getDateFromApplet()
来返回java.util.Date
。请注意,我所展示的应该可以工作,但自从我这样做以来已经很多年了(当时它在 NS4、6 和 IE 4+ 中工作)。我没有使用
getElementById()
,但是我使用了var applet = document.myAppletName;
。另一个复杂之处是,如果您希望在页面加载时执行此操作,则小程序几乎肯定还没有准备好,这需要类似以下的代码:
最后,可能(应该)可以使用
< 来执行此操作;object>
标签,但正如我所说,自从我需要从客户端 JavaScript 以这种方式与 Java 小程序交互以来已经很多年了,所以我还没有测试它。What you can do in Internet Explorer is load an applet into the page which exposes methods that do the things you want to do. You get a reference to the applet, then invoke methods on that reference.
In your applet you'd need a public method
getDateFromApplet()
that returns ajava.util.Date
.Note that what I present should work, but it has been years since I did this (it worked in NS4, 6 and IE 4+ at the time). I didn't use
getElementById()
however, I usedvar applet = document.myAppletName;
.The other complication to this is that if you want this to execute on page load, the applet will almost certainly not be ready, which requires code something like:
Lastly, it might (should) be possible to do this with the
<object>
tag, but as I said, it has been years since I needed to interact with a Java applet in this way from client-side JavaScript, so I haven't tested it.您正在使用 LiveConnect,它是 Mozilla 特定的 JavaScript/Java 桥接器。其他浏览器不支持它。
You are using LiveConnect which is a Mozilla-specific JavaScript/Java bridge. It is not supported in other browsers.
您可以通过 JAVA 中 servlet 的 JSON 响应来提供对象的属性吗?由于 JS 可以轻松使用 JSON 并且它是轻量级的,因此它提供了一种从 Java 对象创建 JS 对象的简单方法。
另请参阅 Rhino JavaScript 引擎以及如何从 JavaScript 获取 Java 资源。
https://developer.mozilla.org/en-US/docs/Rhino_documentation
You could make the properties of your objects available through a JSON response from your servlet in JAVA? Since JS can readily use JSON and it is light weight it makes for an easy way to crate JS Objects from your Java Objects.
Also look at the Rhino JavaScript engine and making Java resources available from JavaScript.
https://developer.mozilla.org/en-US/docs/Rhino_documentation
据我所知,我认为这是不可能的——Java 和 Javascript 是完全不同的。恰好 JS API 包含一个 Date 对象,Java API 也是如此。
编辑: Java Scripting API (http://java.sun.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html#jsengine)似乎是最接近的你正在尝试做的事。
As far as I know, I don't think this is possible - Java and Javascript are completely different. It just so happens that the JS API contains a Date object, as does the Java API.
Edit: The Java Scripting API (http://java.sun.com/javase/6/docs/technotes/guides/scripting/programmer_guide/index.html#jsengine) seems to be the closest you can get to what you're trying to do.
从 Java 1.6 update 10 开始,跨语言 LiveConnect Bridge 附加一个Packages 对象指向页面内的小程序,就像 Firefox 中可用的 Packages 对象一样。因此,在至少有一个小程序的页面中,即使在 Internet Explorer 中,
也会返回当前日期。还可以注册新的转换器,以便方便地访问在 Java 虚拟机中运行的非 Java 语言。当然,JavaFX 实现了这样的桥梁。
在从 JavaScript 调用小程序之前,不需要等待小程序加载,但这可能是一个好主意。 Java 插件将使 JavaScript 等待,直到小程序完成加载或出现错误。一旦 Applet.init() 被调用,Applet 就有可能调用网页中的 JavaScript。
As of Java 1.6 update 10, the Inter-Language LiveConnect Bridge attaches a Packages object to applets within the page, just like the Packages object available in Firefox. So in a page with at least one applet, even in Internet Explorer,
returns the current date. It's also possible to register new converters for convenient access to non-Java languages running in the Java virtual machine. Of course JavaFX implements such a bridge.
It's not supposed to be necessary to wait for the applet to load before calling it from JavaScript but it's probably a good idea. The Java plugin will make JavaScript wait until the applet finishes loading or has an error. It is possible for the applet to call JavaScript in the web page as soon as Applet.init() is called.
您可以使用 DWR 或其他一些反向 ajax 库,它们允许您通过 Javascript 对象在服务器端创建 Java 对象。
You could possibly use DWR or some other reverse ajax library that will allow you to create Java objects on the server side via Javascript objects.