日期:2014-05-20 浏览次数:20808 次
String formula = "x=a*b";
String x = "Result amount";
String a = "input";
String b = "precent";
System.out.println(formula.replace("x", x).replace("=a","="+a).replace("*b","*"+b));
ScriptEngineManager factory = new ScriptEngineManager();
ScriptEngine engine = factory.getEngineByName("JavaScript");
String script=" function hello(){";
script+=" var x='Result amount';";
script+=" var a='input';";
script+=" var b='precent'; ";
script+=" return (x+'='+a+'*'+b);";
script+=" }";
engine.eval(script);
Invocable inv = (Invocable) engine;
Object obj = inv.invokeFunction("hello");
System.out.println(obj);