是否可以在 ie 中从 JavaScript 创建任意 Java 对象?

发布于 2024-08-02 09:31:27 字数 439 浏览 8 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

葵雨 2024-08-09 09:31:27

您可以在 Internet Explorer 中执行的操作是将小程序加载到页面中,该页面公开执行您想要执行的操作的方法。您获得对该小程序的引用,然后调用该引用上的方法。

<applet id="myAppletId" name="myAppletName" ...>

var applet = document.getElementById('myAppletId');
var d = applet.getDateFromApplet();

在您的小程序中,您需要一个公共方法 getDateFromApplet() 来返回 java.util.Date

请注意,我所展示的应该可以工作,但自从我这样做以来已经很多年了(当时它在 NS4、6 和 IE 4+ 中工作)。我没有使用getElementById(),但是我使用了var applet = document.myAppletName;

另一个复杂之处是,如果您希望在页面加载时执行此操作,则小程序几乎肯定还没有准备好,这需要类似以下的代码:

function checkApplets() {
    var da = document.applets; // document.getElementsByName('applet');?

    if (da.length > 0) {
        for (var ii = da.length; ii-- > 0;) {
            if (!da[ii].isActive()) {
                window.timerId = setTimeout(checkApplets, 250);
                return;
            }
        }

        if (window.timerId) {
            clearTimeout(window.timerId);
        }
    }

    window.appletsLoaded = true;
}

最后,可能(应该)可以使用 < 来执行此操作;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.

<applet id="myAppletId" name="myAppletName" ...>

var applet = document.getElementById('myAppletId');
var d = applet.getDateFromApplet();

In your applet you'd need a public method getDateFromApplet() that returns a java.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 used var 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:

function checkApplets() {
    var da = document.applets; // document.getElementsByName('applet');?

    if (da.length > 0) {
        for (var ii = da.length; ii-- > 0;) {
            if (!da[ii].isActive()) {
                window.timerId = setTimeout(checkApplets, 250);
                return;
            }
        }

        if (window.timerId) {
            clearTimeout(window.timerId);
        }
    }

    window.appletsLoaded = true;
}

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.

时光清浅 2024-08-09 09:31:27

您正在使用 LiveConnect,它是 Mozilla 特定的 JavaScript/Java 桥接器。其他浏览器不支持它。

You are using LiveConnect which is a Mozilla-specific JavaScript/Java bridge. It is not supported in other browsers.

赴月观长安 2024-08-09 09:31:27

您可以通过 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

执笏见 2024-08-09 09:31:27

据我所知,我认为这是不可能的——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.

菊凝晚露 2024-08-09 09:31:27

从 Java 1.6 update 10 开始,跨语言 LiveConnect Bridge 附加一个Packages 对象指向页面内的小程序,就像 Firefox 中可用的 Packages 对象一样。因此,在至少有一个小程序的页面中,即使在 Internet Explorer 中,

new document.applets[0].Packages.java.util.Date().toString();

也会返回当前日期。还可以注册新的转换器,以便方便地访问在 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,

new document.applets[0].Packages.java.util.Date().toString();

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.

热情消退 2024-08-09 09:31:27

您可以使用 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文