EngineScript和CompiledScript的使用
Jdk1.6开始就提供了动态脚本语言诸如JavaScript动态的支持。这无疑是一个很好的功能,毕竟Java的语法不是适合成为动态语言。而JDK通过执行JavaScript脚本可以弥补这一不足。这也符合“Java虚拟机不仅仅是Java一种语言的虚拟机“这一目标。ScriptEngine功能强大,基本能实现笔者所能想到的Java与JavaScript交互的需求。而且十分的方便简洁。jdk1.8又对S
·
Jdk1.6开始就提供了动态脚本语言诸如JavaScript动态的支持。这无疑是一个很好的功能,毕竟Java的语法不是适合成为动态语言。而JDK通过执行JavaScript脚本可以弥补这一不足。这也符合“Java虚拟机不仅仅是Java一种语言的虚拟机“这一目标。
ScriptEngine功能强大,基本能实现笔者所能想到的Java与JavaScript交互的需求。而且十分的方便简洁。jdk1.8又对ScriptEngine接口进行了一定改变,因此,在jdk1.8能运行的代码,在1.6中可能会报错,反之亦然。
@Test
public void testScript2() throws Exception{
ScriptEngine scriptEngine = new ScriptEngineManager().getEngineByName("javascript");
String script = "var msg = 'hello'; " +
"var number = 123; " +
"var array = ['a','b','c']; " +
"var json = {" +
" 'name':'zwh'," +
" 'subjson':{" +
" 'subname':'zwhsub'," +
" 'subage':23" +
" }" +
"}; ";
//获取js变量
scriptEngine.eval(script);
System.out.println(scriptEngine.get("msg"));
System.out.println(scriptEngine.get("number"));
ScriptObjectMirror array = (ScriptObjectMirror)scriptEngine.get("array");
System.out.println(array.getSlot(0));
ScriptObjectMirror json = (ScriptObjectMirror)scriptEngine.get("json");
System.out.println(json.get("name"));
ScriptObjectMirror subJson = (ScriptObjectMirror)json.get("subjson");
System.out.println(subJson.get("subname"));
//添加脚本
System.out.println("==============================");
String script2 = "msg +=' world';" +
"number += 5";
scriptEngine.eval(script2);
System.out.println(scriptEngine.get("msg"));
System.out.println(scriptEngine.get("number"));
ScriptObjectMirror array2 = (ScriptObjectMirror)scriptEngine.get("array");
System.out.println(array2.getSlot(0));
ScriptObjectMirror json2 = (ScriptObjectMirror)scriptEngine.get("json");
System.out.println(json2.get("name"));
ScriptObjectMirror subJson2 = (ScriptObjectMirror)json.get("subjson");
System.out.println(subJson2.get("subname"));
//传入参数
System.out.println("==============================");
String script3 = "Math.pow(a,b)";
Bindings bindings = scriptEngine.createBindings();
bindings.put("a",2);
bindings.put("b",3);
System.out.println(scriptEngine.eval(script3, bindings));
//调用方法
System.out.println("===============================");
String script4 = "function add(a,b){return a-b;}";
scriptEngine.eval(script4);
Invocable invocable = (Invocable)scriptEngine;
System.out.println(invocable.invokeFunction("add", new Object[]{3, 4}));
}
CompiledScript可以认为是对script解析一个缓存。
@Test
public void scriptTest(){
try{
ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript");
String script = "a > b";
CompiledScript compiledScript = ((Compilable)engine).compile(script);
Bindings bindings = engine.createBindings();
bindings.put("a",23);
bindings.put("b",1);
Object result = compiledScript.eval(bindings);
System.out.println(result);
String script2 = "a < b";
CompiledScript compiledScript2 = ((Compilable)engine).compile(script2);
bindings.put("a",233);
bindings.put("b",1);
Object result2 = compiledScript2.eval(bindings);
System.out.println(result2);
Object result3 = compiledScript.eval(bindings);
System.out.println(result3);
}catch(Exception e){
e.printStackTrace();
}
}
更多推荐
已为社区贡献6条内容
所有评论(0)