从JavaScript调用GWT Java函数 [英] Calling GWT Java function from JavaScript

查看:149
本文介绍了从JavaScript调用GWT Java函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是GWT和JavaScript的新手。
有类似的问题,我试图遵循这种类型,但我一直在失败。

我有一个GWT应用程序,我需要从Javascript调用Java函数(特别是在href标记的onclick上)。以下是我所做的。


$ b

  public class JSNITest {

public static void handleAnchorClick(int a ,int b){
Window.alert(当前行和列是+ a ++ b);


public static native exportMyFunction()/ * - {
$ wnd.handleAnchorClick = function(param1,param2){
@ company.package.class .JSNITest :: handleAnchorClick(*)(参数1,参数2);
} - * /;

}

而在HTML中,

 < a href =javascript:handleAnchorClick(a1,a2);>链接< / a> 

(a1,a2)是两个整数变量在我的代码中。
我还在入口点函数中调用了EnclosingClass.exportMyFunction()。
我一直遇到各种异常(没有这样的类异常)。有人可以请我纠正吗?

问候

解决方案

让我解释一下更多关于将GWT内容导出到JS世界的信息。你有几个选择,但我会专注于三种方法。





0 - JsInterop :GWT维护人员正在使用新功能轻松将java方法导出到javascript,并打包javascript对象。该功能在2.7.0中非常实用,缺乏一些功能,但在2.8.0中几乎可以正常工作。请参阅设计文档和其他讨论。 / p>

[END]



<1-> JSNI :第一个是写你的自己的jsni,在这种情况下,你必须意识到你可能犯的错误。基本上这些错误是因为你必须知道如何处理类型。在你的情况下,如果你想获得一个JavaScript数组(就像你在下面的评论中所要求的那样),解决方案可以是:

  public static native exportMyFunction()/ *  -  {
$ wnd.handleAnchorClick = @ company.package.class.JSNITest :: handleAnchorClick(*);
} - * /;

public static void handleAnchorClick(JsArrayMixed args){
Window.alert(Current row and Column is+
args.getNumber(0)++ args.getNumber (1));
}

public void onModuleLoad(){
exportMyFunction();
}

// javascript code
window.handleAnchorClick([1,2])

请注意,JSNI只允许您传递原始类型(long除外)和 JavaScriptObject 对象。所以当传递一个javascript数组时,你必须用 JavaScriptObject 来接收它,就像这个例子。在这种情况下,由于JavaScript只使用数字类型,因此 args.getNumber 将始终返回一个double,并且必须在java中进行转换。



2- gwt-exporter 为了导出大型项目,或者当您需要处理复杂的对象和类时,我宁愿使用 gwt-exporter


$ b

 静态类MyClass实现可导出{
@Export($ wnd.handleAnchorClick)
public static void handleAnchorClick(double [] args){
Window.alert(Current row and Column is + args [0] ++ args [1]);



public void onModuleLoad(){
GWT.create(MyClass.class);
}

// javascript code
window.handleAnchorClick([1,2])

gwt-exporter将处理任何类型的原始类型(甚至很长) myfunc(long [] args),var- args myfunc(long ... args),它支持方法重载等等。



3- gwtquery 最后,如果您更喜欢 gwtquery ,则可以使用一种技术将函数属性添加到任何js对象,如 window

  // // GQuery Properties对象能够将一个Java Function对象
//包装到一个js属性中。
属性wnd = window.cast();
wnd.setFunction(handleAnchorClick,new Function(){
public void f(){
//获取js参数[] array
JsArrayMixed args = arguments(0 );
//获取参数的第一个元素[] array
JsArrayMixed ary = args.getObject(0);

Window.alert(Current row and Column is +
ary.getNumber(0)++ ary.getNumber(1));
}
});

// javascript code
window.handleAnchorClick([1,2])

使用gquery,您可以使用gwt JsArrayMixed 类,它总是以double形式返回一个数字,或者您可以使用 JsCache ,它允许将数字转换为java中的任何其他数值类型<($ J $ C>)((JsCache)ary.get(1,Integer.class)



作为总结,我宁愿使用 gwt-exporter 作为第一个选项,因为它专门处理这个问题。作为第二个选项,我会使用 gquery ,这是对gwt的严重补充。最后,我会尽量避免使用手写的 jsni ,但Javascript通常是问题和错误的来源(认为主要目标的gwt不是要处理js)。


I am a newcomer to GWT and JavaScript. There are similar question of this type I have tried to follow, but I keep failing.

I have a GWT app, I need to call a Java function from Javascript( on the onclick of a href tag, in particular.) Following is what I have done.

public class JSNITest {

 public static void handleAnchorClick(int a , int b) {
   Window.alert("Current row and Column is " + a + "  " + b);
 }

 public static native void exportMyFunction()/*-{
    $wnd.handleAnchorClick = function(param1,param2){
        @company.package.class.JSNITest::handleAnchorClick(*)(param1,param2);
 }-*/;

}

And in the HTML,

<a href="javascript:handleAnchorClick(a1,a2);">link</a> 

(a1 , a2) are two integer variables in my code. I have also called EnclosingClass.exportMyFunction() in the entry point function. I keep running into various kinds of exceptions(No Such class exception). Can someone please correct me?

Regards

解决方案

Let me explain a bit more about exporting GWT stuff to the JS world. You have several options to do that, but I will focus on three methods.

[EDITED]

0- JsInterop: GWT maintainers are working in a new feature to easily export java methods to javascript, and wrap javascript objects. The feature was very experimental in 2.7.0 lacking some features, but in 2.8.0 will be almost functional. Please take a look to the Design Document, and other discussions in the mailing list.

[END]

1- JSNI: The first one is to write your own jsni, in this case you have to be aware about the possible mistakes you could make. Basically these mistakes are because you have to know how to deal with types. In your case if you want to get a javascript array (like you are asking in your comment below), the solution could be:

public static native void exportMyFunction()/*-{
  $wnd.handleAnchorClick = @company.package.class.JSNITest::handleAnchorClick(*);
}-*/;

public static void handleAnchorClick(JsArrayMixed args) {
  Window.alert("Current row and Column is " +
                args.getNumber(0) + "  " + args.getNumber(1));
}

public void onModuleLoad() {
  exportMyFunction();
}

//javascript code
window.handleAnchorClick([1,2])

Note that JSNI only allows you to pass primitive types (except long) and JavaScriptObject objects. So when passing a javascript array, you have to receive it with a JavaScriptObject like in the example. In this case, since javascript only uses a type for numbers, args.getNumber will return always a double, and you have to convert in java.

2- gwt-exporter For exporting large projects, or when you need to handle complex objects and classes I'd rather use gwt-exporter

static class MyClass implements Exportable {
  @Export("$wnd.handleAnchorClick")
  public static void handleAnchorClick(double[] args) {
    Window.alert("Current row and Column is " +args[0] + "  " + args[1]);
  }
}

public void onModuleLoad() {
  GWT.create(MyClass.class);
}

//javascript code
window.handleAnchorClick([1,2])

gwt-exporter will deal with any kind of primitive types (even with long) myfunc(long[] args), with var-args myfunc(long...args), it supports method overload, and much more.

3- gwtquery Finally if you prefer gwtquery, you can use a technique to add function properties to any js object like window

// The GQuery Properties object is able to wrap a java Function object
// into an js property.
Properties wnd = window.cast();
wnd.setFunction("handleAnchorClick", new Function() {
  public void f() {
    // Get the js arguments[] array
    JsArrayMixed args = arguments(0);
    // Get the first element of the arguments[] array
    JsArrayMixed ary = args.getObject(0);

    Window.alert("Current row and Column is " +
                  ary.getNumber(0) + "  " + ary.getNumber(1));
  }
});

//javascript code
window.handleAnchorClick([1,2])

With gquery you can use the gwt JsArrayMixed class which always returns a number as a double, or you can use a JsCache which allows to convert numbers to any other numeric type in java ((JsCache)ary.get(1, Integer.class)

As a summary, I would rather use gwt-exporter as the first option because it is specialized in handling this problematic. As a second option, I would use gquery which is a serious complement to gwt. Finally, I would avoid to use hand-written jsni when possible, Javascript is normally a source of issues and mistakes (think that the main goal of gwt is not to deal with js).

这篇关于从JavaScript调用GWT Java函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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