package cn.itcast.js;
import java.io.File;
import java.io.FileNotFoundException;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
/**
* @author handw
* @version 创建时间:2009-10-20 上午09:49:31
* 类说明:JDK1.6加入了对Script(JSR223)的支持。这是一个脚本框架,提供了让脚本语言来访问Java内部的方法。你可以在运行的时候找到脚本引擎,然后调用这个引擎去执行脚本。这个脚本API允许你为脚本语言提供Java支持。另外,Web Scripting Framework允许脚本代码在任何的Servlet容器(例如Tomcat)中生成Web内容。
*
*/
public class JdkJs {
/**
* @author handw
* @time 2009-10-20上午09:49:31
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
//JdkJs.test1();
//--------------------------
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByExtension("js");
//-------------------------------
//JdkJs.testUsingJDKClasses(engine);
//--------------------------
JdkJs.testScriptInterface(engine);
}
/**
* 关于ScriptEngine和ScriptEngineManager的使用:
* 超做js首先需要ScriptEngine
* 在jdk中可以用3中方式获得:
* @author handw
* @throws ScriptException
* @throws FileNotFoundException
* @throws NoSuchMethodException
* @time 2009-10-20上午09:51:06
*/
public static void test1() throws FileNotFoundException, ScriptException, NoSuchMethodException
{
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByExtension("js");
// mgr.getEngineByExtension(String extension) //getEngineByExtension可用参数:js
// mgr.getEngineByMimeType(String mimeType) // getEngineByMimeType可用参数:application/javascript;application/ecmascript;text/javascript;text/ecmascript;
// mgr.getEngineByName(String shortName) //getEngineByName可用参数:js;rhino;JavaScript;javascript;ECMAScript--js和javascript是它的扩展;
//具体解析js可以是加载js文件或者js 脚本字符串:
//例如夹在js文件:
String jsfile="";
//engine.eval(new FileReader(jsfile));
//或者
String jsstring="";
//engine.eval(jsstring);
// 解析js需要Invocable 类:首先ScriptEngine 记载js文件或者字符串(如上):
// 然后转化为Invocable 就可以用来执行我们的js函数:
// eg:
engine.eval("function test(){return 'this is test js in java'}");
Invocable inv = (Invocable) engine;
String value = String.valueOf(inv.invokeFunction("test"));
System.out.println(value);
}
/**
* 演示脚本语言如何使用JDK平台下的类,调用一个JDK平台的Swing窗口
* @param engine ScriptEngine实例
* @return void
* */
private static void testUsingJDKClasses(ScriptEngine engine) throws Exception {
// 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(js);
// Invocable 接口: 允许java平台调用脚本程序中的函数或方法
Invocable inv = (Invocable) engine;
// invokeFunction()中的第一个参数就是被调用的脚本程序中的函数,第二个参数是传递给被调用函数的参数;
inv.invokeFunction("doSwing", "Scripting Swing");
}
/**
* 演示脚本语言如何实现Java的接口,启动线程来运行script提供的方法
*
* @param engine ScriptEngine实例
* @return void
* */
private static void testScriptInterface(ScriptEngine engine) throws ScriptException {
String script = "var obj = new Object(); obj.run = function() { println('run method called'); }";
engine.eval(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中调用脚本语言的方法,通过JDK平台给script的方法中的形参赋值
*
* @param engine ScriptEngine实例
* @return void
* */
private static void testInvokeScriptMethod(ScriptEngine engine) throws Exception {
String script = "function helloFunction(name) { return 'Hello everybody,' + name;}";
engine.eval(script);
Invocable inv = (Invocable) engine;
String res = (String) inv.invokeFunction("helloFunction", "Scripting");
System.out.println("res:" + res);
}
/**
* 演示如何暴露Java对象为脚本语言的全局变量,同时演示将File实例赋给脚本语言,并通过脚本语言取得它的各种属性值
*
* @param engine ScriptEngine实例
* @return void
* */
private static void testScriptVariables(ScriptEngine engine) throws ScriptException {
File file = new File("test.txt");
engine.put("f", file);
engine.eval("println('Total Space:'+f.getTotalSpace())");
engine.eval("println('Path:'+f.getPath())");
}
}
文章出处:http://sharong.iteye.com/blog/474476
感谢吃西红柿的人。