package com.chinahrt.zyn.iteye; import javax.script.Bindings; import javax.script.Compilable; import javax.script.CompiledScript; import javax.script.ScriptEngine; import javax.script.ScriptEngineManager; import javax.script.ScriptException; import ognl.Ognl; import ognl.OgnlException; public class ScriptTest { /** * @param args * Administrator * 2013-6-19 下午5:26:06 * @throws ScriptException * @throws OgnlException */ public static void main(String[] args) throws ScriptException, OgnlException { //方式一,转换为javascript脚本,利用java的脚本引擎来执行 ScriptEngine engine = new ScriptEngineManager().getEngineByName("javascript"); Compilable compilable = (Compilable) engine; Bindings bindings = engine.createBindings(); //Local级别的Binding String script = "true&true||(true||false&(true||false))"; //定义函数并调用 CompiledScript JSFunction = compilable.compile(script); //解析编译脚本函数 Object result = JSFunction.eval(bindings); System.out.println(result); //调用缓存着的脚本函数对象,Bindings作为参数容器传入 //利用Ognl表达式来计算(需要导入ognl.jar) Object re = Ognl.getValue("true&&true||(true||false&&(true||false))",null); System.out.println(re); } }