package com.huike.hidp.system.customform.service;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import org.junit.Test;
import com.sun.script.javascript.RhinoScriptEngine;
public class MethodTest {
@Test
public void testInvokeScriptMethod() throws Exception{
ScriptEngine engine = new RhinoScriptEngine();
this.invokeScriptMethod(engine);
}
public void invokeScriptMethod(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);
}
}
import javax.script.Invocable;
import javax.script.ScriptEngine;
import org.junit.Test;
import com.sun.script.javascript.RhinoScriptEngine;
public class MethodTest {
@Test
public void testInvokeScriptMethod() throws Exception{
ScriptEngine engine = new RhinoScriptEngine();
this.invokeScriptMethod(engine);
}
public void invokeScriptMethod(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 Rhino脚本引擎的例子,演示了如何通过Rhino引擎执行JavaScript代码并调用其内部定义的方法。
1047

被折叠的 条评论
为什么被折叠?



