如何从 javascript 调用 java 方法

发布于 2024-10-14 18:47:43 字数 186 浏览 7 评论 0原文

正如标题中所述,我想要实现的目标如下:

  1. 创建 java 对象
  2. 将其传递给 JavaScript
  3. 在传递的对象上调用方法(例如 setter)
  4. 继续使用 java 中的对象

我正在使用包含在中的脚本爪哇。 如果有人能帮助我解决这个问题,我会很高兴。

As described in the title, what I am trying to achieve is the following:

  1. Create java object
  2. Pass it to JavaScript
  3. Invoke a method (a setter for instance) on the passed object
  4. Continue work with the object in java

I am using the scripting included in java.
I will be happy if someone can help me with this.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

a√萤火虫的光℡ 2024-10-21 18:47:43

如果您使用 ScriptEngine 框架,这非常简单。您可以通过两种方式将 Java 对象“传递”给 JavaScript:

  1. 您可以为 JavaScript 代码“播种”执行环境,并将 Java 对象安排在全局名称空间中“存在”
  2. 您可以将 Java 对象作为参数传递给 JavaScript 函数。

如果需要,您还可以从 JavaScript 访问 Java 构造函数并实例化 Java 对象。

首先,您必须为脚本引擎设置“绑定”。它就像一个 Map:

final Bindings globals = engine.createBindings();
globals.put("foo", yourObject);

现在,当 JavaScript 代码在该引擎中运行时,全局符号“foo”将充当对 Java 对象的引用。您可以根据需要绑定任意数量的引用。

如果您想将 Java 对象作为参数传递给 JavaScript 函数,您首先需要的是一种调用 JavaScript 函数的方法。为此,您可以使用“Invocable”接口公开的“invokeFunction”或“invokeMethod”方法:

final Object result = ((Invocable) engine).invokeMethod(context, methodName, arg, arg, ... );

“上下文”只是您希望 this 在函数中引用的内容的引用这就是所谓的。 “methodName”只是一个字符串,给出全局 JavaScript 函数的名称。

Java 类可通过其完全限定路径名供 JavaScript 环境使用:

var javaHashMap = new java.util.HashMap();

这将为您提供一个作为 JavaScript 变量的 Java HashMap 实例。

If you're working with the ScriptEngine framework, this is really easy. You can "pass" Java objects to JavaScript in two ways:

  1. You can "seed" the execution environment for JavaScript code and arrange for Java objects to just be "there" in the global namespace
  2. You can pass Java objects in as arguments to JavaScript functions.

You can also access Java constructors from JavaScript and instantiate Java objects if you want.

To do the first thing, you have to set up the "bindings" for the script engine. It's just like a Map:

final Bindings globals = engine.createBindings();
globals.put("foo", yourObject);

Now, when JavaScript code runs in that engine, the global symbol "foo" will serve as a reference to a Java object. You can bind in as many references as you like.

If you want to pass a Java object as a parameter to a JavaScript function, the first thing you need is a way to call a JavaScript function. To do that, you use the "invokeFunction" or "invokeMethod" method exposed by the "Invocable" interface:

final Object result = ((Invocable) engine).invokeMethod(context, methodName, arg, arg, ... );

The "context" there is just a reference to something you want this to refer to in the function that's called. The "methodName" is just a string giving the name of your global JavaScript function.

Java classes are available to the JavaScript environment via their fully-qualified pathnames:

var javaHashMap = new java.util.HashMap();

That would give you a Java HashMap instance as a JavaScript variable.

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