如何从Javascript(Java的1.8)访问Java枚举 [英] How to access Java Enums from Javascript (Java 1.8)

查看:233
本文介绍了如何从Javascript(Java的1.8)访问Java枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java 1.7之前,它的拆除,人们可以使用包来访问Java枚举从JavaScript的HTML页以下的方式来看待一个浏览器:

In Java 1.7, prior to it's removal, one could use 'Packages' to access Java Enums in the following way from Javascript on an HTML page viewed a browser:

var enumvar1 = document.appletid.Packages.com.mycompany.MyClass$MyEnumYesNo.YES
var enumvar2 = document.appletid.Packages.com.mycompany.MyClass$MyEnumYesNo.NO

我升级这些HTML页面使用Java 1.8(现在使用犀牛的JavaScript引擎),而我似乎无法弄清楚如何访问枚举成员。

I'm upgrading these HTML pages to use Java 1.8 (which now uses the Nashorn javascript engine), and I cannot seem to figure out how to access the Enum members.

我已经重写了Java小程序到一个新的MyClass的对象返回到JavaScript变量,我可以从JavaScript变量访问MyClass的所有方法和字段,但我想不通的语法来获得在枚举。该错误是类型属性为空/未定义。我试过拿着类的包名,类名,变量的各种组合,和小程序ID变量。

I've rewritten the Java applet to return a new MyClass object to a javascript variable, and I can access all the methods and fields in MyClass from the JavaScript variable, but I can't figure out the syntax to get at the Enums. The errors are of the type "property is null/undefined". I've tried various combinations of the package name, class name, variable holding the class, and applet ID variable.

我还没有发现对Java 1.8的例子在谷歌上搜索周围,但在阅读Oracle的犀牛文档,它意味着枚举可以访问。

I haven't found any examples for Java 1.8 in googling around, although in reading the Nashorn documentation from Oracle, it implies that Enums can be accessed.

有人能提供一个例子语法?也许一些简单,我只是俯瞰...

Could someone provide an example syntax? Probably something simple that I am just overlooking...

谢谢!

推荐答案

犀牛引入了全局函数 Java.type()与Java类的交互。的http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/api.html

Nashorn introduces global function Java.type() to interact with Java classes. http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/api.html

下面是一个例子,你如何使用它

Here is an example how you can use it

var retPolicy = Java.type("java.lang.annotation.RetentionPolicy");
print(retPolicy.RUNTIME);

另外,如果你正在寻找的方式来从犀牛迁移到犀牛考虑以下手册的 https://wiki.openjdk.java.net/display/Nashorn/Rhino+Migration+Guide

这篇关于如何从Javascript(Java的1.8)访问Java枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆