JSNI

5 views
Skip to first unread message

Magic

unread,
May 26, 2010, 6:29:47 AM5/26/10
to Google App Engine for Java on Russian
Кто работал с этим?
Мне надо вызвать из javascript'а java метод получается примерно так:

<script type="text/javascript">
function jsFunc() {
@xxxxxxxxxxxxxxxxxx.JSNIExample::staticFoo(Ljava/lang/String;)
("as"); }
</script>

на этом месте все и валится
Ошибка: reference to undefined XML name @com
Источник: http://127.0.0.1:2491/
Строка: 13

код класса:

public class JSNIExample {
String myInstanceField;
static int myStaticField;

void instanceFoo(String s) {
}

static void staticFoo(String s) {
RootPanel.get().add(new HTML(s));
}
}

Смотрел примеры:
http://googlewebtoolkit.blogspot.com/2008/07/getting-to-really-know-gwt-part-1-jsni.html
и на http://code.google.com/

что делаю не так?

Magic

unread,
May 26, 2010, 7:02:40 AM5/26/10
to Google App Engine for Java on Russian
Опять моя невнимательность - синтаксис типа
"@xxx.JSNIExample::staticFoo(Ljava/lang/String;)("as");"
доступен только в нативном коде, а для доступности в js методов надо
его опубликовать:

например так:
You can even create JavaScript-callable libraries from your GWT code.
This is a pretty neat trick:
package org.example.yourcode.format.client;
public class DateFormatterLib implements EntryPoint {

// Expose the following method into JavaScript.
private static String formatAsCurrency(double x) {
return NumberFormat.getCurrencyFormat().format(x);
}

// Set up the JS-callable signature as a global JS function.
private native void publish() /*-{
$wnd.formatAsCurrency =

@org.example.yourcode.format.client.DateFormatterLib::formatAsCurrency(D);
}-*/;

// Auto-publish the method into JS when the GWT module loads.
public void onModuleLoad() {
publish();
}
}

> Смотрел примеры:http://googlewebtoolkit.blogspot.com/2008/07/getting-to-really-know-g...
> и наhttp://code.google.com/
>
> что делаю не так?

Reply all
Reply to author
Forward
0 new messages