ScriptEngine engine = new ScriptEngineManager()
.getEngineByName("javascript");
Compilable compilable = (Compilable) engine;
Bindings bindings = engine.createBindings(); // Local级别的Binding
String script = "function add(op1,op2){return op1+op2} add(a, b)"; //
// 定义函数并调用
CompiledScript JSFunction = compilable.compile(script); // 解析编译脚本函数
bindings.put("a", 1);
bindings.put("b", 2); // 通过Bindings加入参数
Object result = JSFunction.eval(bindings);
System.out.println(result); // 调用缓存着的脚本函数对象,Bindings作为参数容器传入
java执行javascript方法体,并获取返回值
最新推荐文章于 2024-07-20 03:02:11 发布