$wnd and $doc Calling native JavaScript with JSNI

$wnd 是什么?

GWT provides the $wnd and $doc variables to refer to the window and document objects

GWT使用通过Java Native method使用它,声明一个native方法,将含有JavaScript的方法体注释。编译器将注释块内的内容逐字输出,使之与编译产生的JavaScript整合到一起。

Java调用JavaScript方法:

JSNI方法定义需要使用native关键字,并且需要在参数列表之后,结尾的分号之前定义。JSNI方法的开始使用 /*-{ , 结尾使用}-*/,例如:

public static native void alert(String msg) /*-{

$wnd.alert(msg);

}-*/;

这是gwt  Window 类中的源码--一个标准的jsni 方法, 我们可以看到,方法实现就是一行简单的javascript 代码. (这里没有用 alert 或者 window.alert 的原因是: gwt 代码运行在一个iframe中,如果直接用alert或者window.alert,引用的是iframe文档,而不是host page 文档). 经过这个方法包装,以后在gwt程序中使用 " Window.alert"  实际上就是调用了javascript 的 alert 方法,当然你也可以不用这个封装, 直接实用 $wnd.alert .

当上述方法在Java中调用的时候,实际上将调用Window的alert()方法,将传入的内容打印出来。在Hosted Mode下,断点可以调协在上述方法中,可以查看传入的参数。

参考如下:

http://www.javaeye.com/topic/365678

http://www.webreference.com/programming/java/toolkits/

The GWT makes ingenious use of Java's native methods with something called the JavaScript Native Interface. You declare native methods with commented-out bodies that contain JavaScript. When those native methods are compiled, the GWT incorporates the commented JavaScript. Here's an example of such a native method:

Calling native JavaScript with JSNI

http://www.ibm.com/developerworks/java/library/j-ajax4/

Visual-effects libraries are becoming increasingly popular in Web application development, whether their effects are used to provide subtle user-interaction cues or just to add polish. I'd like to add some eye-candy to the Weather Reporter application. GWT doesn't provide this type of functionality, but its JavaScript Native Interface (JSNI) offers a solution. JSNI lets you make JavaScript calls directly from GWT client Java code. This means that I can exploit effects from the Scriptaculous library (see Resources) or from the Yahoo! User Interface library, for example.

JSNI uses a cunning combination of the Java language's native keyword and JavaScript embedded in a special comment block. It's probably best explained by example, so Listing 10 shows a method that invokes a given Scriptaculous effect on an Element:

Listing 10. Invoking Scriptaculous effects with JSNI

private native void applyEffect(Element element, String effectName) /*-{

// Trigger named Scriptaculous effect
   $wnd.Effect[effectName](element);
}-*/;

This is perfectly valid Java code because the compiler sees only private native void applyEffect(Element element, String effectName);. GWT parses the contents of the comment block and outputs the JavaScript verbatim. GWT provides the $wnd and $doc variables to refer to the window and document objects. In this case, I'm simply accessing the top-level Scriptaculous Effect object and using JavaScript's square-bracket object-accessor syntax to invoke the named function specified by the caller. The Element type is a "magic" type provided by GWT that represents a Widget's underlying HTML DOM element in both Java and JavaScript code. Strings are one of the few other types that can be passed transparently between Java code and JavaScript via JSNI.

Now I have a weather report that fades in nicely when the data is returned from the server. The final touch is to re-enable the ZIP code TextBox when the effect has finished. Scriptaculous uses an asynchronous callback mechanism to notify listeners about the life cycle of effects. This is where things get a little more complex because I need to have JavaScript call back into my GWT client Java code. In JavaScript, you can invoke any function with an arbitrary number of arguments, so Java-style method overloading doesn't exist. This means that JSNI needs to use an unwieldy syntax to refer to Java methods to disambiguate possible overloads. The GWT documentation states this syntax as:

[instance-expr.]@class-name::method-name(param-signature)(arguments)

The instance-expr. part is optional because static methods are invoked without the need for an object reference.

最新文章

  1. Kubernetes 入门(3)集群安装
  2. LeetCode 217. 存在重复元素(哈希)
  3. Go Web 编程--如何确保Cookie数据的安全传输
  4. Mysql 扩展性设计之数据切分、那么数据切分后会带来哪些问题呢?比如分布式事务、数据的一致性、垂直切分和水平切分应用场景
  5. 插入u盘被计算机限制怎么回事,电脑不读u盘怎么处理,显示本次操作由于计算机的限制而被取消 请您与系统管理员联系,怎门回事?...
  6. The mbstring extendsion is missing,Please check you PHP configuration
  7. 计算机应用基础实训教程视频,计算机应用基础与实训教程第7章.ppt
  8. NOIP2013提高组华容道题解
  9. 曲速未来 揭示:发现假EOS钱包窃取用户资金
  10. 图解Transformer(完整版)
  11. 有负数c语言除法,C语言负数的除法和求余运算
  12. 利用爬虫获得疫情信息,并存入表格
  13. win10找不到网络里的计算机,Win10专业版找不到网络中的其他电脑
  14. 键盘部分失灵/被锁定解决办法
  15. 微信小程序评论的实现
  16. 抽象数据类型的三元组C语言,试仿照三元组的抽象数据类型分别写出数据类型复数和有理数的定义?...
  17. 【学透二叉树-二叉搜索树(二叉树)的最近公共祖先】
  18. Windows 计算机上查看 DNS 缓存的方法
  19. 14-射频校准的原理和设置
  20. dac 矩形波函数c语言,使用DAC接口生成矩形波

热门文章

  1. 计算机科学中的数学(一)
  2. 调用链监控 - Tracing - APM
  3. keep-alive的用法和作用
  4. [SRM603] WinterAndSnowmen
  5. Django中models模型(操作数据库)
  6. php 中 href,html中href什么意思
  7. java pacs上传服务_PACS千万家,好看耐用第一家---基于JAVA开发的跨平台PACS系统
  8. 【Qt开发】QThread中的互斥、读写锁、信号量、条件变量
  9. 小米android10升级版本,小米9 MIUI安卓10开发版已开始推送,建议大家不要去升级!...
  10. Radiology:影像组学:图像不仅仅是图片,它们还是数据