java调用js函数

问题:js函数可能有多个,之间有相互调用关系,有jquery

jar包 org.mozilla.javascript-1.7.2

js   envjs-1.2.js

java代码

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date; import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.Validate;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.Function;
import org.mozilla.javascript.Scriptable; public class RhinoScaper {
private Context context;
private Scriptable scriptable; /**
* 初始化方法
*/
public void init() {
context = ContextFactory.getGlobal().enterContext();
scriptable = context.initStandardObjects(null);
context.setOptimizationLevel(-1);
context.setLanguageVersion(Context.VERSION_1_5);
context.evaluateString(scriptable, "var v='sssaass';"
+ "var print = function(v) {"
+ " java.lang.System.out.println(v);return v ;"
+ " };function hah(){return v }", "print", 1, null);
String files = "envjs-1.2.js,jquery.js,自己的js";
String[] file = files.split(",");
for (String f : file) {
evaluateJs(f);
}
} /**
* 调用函数
*
* @param functionName
* @param functionArags
* @return
*/
public Object invokFunction(String functionName, Object... functionArags) {
Validate.notNull(context, "context is null");
Validate.notNull(scriptable, "scriptable is null");
Function function = (Function) scriptable.get(functionName, scriptable);
Object call = function.call(Context.getCurrentContext(), scriptable,
function, functionArags);
// System.out.println("reslult = "+call.toString());
return call;
} public void evaluateJs(String f) {
Validate.notNull(context, "context is null");
Validate.notNull(scriptable, "scriptable is null");
FileReader in = null;
try {
in = new FileReader(f);
context.evaluateReader(scriptable, in, f, 1, null);
} catch (FileNotFoundException e1) {
// e1.printStackTrace();
throw new RuntimeException("unknown file " + f);
} catch (IOException e1) {
e1.printStackTrace();
} finally {
// close(in);
IOUtils.closeQuietly(in);
}
} public static void main(String[] args) throws Exception {
RhinoScaper rhinoScaper = new RhinoScaper();
rhinoScaper.init(); String param1= "";//参数1
     String param2="";//参数2
Object result = rhinoScaper.invokFunction("函数名称", param1,
param2);
System.out.println(result); } /**
* 加载js文件
*
* @param sourceName
* 名称
* @param classpathURI
* 文件路径
*/
public void loadJS(String sourceName, String classpathURI) {
Validate.notNull(context, "context is null");
Validate.notNull(scriptable, "scriptable is null");
String js = null;
InputStream inputStream = null;
try {
inputStream = getClass().getResourceAsStream(classpathURI);
js = IOUtils.toString(inputStream, "UTF-8");// 设置默认js文件编码为utf-8
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(inputStream);
}
context.evaluateString(scriptable, js, sourceName, 1, null);
} /**
* 加载js字符串
*
* @param source
* js字符串(注意处理js中由于变量名为throws,float类似名称导致的报错)
* @param sourceName
* 名称
*/
public void JSloadString(String source, String sourceName) {
Validate.notNull(context, "context is null");
Validate.notNull(scriptable, "scriptable is null");
context.evaluateString(scriptable, source, sourceName, 1, null);
}
}
上一篇:jq小demo—图片翻页展示效果 animate()动画


下一篇:Android与H5交互(java与js的交互)