在javascript代码中调用java方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6649125/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 12:22:30  来源:igfitidea点击:

calling java methods in javascript code

javajavascript

提问by Nadya Nux

i created a java class content method return a String, my question is how to call this function in my javascript code to use the returned value from the java method. I want to call client-side Java code embedded in browser.

我创建了一个java类内容方法返回一个字符串,我的问题是如何在我的javascript代码中调用这个函数来使用java方法的返回值。我想调用嵌入在浏览器中的客户端 Java 代码。

here is an exemple of what im talking about:

这是我在说什么的一个例子:

in my webpage i have a javascript code, here is some of it:

在我的网页中,我有一个 javascript 代码,这是其中的一些:

    function createChartControl(htmlDiv1)
{
    // Initialize Gantt data structures
    //project 1
    var parentTask1 = new GanttTaskInfo(1, "Old code review", new Date(2010, 5, 11), 208, 50, "");
......................

i want to create a java class content methods to provide data to this javascript function "GanttTaskInfo". for exemple function to get name, get id and date. well i think this time im clear :D i searched a way to call java methods in javascript, and i found applets as you said, but i think its not usefull to me. thanks again

我想创建一个 java 类内容方法来为这个 javascript 函数“GanttTaskInfo”提供数据。例如获取名称、获取 ID 和日期的函数。好吧,我想这次我很清楚:D 我搜索了一种在 javascript 中调用 java 方法的方法,我发现了您所说的小程序,但我认为它对我没有用。再次感谢

采纳答案by zacheusz

When it is on server side, use web services - maybe RESTful with JSON.

当它在服务器端时,使用 Web 服务 - 可能是带有 JSON 的 RESTful。

  • create a web service (for example with Tomcat)
  • call its URL from JavaScript (for example with JQueryor dojo)
  • 创建一个 Web 服务(例如使用Tomcat
  • 从 JavaScript 调用它的 URL(例如使用 JQuery或 dojo)

When Java code is in applet you can use JavaScript bridge.The bridge between the Java and JavaScript programming languages, known informally as LiveConnect, is implemented in Java plugin. Formerly Mozilla-specific LiveConnect functionality, such as the ability to call static Java methods, instantiate new Java objects and reference third-party packages from JavaScript, is now available in all browsers.

当 Java 代码在小程序中时,您可以使用 JavaScript 桥接器。Java 和 JavaScript 编程语言之间的桥梁,非正式地称为LiveConnect,是在 Java 插件中实现的。以前特定于 Mozilla 的 LiveConnect 功能,例如调用静态 Java 方法、实例化新 Java 对象和从 JavaScript 引用第三方包的能力,现在可在所有浏览器中使用。

Below is example from documentation. Look at methodReturningString.

以下是文档中的示例。看看methodReturningString

Java code:

爪哇代码:

public class MethodInvocation extends Applet {
    public void noArgMethod() { ... }
    public void someMethod(String arg) { ... }
    public void someMethod(int arg) { ... }
    public int  methodReturningInt() { return 5; }
    public String methodReturningString() { return "Hello"; }
    public OtherClass methodReturningObject() { return new OtherClass(); }
}

public class OtherClass {
    public void anotherMethod();
}

Web page and JavaScript code:

网页和 JavaScript 代码:

<applet id="app"
        archive="examples.jar"
        code="MethodInvocation" ...>
</applet>
<script language="javascript">
    app.noArgMethod();
    app.someMethod("Hello");
    app.someMethod(5);
    var five = app.methodReturningInt();
    var hello = app.methodReturningString();
    app.methodReturningObject().anotherMethod();
</script>

回答by Darin Dimitrov

Java is a server side language, whereas javascript is a client side language. Both cannot communicate. If you have setup some server side script using Java you could use AJAXon the client in order to send an asynchronous request to it and thus invoke any possible Java functions. For example if you use jQuery as js framework you may take a look at the $.ajax()method. Or if you wanted to do it using plain javascript, here's a tutorial.

Java 是一种服务器端语言,而 javascript 是一种客户端语言。两者无法沟通。如果您使用 Java 设置了一些服务器端脚本,您可以在客户端使用AJAX以向其发送异步请求,从而调用任何可能的 Java 函数。例如,如果您使用 jQuery 作为 js 框架,您可以查看该$.ajax()方法。或者,如果您想使用普通的 javascript 来完成,这里有一个教程