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);
}
}
java调用js函数的更多相关文章
- Java中通过脚本引擎调用js函数
import java.io.*; import javax.script.Invocable; import javax.script.ScriptEngine; import javax.scri ...
- VC与JavaScript交互(二) --- 调用JS函数
这一章,我们来动手实践VC调用JS函数. 我们动手写一个HTML,其中包含这样一段JS代码: //[html] <script type="text/javascript"& ...
- Android使用JNI(从java调用本地函数)
当编写一个混合有本地C代码和Java的应用程序时,需要使用Java本地接口(JNI)作为连接桥梁.JNI作为一个软件层和API,允许使用本地代码调用Java对象的方法,同时也允许在Java方法中调用本 ...
- js,JavaScript,a标签onclick传递参数不对,A标签调用js函数写法总结
错误示例: <a href="javascript:waterLineEdit(${goods.goods_id})" >修改 </a> <!-- 浏 ...
- [模拟回调] demo1模拟用字符串调用js函数 demo2模拟springmvc controller回调页面js函数
demo1. 模拟用字符串调用js 函数 function dataQuery() { var strFun = "testCallBack"; var strParam = &q ...
- 在动态THML语句中调用JS函数传递带空格参数的问题
刚刚遇到一个问题,调用js函数的参数里带空格,造成调用失败的问题. 部分代码如下: html+="<div><a href=javascript:confirm(&qu ...
- 服务器控件调用JS函数
是服务器端控件,不能在JS里直接调用,但可以在aspx.cs 里写方法可以调用JS函数,比如JS方法名称是check(), function check() { alert(document.ge ...
- form表单里的button调用js函数
近来发现一个特别奇怪的问题:在form表单里,button的onclick事件无法调用js函数.代码如下(这段代码放在form标签里): dropUpdateAddress调用的js函数为: 这个时候 ...
- Java 调用 Javascript 函数的范例
在Java 7 以后,可以在Java代码中调用javascript中的函数,请看下面的例子: package com.lee; import java.io.FileNotFoundException ...
随机推荐
- golang 数据类型之间的转换
一.基本数据类型之间的转换 1.string到int int,err:=strconv.Atoi(string) 2.string到int64 int64, err := strconv.ParseI ...
- python-Web-django-自定义标签
简化:@register.simple_tag def current_time(token): return datetime.datetime.now().strftime(str(token)) ...
- 关联SecureCRT
建议安装SecureCRT 8.1及其以上版本,打开http://EVE-NG地址/files/windows.zip下载eve-ng的Windows关联文件,然后使用文本编辑工具编辑win7_64b ...
- nginx集群+mysql数据同步
mysql集群配置在网站负载均衡中是必不可少的: 首先说下我个人准备的负载均衡方式: 1.通过nginx方向代理来将服务器压力分散到各个服务器上: 2.每个服务器中代码逻辑一样: 3.通过使用redi ...
- (转)C++ bitset用法
今天做题发现要用到bitset,找到一篇介绍的巨好的文章. 转载自:https://www.cnblogs.com/magisk/p/8809922.html C++的 bitset 在 bitset ...
- 【3.2】【mysql基本实验】mysql GTID复制(基于空数据的配置)
概述:本质上和传统异步复制没什么区别,就是加了GTID参数. 且可以用传统的方式来配置主从,也可以用GTID的方式来自动配置主从. 这里使用GTID的方式来自动适配主从. 需要mysql5.6.5以上 ...
- 从零开始,SpreadJS新人学习笔记【第4周】
数据绑定.脏数据和单引号前缀 本周,让我们一起来学习SpreadJS 的数据绑定.脏数据和单引号前缀,希望我的学习笔记能够帮助你们,从零开始学习 SpreadJS,并逐步精通. 在此前的学习笔记中,相 ...
- Websocket 突破最大长连接
为了测试机器能够最大的长连接个数,故写了一个js脚本,需要用node进行执行 var WebSocketClient = require('websocket').client; var size = ...
- # 「NOIP2010」关押罪犯(二分图染色+二分答案)
「NOIP2010」关押罪犯(二分图染色+二分答案) 洛谷 P1525 描述:n个罪犯(1-N),两个罪犯之间的仇恨值为c,m对仇恨值,求怎么分配使得两件监狱的最大仇恨值最小. 思路:使最大xxx最小 ...
- Yii2.0中使用Union查询,并使用join,支持分页
$query1 = Class1::find()->where($where); $query2 = Class1::find()->alias('a')->join('left j ...