ScriptEngineManager的使⽤说明
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;
import javax.script.SimpleBindings;
/**
* ScriptEngine engine = new ScriptEngineManager().getEngineByName("nashorn");
engine.eval(new FileReader("script.js"));
* @author chenyuanbao
*
*/
public class Test {
public static void main(String[] args) throws ScriptException {
exeVar();
exeFor();
exeCondition("0.01");
exeCondition1("8","8");
try {
putValue();
callJsFunction();
} catch (Exception e) {
e.printStackTrace();
}
}
//java将变量导⼊js脚本 2的8次⽅
public static void putValue() throws Exception{
String str="Math.pow(a,b)";
Map<String, Object>input=new TreeMap<>();
input.put("a",2);
input.put("b",8);
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = EngineByName("javascript");
System.out.println(engine.eval(str,new SimpleBindings(input)));
}
//调⽤js函数
public static void callJsFunction() throws Exception{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = EngineByName("javascript");
engine.eval("function add (a, b) {return a+b; }");
Invocable jsInvoke = (Invocable) engine;
Object res = jsInvoke.invokeFunction("add", new Object[] { 10, 5 });
js获取json的key和valueSystem.out.println(res);
}
public static void exeCondition1(String x,String y) throws ScriptException{
List<String> conditions = new ArrayList<>();
conditions.add("x==0");//如果写x=0 报错,x==0则正常。
conditions.add("x>0&&y<10");
conditions.add("x>=10&&y<20");
conditions.add("x>=20&&y<30");
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = EngineByName("javascript");
engine.put("x", x);
engine.put("y", y);
for(String script:conditions){
Boolean ret = (Boolean) engine.eval(script);
System.out.println(script+"="+ret);
}
}
}
public static void exeCondition(String val) throws ScriptException{
List<String> conditions = new ArrayList<>();
conditions.add("x==0");//如果写x=0 报错,x==0则正常。
conditions.add("x>0&&x<1");
conditions.add("x>=1&&x<2");
conditions.add("x>=2&&x<3");
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = EngineByName("javascript");
engine.put("x", val);
for(String script:conditions){
Boolean ret = (Boolean) engine.eval(script);
System.out.println(script+"="+ret);
}
}
public static void exeVar() throws ScriptException{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = EngineByName("javascript");
Double hour = (Double) engine.eval("var date = new Date();" + "Hours();");
System.out.println("hour : " + hour);
}
public static void exeFor() throws ScriptException{
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = EngineByName("javascript");
engine.put("name", "abcdefg");//倒置⼀个字符串
engine.eval("var output ='' ;" + "for (i = 0; i <= name.length; i++) { " + " output = name.charAt(i) + output" + "}");
String name = (String) ("output");
System.out.printf("被翻转后的字符串:%s \n", name);
}
//该函数测试Java与js对象
public static void getObject() throws Exception{
String str = " var obj=new Object(); "
+ " obj.info='hello world'; "
+ " Info=function(){ "
+ " return this.info; "
+ " }; ";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = EngineByName("javascript");
engine.eval(str);
//获取对象
ScriptObjectMirror obj=(ScriptObjectMirror) ("obj");
//输出属性
System.out.("info"));
System.out.("getInfo"));
str="Info()";
//执⾏⽅法
System.out.println(engine.eval(str));
}
//该函数测试Java获取JS变量值的能⼒
public static void getJsValue() throws Exception{
String str = " var msg='hello'; "
+ " var number = 123; "
+ " var array=['A','B','C']; "
+ " var json={ "
+ " 'name':'pd', "
+ " 'subjson':{ "
+ " 'subname':'spd' "
+ " ,'id':123 "
+ " } "
+ " }; ";
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = EngineByName("javascript");
ScriptEngine engine = EngineByName("javascript");
//执⾏语句
engine.eval(str);
str="msg+=' world';number+=5";
//再次执⾏
engine.eval(str);
//获取js变量msg(String类型)
System.out.("msg"));
//获取js变量msg(int类型)
System.out.("number"));
//获取js变量array(数组)
ScriptObjectMirror array=(ScriptObjectMirror) ("array"); //getSlot(int index)函数⽤于获取下标为index的值
System.out.Slot(0));
//获取js变量json(json类型)
ScriptObjectMirror json=(ScriptObjectMirror) ("json");
//get(String key)函数⽤于键key的值
System.out.("name"));
//获取js变量subjson(嵌套json类型)
ScriptObjectMirror subjson=(("subjson"); System.out.("subname"));
}
//参考⽂档
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论