在javascript代码中调用java方法 [英] calling java methods in javascript code

查看:148
本文介绍了在javascript代码中调用java方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



这里是我所说的例子:



在我的网页上我有一个javascript代码,这里有一些:

  function createChartControl(htmlDiv1)
{
//初始化甘特图数据结构
//项目1
var parentTask1 = new GanttTaskInfo(1,旧代码审查,新日期(2010年,5,11),208, 50,);
......................

我想创建一个java类内容方法来为这个javascript函数GanttTaskInfo提供数据。
for exemple函数获取名称,获取id和日期。
我认为这次我很清楚:D
我搜索了一种在javascript中调用java方法的方法,我找到了你说的applet,但我觉得它对我没用。
再次感谢

解决方案

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




  • 创建一个Web服务(例如使用 Tomcat

  • 从JavaScript调用其URL(例如 with jQuery or dojo)



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



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



Java代码:

  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(){returnHello; }
public OtherClass methodReturningObject(){return new OtherClass(); }
}

公共类OtherClass {
public void anotherMethod();
}

网页和JavaScript代码:

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


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.

here is an exemple of what im talking about:

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

    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

解决方案

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

  • create a web service (for example with Tomcat)
  • call its URL from JavaScript (for example with JQuery or 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.

Below is example from documentation. Look at 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:

<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>

这篇关于在javascript代码中调用java方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆