Rhino,从多个 javascript 文件添加代码
我正在使用 Rhino 在 Java 应用程序中嵌入一些 javascript。我按照Rhino网站上的示例,通过调用Context的evaluateString方法并将实际脚本作为字符串传递来执行脚本。
我有一大堆现有的 javascript 代码,我想利用它们。我不想将其全部连接成一个巨大的字符串并将其传递给evaluateString。我宁愿能够加载代码,以便我可以从传递到评估字符串的代码中调用它(有点像 Microsoft 脚本控件中的 AddCode 方法)。我想添加代码,就像我当前可以使用 ScriptableObject.putProperty 方法添加变量一样。
有办法做到这一点吗?有人可以提供代码片段或文档链接吗?谢谢!
I am embedding some javascript in a Java application using Rhino. I am following the example on the Rhino website, executing a script by calling the Context's evaluateString method and passing the actual script in as a String.
I have a whole bunch of existing javascript code that I would like to make use of. I don't want to concatenate it all into an enormous String and pass it in to evaluateString. I would rather be able to load the code in so that I can call it from the code that I do pass into evaluateString (kind of like the AddCode method works in Microsoft's scripting control). I would like to add code like I can currently add variables by using the ScriptableObject.putProperty method.
Is there a way to do this? Can someone provide a code snippet or a link to the documentation. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
来自文档和示例 看起来对先前评估的对象的引用是由 范围。
(Rhino 1.7 release 2)
我知道有些人直接使用Rhino来获取最新版本,但是Java 6 实现 可以像这样评估脚本:
From the documentation and examples it looks like references to previously evaluated objects are controlled by scopes.
(Rhino 1.7 release 2)
I know some people use Rhino directly to get the latest version, but the Java 6 implementation can evaluate scripts like this:
在我的代码中,我有这种需求(实用程序脚本等),我只是简单地将它们连接到一个巨大的 StringBuilder 中并对其进行评估(Java 6)。这是因为 javascript 无法执行 otherJSScript.someUsefulFunction() (没有 Java 包装对象)的唯一方法。
In my code I had that need (utility scripts and such), and I just simply concatenated them together in a giant StringBuilder and evaled it (Java 6). Its the only way since javascript can't do (without Java wrapper objects) otherJSScript.someUsefulFunction().