如何将java脚本引擎代码转换为使用Rhino
我将javascript表达式嵌入到java程序中,如下所示 但是我现在意识到我需要直接使用Rhino而不是ScriptEngine,因为我需要保持Java1.5的兼容性,我认为我可以如下运行评估如何将java脚本引擎代码转换为使用Rhino,java,rhino,scriptengine,Java,Rhino,Scriptengine,我将javascript表达式嵌入到java程序中,如下所示 但是我现在意识到我需要直接使用Rhino而不是ScriptEngine,因为我需要保持Java1.5的兼容性,我认为我可以如下运行评估 Context context = Context.enter(); try { Scriptable scope = context.initStandardObjects(); String result = context.evaluateString(scope, expr,
Context context = Context.enter();
try {
Scriptable scope = context.initStandardObjects();
String result = context.evaluateString(scope, expr, null, 0, null);
return result
} finally {
Context.exit();
}
但是有什么办法可以替代呢
jsEngine.put(fieldname.getScriptVariable(), value);
要将值映射到变量,如果必须与java 1.5兼容,请至少使用雅加达项目的BSF框架。它类似于java 1.6中引入的java脚本框架,我认为您需要做以下几点:
Object wrappedObject = Context.javaToJS(yourObjectHere, scope);
ScriptableObject.putProperty(scope, "yourVariableNameHere", wrappedObject);
请参阅Rhino示例。了解这一点很有用,但我对javascript的使用仅限于上述问题,因此我现在不想介绍其他内容