Rhino API-使用org.mozilla.javascript.Context访问js方法
Rhino API - Access js method using org.mozilla.javascript.Context?
如何访问此脚本中的get方法:
(function( global ){
var Result;
(Result = function( val ) {
this.tpl = val || '' ;
}).prototype = {
get: function ()
{
return 'text' ;
}
};
global.Result = Result ;
} ( window ) ) ;
我尝试过这种方式:
创建Window类和Result接口:
public interface Result{ public String get(); }
public class Window { public Result Result; }
调用js函数:
public void call() {
Context context = Context.enter();
ScriptableObject scope = context.initStandardObjects();
FileReader fileReader = new FileReader("file.js");
Object window = Context.javaToJS(new Window(), scope);
scope.put("window", scope, window);
context.evaluateReader(scope, fileReader, "test", 1, null);
context.evaluateString(scope, "Result = window.Result;", "test", 2, null);
context.evaluateString(scope, "result = Result.get();", "test", 3, null);
Object result = scope.get("result", scope);
System.out.println("'n" + Context.toString(result));
context.exit();
}
但是我无法从get函数得到返回结果:
它对我有效:
public class Result extends ScriptableObject{
@Override
public String getClassName() {
// TODO Auto-generated method stub
return "Result";
}
}
public class Window extends ScriptableObject {
private Result Result;
public Result getResult() {
return Result;
}
@Override
public String getClassName() {
return "Window";
}
}
public void call() {
Context context = Context.enter();
ScriptableObject scope = context.initStandardObjects();
FileReader fileReader = new FileReader("file.js");
Window window = new Window();
scope.put("window", scope, window);
scope.put("window.Result", window.getResult());
context.evaluateReader(scope, fileReader, "test", 1, null);
context.evaluateString(scope, "Result = window.Result;", "test", 1, null);
context.evaluateString(scope, "var myResult = new Result();", "test", 1, null);
context.evaluateString(scope, "r = myResult.get();", "test", 1, null);
Object result = scope.get("r", scope);
System.out.println("'n" + Context.toString(result));
context.exit();
}
相关文章:
- 访问布局信息是否也会导致浏览器重排
- Javascript,访问一个主要对象模块模式中的每个对象
- 如何访问声音管理器2创建的声音对象
- 在Twitter上用ie9中的空白src访问iframe的contentWindow
- JavaScript Pub/Sub属性访问问题
- 从JavaScript访问struts操作中的属性
- 是否可以从父类访问子类的属性
- 如何访问fastOpt.js
- 访问JSON对象内部的数组元素
- 从模块内部访问Express装载路径
- 难以访问的JS环境中的语法错误
- 如何从对象的原型方法访问JavaScript对象属性
- 访问json数组中的对象
- 通过javascript/html访问twitter共享iframe
- Dojo:访问dijit.form.Select中单击的项目
- Rhino API-使用org.mozilla.javascript.Context访问js方法
- 在尝试管理和访问不同组件和处理程序方法中的状态时,我可以使用RxJS与Redux/context吗
- 如何访问this.context.router (ReactJS)
- 如何在JavaScript的context promise的then块中访问输入值?
- 无法从CRM中的HTML web资源中访问Xrm.Page.context