我创建了一个java类内容方法返回一个String,我的问题是如何在我的javascript代码中调用这个函数来使用java方法返回的值.我想调用嵌入在浏览器中的客户端Java代码.
这是我正在谈论的例子:
在我的网页上我有一个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, "");
......................
Run Code Online (Sandbox Code Playgroud)
我想创建一个java类内容方法来为这个javascript函数"GanttTaskInfo"提供数据.例如获取名称的函数,获取id和日期.我觉得这次我很清楚:我搜索了一种在javascript中调用java方法的方法,我发现你说的applet,但我觉得它对我没用.再次感谢
zac*_*usz 25
当它在服务器端时,使用Web服务 - 可能是RESTful with JSON.
当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() { return "Hello"; }
public OtherClass methodReturningObject() { return new OtherClass(); }
}
public class OtherClass {
public void anotherMethod();
}
Run Code Online (Sandbox Code Playgroud)
网页和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>
Run Code Online (Sandbox Code Playgroud)