我正在尝试从Java代码调用JavaScript函数。我的代码如下:
ScriptEngineManager manager = new ScriptEngineManager();
ScriptEngine engine = manager.getEngineByName("JavaScript");
String script = baseUrl + "UIConfigurator/includes/js/DataCaptureFramework.js";
try {
LOGGER.info("evaluating engine");
LOGGER.info(script);
URL url1 = new URL(script);
LOGGER.info("url1 "+url1.getPath()+url1);
BufferedReader in = new BufferedReader(
new InputStreamReader(url1.openStream()));
String inputLine = null;
StringBuffer buffer = new StringBuffer();
while ((inputLine = in.readLine()) != null)
buffer.append(inputLine);
LOGGER.info(buffer.toString());
engine.eval(buffer.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
LOGGER.error(e.toString());
LOGGER.error("Throwing error in the catch part");
}
Invocable inv = (Invocable) engine;
try {
LOGGER.info("invoking function");
inv.invokeFunction("displayDataCapturePopUp("+user.getOrgId()+","+user.getId()+","+session.getLoginType()+");");
} catch (NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ScriptException e) {
// TODO Auto-generated catch block
LOGGER.error("throwing error in catch block of invoking engine and function");
}
当我运行此代码时,它会给我以下错误:
11:53:50,847错误[登录] javax.script.ScriptException: sun.org.mozilla.javascript.internal.EcmaError:ReferenceError: “jQuery”没有定义。 (#1)in 第1 11:53:50,847错误[登录]捕获中的投掷错误 第11:53:50,848条信息[登录]调用函数11:53:50,848错误 [STDERR] java.lang.NoSuchMethodException:没有这样的方法: displayDataCapturePopUp(301,1373864,0);
我尝试过很多东西,但仍然无法摆脱这种情况。请告诉我我做错了什么。我试图从JavaScript调用以下函数。
function displayDataCapturePopUp(orgId,userId,loginTypeId){ jQuery(" <link/>", {rel: "stylesheet",type: "text/css",href:
"/UIConfigurator/includes/css/librarycss/jquery-ui.min.css"}).appendTo("head"); jQuery("<link/>", {rel: "stylesheet",type: "text/css",href:
"/UIConfigurator/includes/css/DataCaptureFramework.css"}).appendTo("head"); jQuery("<link/>", {rel: "stylesheet",type: "text/css",href:
"/UIConfigurator/includes/css/librarycss/tooltipster.css"}).appendTo("head"); jQuery("<link/>", {rel: "stylesheet",type: "text/css",href:
"/UIConfigurator/includes/css/librarycss/jquery.alerts.css"}).appendTo("head");
jQuery.getScript('/UIConfigurator/includes/js/UiAjaxInteraction.js',function() {
jQuery.getScript('/iONjsLib/js/jquery-ui-1.10.4.min.js',function() {
jQuery.getScript('/iONjsLib/js/jquery.tooltipster.min.js',function() {
jQuery.getScript('/UIConfigurator/includes/js/libraryjs/jquery.alerts.js',function() {
jQuery.getScript('/iONjsLib/js/jquery.blockUI.js',function() {
getDataCapturePopUpDetails(orgId,userId,loginTypeId); }
);
}
);
}
);
}
);
}
);
}
答案 0 :(得分:0)
ScriptEngine
在完全独立的环境中运行JavaScript,无法访问您的浏览器。看起来你正试图用它在浏览器中显示某种弹出窗口;这不行。如果您需要远程控制浏览器,则需要使用其他API,而不是ScriptEngine
。