【问题标题】:JavaScript in Java code throwing exceptionJava代码中的JavaScript抛出异常
【发布时间】:2016-09-13 06:43:01
【问题描述】:

我正在尝试从 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) 在 行号 1 11:53:50,847 ERROR [Login] 在 catch 中抛出错误 部分 11:53:50,848 INFO [登录] 调用函数 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);                           }
                );              
                                                                                }
                );          
                                                                                }
                );      
                                                                        }
                );  
                                                                                }
                );
                                                        }

【问题讨论】:

  • 寻求帮助时,请将您的代码格式化为可读性。
  • 你试图调用的 JavaScript 函数依赖于 JQuery,而你没有做任何事情来让 JQuery 对它可用,所以它失败了。
  • 它说“jQuery”不可用。你知道jQuery 不是标准Javascript 的一部分,不是吗?您是否在脚本中包含了它的定义?

标签: javascript java jquery


【解决方案1】:

ScriptEngine 在完全独立的环境中运行 JavaScript,无法访问您的浏览器。看起来您正在尝试使用它在浏览器中显示某种弹出窗口;这行不通。如果需要远程控制浏览器,则需要使用不同的 API,而不是 ScriptEngine

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-28
    • 2012-10-30
    • 1970-01-01
    • 2013-08-08
    • 2021-10-21
    • 1970-01-01
    • 2015-11-22
    • 1970-01-01
    相关资源
    最近更新 更多