本文介绍在 java 环境中执行 javascript 脚本的简单使用,具体包含以下内容
java 8 中的内置 nashorn javascript 引擎介绍rhino javascript 引擎介绍以及对 xml 的处理介绍nashorn 是 java 8 中内置的 javascript 引擎,无需加入任何依赖。
nashorn 基本使用步骤如下
new 出 scriptenginemanager 对象通过 scriptenginemanager 对象中的 getenginebyname 方法获取指定的 javascript 引擎,返回 scriptengine 对象java 8 中的职场潜规则默认的 javascript 引擎包括:[nashorn, nashorn, js, js, javascript, javascript, ecmascript, ecmascript]通过 scriptengine 对象的 eval 方法来执行 javascript 脚本。javascript 脚本中没有函数,没有返回值,通过 print 输出内容,这时需要通过 printwriter 获取读取脚本中的 print 输出,具体如下
@test public void test_nashorn() { try { scriptengine engine = new scriptenginemanager().getenginebyname("nashorn"); scriptcontext scriptcontext = engine.getcontext(); stringwriter stringwriter = new stringwriter(); printwriter printwriter = new printwriter(stringwriter); scriptcontext.twriter(printwriter); string jsstring = "var obj=json.par('{\\\"data\\\":\\\"7155\\\",\\\"sign\\\":\\\"success\\\",\\\"message\\\":null}');print(obj.sign==\"success\");"; stringwriter = new stringwriter(); printwriter = new printwriter(stringwriter); scriptcontext.twriter(printwriter); engine.eval(jsstring); system.out.println(string.format("1 result = %s",stringwriter.tostring())); jsstring = "var obj=json.par('{\\\"data\\\":\\\"7157\\\",\\\"sign\\\":\\\"success\\\",\\\"message\\\":null}');print((function getdata() { return obj.data;})())"; stringwriter = new stringwriter(); printwriter = new printwriter(stringwriter); scriptcontext.twriter(printwriter); engine.eval(jsstring); system.out.println(string.format("2 result = %s",stringwriter.tostring())); jsstring = "var obj=json.par('{\\\"data\\\":\\\"7157\\\",\\\"sign\\\":\\\"success\\\",\\\"message\\\":null}');print((function getdata() { return obj.data;})())"; stringwriter = new stringwriter(); printwriter = new printwriter(stringwriter); scriptcontext.twriter(printwriter); engine.eval(jsstring); system.out.println(string.format("3 result = %s",stringwriter.tostring())); } catch (exception e) { e.printstacktrace(); } }
javascript 脚本是一个匿名函数并且有返回值,通过 eval 函数可以直接
@testpublic void test_js_function_return() { try { scriptengine engine = new scriptenginemanager().getenginebyname("nashorn"); string jsfunction = "(function(){var obj=json.par('{\\\"data\\\":\\\"7155\\\",\\\"sign\\\":\\\"success\\\",\\\"message\\\":null}');return obj.sign==\"success\"})();"; boolean result = (boolean) engine.eval(jsfunction); system.out.println(result); } catch (exception e) { e.printstacktrace(); }}
javascript 脚本中有变量,有多个函数,具体如下
@testpublic void test_invoke_js_function() { try { scriptengine engine = new scriptenginemanager().getenginebyname("nashorn"); /* var obj = json.par('{\"data\":\"7155\",\"sign\":\"success\",\"message\":null}'); function checksign() { return obj.sign == 'success' } function getdata() { return obj.data } function calculate(a, b) { return a + b } */ string jsfunction = "var obj=json.par('{\\\"data\\\":\\\"7155\\\",\\\"sign\\\":\\\"success\\\",\\\"message\\\":null}');function checksign(){return obj.sign=='success'}function getdata(){return obj.data}function calculate(a,b){return a+b}"; engine.eval(jsfunction); invocable invocable = (invocable) engine; object result = invocable.invokefunction("checksign", null); system.out.println(result); result = invocable.invokefunction("getdata", null); system.out.println(result); result = invocable.invokefunction("calculate", 2, 5); system.out.println(result); } catch (exception e) { e.printstacktrace(); 理论经济学 }}
输出如下true71557.0
这种情况是读取 ja世界上最难的语言vascript 文件,并执行,具体如下
src/test/resources/test.js@testpublic void test_invoke_js_file() { try { scriptengine engine = new scriptenginemanager().getenginebyname("nashorn"); engine.eval(new filereader(testjngine.class.getresource("/test.js").getpath())); invocable invocable = (invocable) engine; object result = invocable.invokefunction("checksign", null); system.out.println(result); result = invocable.invokefunction("getdata", null); system.out.println(result); result = invocable.invokefunction("calculate", 2, 5); system.out.println(result); } catch (exception e) { e.printstacktrace(); }}
文件内容和上面一样。
默认的 nashorn 引擎是无法解析 xml 的,像 domparr 这样的对象是浏览器内置的组件。
这里可以通过 maven 依赖 rhino 引擎来处理 xml。
rhino maven 依赖如下<dependency> <groupid>cat.inspiracio</groupid> <artifactid>rhino-js-engine</artifactid> <version>1.7.10</version></dependency>
使用的步骤和其他 javascript 引擎一样,引擎的名称为 rhino.
这里通过读取文件的方式来加载和解析 javascript 脚本,脚本中是对一段 xml 的解析的过程。
src/test/resources/xml.js 文件内容如下print("----------------------------------------");var e = new xml('<employees> <employee id="1"><name>joe</name><age>20</age></employee> <employee id="2"><name>sue</name><age>30</age></employee> </employees>');// 获取所有的员工print("获取所有的员工:\n" + e..name);// 名字叫 joe 的员工print("名字叫 joe 的员工:\n" + e.employee.(name == "joe"));// 员工的id 为 1 和 2print("员工的id 为 1 和 2:\n" + e.employee.(@id == 1 || @id == 2));// 员工的id 为 1print("员工的id 为 1: " + e.employee.(@id == 1).name);print("----------------------------------------");
执行如下@testpublic void test_rhino_file_js() { try { scriptengine engine = new scriptenginemanager().getenginebyname("rhino"); scriptcontext scriptcontext = engine.getcontext(); stringwriter stringwriter = new stringwriter(); 温州职业技术学校 printwriter printwriter = new printwriter(stringwriter); scriptcontext.twriter(printwriter); engine.eval(new filereader(testjngine.class.getresource("/xml.js").getpath())); system.out.println(string.format("xml result = %s",stringwriter.tostring() )); } catch (exception e) { e.printstacktrace(); }}
输出如下xml result = ----------------------------------------all the employee names are:<name>joe</name><name>sue</name>the employee named joe is:<employee id="1"> <name>joe</name> <age>20</age></employee>employees with ids 1 & 2:<employee id="1"> <name>joe</name> <age>20</age></employee><employee id="2"> <name>sue</name> <age>30</age></employee>name of the the employee with id=1: joe----------------------------------------
<ccardprocesssyncrespon> <retcode>0</retcode> <message>操作成功!</message> <refundcycle /> <epayrefundcyclemin>1</epayrefundcyclemin> <epayrefundcyclemax>7</epayrefundcyclemax> <epayrefundcycleunitf /></ccardprocesssyncrespon>
具体测试如下@testpublic void test_rhino() { try { scriptengine engine = new scriptenginemanager().getenginebyname("rhino"); string jsstring = jsstring = "var obj=new xml('<ccardprocesssyncrespon> <retcode>0</retcode> <message>操作成功!</message> <refundcycle /> <epayrefundcyclemin>1</epayrefundcyclemin> <epayrefundcyclemax>7</epayrefundcyclemax> <epayrefundcycleunitf /> </ccardprocesssyncrespon>');print(obj.message == '操作成功!');"; scriptcontext scriptcontext = engine.getcontext(); stringwriter stringwriter = new stringwriter(); printwriter printwriter = new printwriter(stringwriter); 嘴巴scriptcontext.twriter(printwriter); engine.eval(jsstring); system.out.println(string.format("xml result = %s",stringwriter.tostring() )); } catch (exception e) { e.printstacktrace(); }}
上面的输出结果如下
xml result = true
本文发布于:2023-04-05 04:08:19,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/3cda7dc871abbed002e78f6d9f2bb4e5.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:javascript脚本怎么运行(js脚本编写教程).doc
本文 PDF 下载地址:javascript脚本怎么运行(js脚本编写教程).pdf
留言与评论(共有 0 条评论) |