本文目录一览:
如何在java中调用js方法
在Java中调用js文件中的function,传入调用参数,并获取返回值
js文件中的merge函数将两个参数a,b相加,并返回c。
// expression.js
function merge(a, b) {
c = a * b;
return c;
}
在Java代码中读取js文件,并参数两个参数,然后回去返回值。
package com.sinaapp.manjushri;
import java.io.FileReader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/** * Java调用并执行js文件,传递参数,并活动返回值 * * @author manjushri */
public class ScriptEngineTest {
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
String jsFileName = "expression.js"; // 读取js文件
FileReader reader = new FileReader(jsFileName); // 执行指定脚本
engine.eval(reader);
if(engine instanceof Invocable) {
Invocable invoke = (Invocable)engine; // 调用merge方法,并传入两个参数
// c = merge(2, 3);
Double c = (Double)invoke.invokeFunction("merge", 2, 3);
System.out.println("c = " + c);
}
reader.close();
}
}
输出结果:
c = 5.0
java怎么调用javascript
可参考:
package javaIjs;
/**
* @param args
*/
import java.io.FileReader;
import javax.script.Invocable;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
/**
* Java调用并执行js文件,传递参数,并活动返回值
*
* @author manjushri
*/
public class ScriptEngineTest {
public static void main(String[] args) throws Exception {
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("javascript");
String jsFileName = System.getProperty("user.dir")+"/src/javaIjs/expression.js";
// 读取js文件
FileReader reader = new FileReader(jsFileName);
// 执行指定脚本
System.out.println(engine.getContext().getReader());
engine.eval(reader);
if (engine instanceof Invocable) {
Invocable invoke = (Invocable) engine;
// 调用merge方法,并传入两个参数
// c = merge(2, 3);
Double c = (Double) invoke.invokeFunction("merge1", 21, 3);
System.out.println("c = " + c);
}
reader.close();
}
}
java程序中运行js脚本
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class ExecJs {
/**
* 记录日志类
*/
private Logger log = Logger.getLogger(ExecJs.class);
/**
* 后置处理,执行js脚本
* @param js
* @throws Exception
*/
public void execJs(String js, MapString,Object map) throws Exception {
if (log.isDebugEnabled()) {
log.debug("execJs js : " + js);
IteratorEntryString, Object it = map.entrySet().iterator();
while (it.hasNext()) {
EntryString, Object entry = (EntryString, Object) it.next();
log.info("EXECJS MAP : " + entry.getKey() + "---" + entry.getValue());
}// end while
}// end if
if ("".equals(js) || js == null) {
log.info("EXECJS ERROR : JAVASCRIPT CONTENT IS NULL");
} else if(map == null || map.size()=0){
log.info("EXECJS ERROR : MAP CONTENT IS NULL");
} else {
// 获取脚本引擎
ScriptEngineManager mgr = new ScriptEngineManager();
ScriptEngine engine = mgr.getEngineByName("javascript");
// 绑定数据
ScriptContext newContext = new SimpleScriptContext();
Bindings bind = newContext.getBindings(ScriptContext.ENGINE_SCOPE);
bind.putAll(map);
try {
engine.setBindings(bind, ScriptContext.ENGINE_SCOPE);
engine.eval(js);
} catch (Exception e) {
log.info("EXECJS EXCEPTION : EXECUTE JAVASCRIPT EXCEPTION", e);
throw (e);
}// end try
}// end if
}
}