Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to run JS in Java

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

这篇文章主要讲解了"Java里面怎么运行JS",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"Java里面怎么运行JS"吧!

1、开始环境构建

Java自JDK1.6以来,就支持JS的脚本执行,首先通过初始化脚本引擎管理器,通过这个管理器获取对应脚本的执行引擎,如下:

ScriptEngineManager factory = new ScriptEngineManager();ScriptEngine engine = factory.getEngineByName("JavaScript");

到这个地方,这个engine对象我们就可以随时随地的使用,下面开始幻化吧

2、Hello World

先从一个基本的程序开始,输出我们熟悉的Hello World

String script = "function hello(name) { return 'Hello,' + name;}"; engine.eval(script); Invocable inv = (Invocable) engine; String res = (String) inv.invokeFunction("hello", "World"); System.out.println("res:" + res);

第一行就是一个标准的JS函数,入参name,返回Hello+name这样一个形式

第二行就是把这个函数置入到引擎环境中

第四行调用hello方法,传入参数World,并且将返回值强制为String类型

3、变量

在引擎中设置一个变量有两种,第一是从java语言调用engine的put方法

engin.put("name","zhangsan")

第二种是在脚本里面var一个变量,再在engine里面进行配置

engin.eval("var name=lisi;");

在和Java打交道的时候,更多从Java方面获取对象参数,然后到JS里面去运算;所以在JAVA里面把变量准备好,包括入参和出参都进行设定,然后在JS使用这些变量,当然也可以传入参数,输出复杂对象最好是设定一个变量。举个例子:

//对上面的方法进行扩展 Map map = new HashMap();//一个哈希表 map.put("a", "abc");//向哈希表增加一个元素 engine.put("map", map);//把这个哈希表丢到JS引擎中去 //执行一段JS脚本,作用是把一个键值对为currentTime和当前时间丢到哈希表 engine.eval("map.put('currentTime','20161022');"); Student student=new Student();//这是一个普通的POJO student.setAge(12); student.setName("张三"); engine.put("student", student); String script = "function hello(name) { return 'Hello,' + map.get('currentTime')+map.get('a')+student.getName();}"; engine.eval(script); Invocable inv = (Invocable) engine; String res = (String) inv.invokeFunction("hello", "World"); System.out.println(res);4、JS里面使用JDK

在JS脚本里面使用JDK的方法需要增加Packages后面包名、类和类的方法

String js = "function getUUID(){var f=Packages.java.util.UUID.randomUUID();print(f)}"; engine.eval(js); Invocable inv = (Invocable) engine; inv.invokeFunction("getUUID");

很多时候,JS的功能不如Java强大,而这种JS中使用JAVA方法显得非常重要

5、JSON

JSON 作为对象的组织者介于JAva和JavaScript之间都能比较好的传输数据、组装数据,我们使用第三方JSON处理包来做一个例子

String jsonStr="{'key':'value','SubSets':[{'x':'abc'},{'y':'edf'}],'random':Math.random()},{'other':'info'}"; JSONObject json=new JSONObject(jsonStr); // engine.put("json", json); String script = "function hello(json) { return 'Hello,' + json.getJSONArray(\"SubSets\").getJSONObject(0).get(\"x\").toString();}"; engine.eval(script); Invocable inv = (Invocable) engine; String res = (String) inv.invokeFunction("hello", json); System.out.println(res);

首先是一个比较复杂的JSON对象,使用org.json.JSONObject进行封装,作为入参传递进入JS引擎的hello方法,该方法对JSON对象进行解析也是利用org.json.JSONObject的方法。

6、实现Java接口

JS实现java的接口,来完成具体业务

String script = "var obj = new Object();obj.run = function() { println('执行具体方法==='+name);}"; engine.put("name", "全局参数"); engine.eval(script); Object obj = engine.get("obj"); Invocable inv = (Invocable) engine; Runnable r = inv.getInterface(obj, Runnable.class); Thread th = new Thread(r); th.start();

此处使用Runnable接口开启一个新的线程,具体的业务逻辑封装在JS里面,业务需要返回值可以自定义一个接口,比如

public ReturnObj execute();,那么JS里面的obj.execute方法就是具体干活的地方。

7、其他

JS里面手动清除一个变量,或者对象使用delete方法

var a=10;a;//出现10delete(a);a;//出现Uncaught ReferenceError: a is not defined感谢各位的阅读,以上就是"Java里面怎么运行JS"的内容了,经过本文的学习后,相信大家对Java里面怎么运行JS这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report