简体   繁体   English

从Java调用js方法

[英]Call js methods from java

I have requirement to execute javascript method (which does some reporting and dump a PDF file) from java class. 我需要从Java类执行javascript方法(该方法会进行一些报告并转储PDF文件)。

Facing issue on how to load the context path in java method and call js method directly from java. 面对如何在Java方法中加载上下文路径并直接从Java调用js方法的问题。

Eg. 例如。 I need the context path of js file like /webapp/js/orn/workflow/Engine.js same as we get from browser in my java class :- ocm\\src\\main\\java\\com\\interra\\orion\\util\\workflow\\ScriptEngineUtil.java 我需要像/webapp/js/orn/workflow/Engine.js这样的js文件的上下文路径,与我们从Java类中的浏览器获取的路径相同:-ocm \\ src \\ main \\ java \\ com \\ interra \\ orion \\ util \\ workflow \\ ScriptEngineUtil.java

Any thoughts on how to proceed, I tried using Rhino but got struck on how to context path. 关于如何进行的任何想法,我尝试使用Rhino,但对如何使用上下文路径感到震惊。

The available answers provide enough information to call javascript code using Nashorn, specially the duplicated one that suggests 31piy in the first comment. 可用的答案提供了足够的信息来使用Nashorn调用javascript代码,特别是在第一个注释中建议输入31piy的重复代码。

Then I assume that your real problem is to reach the javascript file that contains your code, present in your webapp folder. 然后,我认为您真正的问题是访问包含在您的webapp文件夹中的代码的javascript文件。 I can see some possible answers to that: 我可以看到一些可能的答案:

Here some fellows suggest possible ways to reach the file using Spring methods: Accessing files/directories in webapp folder in Spring You should consider if there is a way available to your particular framework 这里有些人建议使用Spring方法访问文件的可能方法: 在Spring中访问webapp文件夹中的文件/目录您应该考虑特定框架是否有可用的方法

However: is there a good reason for that file to be in your webapp folder? 但是:将该文件放在您的webapp文件夹中是否有充分的理由? Is its logic necessary at all in the client side? 在客户端,它的逻辑是否完全必要? Since you need the logic in the server side, I would not (even for prudence) expose it to the client of your application. 由于您需要服务器端的逻辑,因此即使出于谨慎考虑,我也不会将其公开给您的应用程序的客户端。 If it is not necessary in the client side, don't expose it. 如果在客户端没有必要,请不要公开它。 Just keep it, let's say, in your application's src/main/resources directory (assuming you build it with maven or gradle) and access it cleanly from your classloader: 假设将其保存在应用程序的src / main / resources目录中(假设您使用maven或gradle进行构建),并可以从类加载器中干净地访问它:

// Access the resource from the classloader
try {
    reader = new InputStreamReader(
        YourServletOrWhateverClass.class.getClassLoader().
            getResourceAsStream("your_javascript_file.js"), 
                Charset.forName("UTF-8")); 
    ScriptEngineManager manager = new ScriptEngineManager();
    ScriptEngine engine = manager.getEngineByName("JavaScript");
    engine.eval(reader);            

    Invocable inv = (Invocable) engine;
    inv.invokeFunction("your_function");
}
catch(NoSuchMethodException nsme) {
    nsme.printStackTrace();
}
catch(ScriptException se) {
    se.printStackTrace();
}
finally {
    try {
        reader.close();
    }
    catch(IOException ioe) {
        ioe.printStackTrace();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM