JDK1.6加入了对Script(JSR223)的支持。这是一个脚本框架,提供了让脚本语言来访问Java内部的方法。你可以在运行的时候找到脚本引 擎,然后调用这个引擎去执行脚本。这个脚本API允许你为脚本语言提供Java支持。另外,Web Scripting Framework允许脚本代码在任何的Servlet容器(例如Tomcat)中生成Web内容。 关于ScriptEngine和ScriptEngineManager的使用:
操作js首先需要ScriptEngine
在jdk中可以用3中方式获得:
mgr.getEngineByExtension(String extension)
mgr.getEngineByMimeType(String mimeType)
mgr.getEngineByName(String shortName)
getEngineByExtension可用参数: js
eg:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByExtension("js");
getEngineByMimeType可用参数:
application/Javascript
application/ecmascript
text/Javascript
text/ecmascript
eg: ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByMimeType("application/Javascript");
getEngineByName可用参数:
jsrhinoJavascriptJavascriptECMAScript//js和Javascript是它的扩展
eg:
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("Javascript");
具体解析js可以是加载js文件或者js 脚本字符串:
例如夹在js文件:
engine.eval_r(new FileReader(jsfile));
或者
engine.eval_r(jsstring);
解析js需要Invocable 类:首先ScriptEngine 记载js文件或者字符串(如上):
然后转化为Invocable 就可以用来执行我们的js函数:
eg:
engine.eval_r("function test(){return 'this is test js in java'}");Invocable inv = (Invocable) engine;String value = String.valueOf(inv.invokeFunction("test"));
以下测试部分来自:http://sharong.javaeye.com/blog/474476,看有人写了就转载过来了。感谢sharong!
以下是测试:
Java代码
privatestaticvoidtestUsingJDKClasses(ScriptEngine engine)throwsException {
// Packages是脚本语言里的一个全局变量,专用于访问JDK的package
String js ="function doSwing(t){var f=new Packages.javax.swing.JFrame(t);f.setSize(400,300);f.setVisible(true);}";
engine.eval_r(js);
// Invocable 接口: 允许java平台调用脚本程序中的函数或方法
Invocable inv = (Invocable) engine;
// invokeFunction()中的第一个参数就是被调用的脚本程序中的函数,第二个参数是传递给被调用函数的参数;
inv.invokeFunction("doSwing","Scripting Swing");
}
private static void testUsingJDKClasses(ScriptEngine engine) throws Exception {// Packages是脚本语言里的一个全局变量,专用于访问JDK的packageString js = "function doSwing(t){var f=new Packages.javax.swing.JFrame(t);f.setSize(400,300);f.setVisible(true);}";engine.eval_r(js);// Invocable 接口: 允许java平台调用脚本程序中的函数或方法Invocable inv = (Invocable) engine;// invokeFunction()中的第一个参数就是被调用的脚本程序中的函数,第二个参数是传递给被调用函数的参数;inv.invokeFunction("doSwing", "Scripting Swing");}
下面的代码段演示脚本语言如何实现Java的接口
Java代码
privatestaticvoidtestScriptInterface(ScriptEngine engine)throwsScriptException {
String script ="var obj = new Object(); obj.run = function() { println('run method called'); }";
engine.eval_r(script);
Object obj = engine.get("obj");
Invocable inv = (Invocable) engine;
Runnable r = inv.getInterface(obj, Runnable.class);
Thread th =newThread(r);
th.start();
}
private static void testScriptInterface(ScriptEngine engine) throws ScriptException {String script = "var obj = new Object(); obj.run = function() { println('run method called'); }";engine.eval_r(script);Object obj = engine.get("obj");Invocable inv = (Invocable) engine;Runnable r = inv.getInterface(obj, Runnable.class);Thread th = new Thread(r);th.start();}
演示如何在Java中调用脚本语言的方法
Java代码
privatestaticvoidtestInvokeScriptMethod(ScriptEngine engine)throwsException {
String script ="function helloFunction(name) { return 'Hello everybody,' + name;}";
engine.eval_r(script);
Invocable inv = (Invocable) engine;
String res = (String) inv.invokeFunction("helloFunction","Scripting");
System.out.println("res:"+ res);
}
private static void testInvokeScriptMethod(ScriptEngine engine) throws Exception {String script = "function helloFunction(name) { return 'Hello everybody,' + name;}";engine.eval_r(script);Invocable inv = (Invocable) engine;String res = (String) inv.invokeFunction("helloFunction", "Scripting");System.out.println("res:" + res);}
演示如何暴露Java对象为脚本语言的全局变量
Java代码
privatestaticvoidtestScriptVariables(ScriptEngine engine)throwsScriptException {
File file =newFile("test.txt");
engine.put("f", file);
engine.eval_r("println('Total Space:'+f.getTotalSpace())");
engine.eval_r("println('Path:'+f.getPath())");
}
http://hi.baidu.com/ab52113141986/blog/item/4005011baff632148618bfa9.html