`

GWT study

    博客分类:
  • GWT
阅读更多

    In the module XML file, you specify your application's entry point class. In order to compile, a GWT module must specify an entry point. If a GWT module has no entry point, then it can only be inherited by other modules. It is possible to include other modules that have entry points specified in their module XML files. If so, then your module would have multiple entry points. Each entry point is executed in sequence.


By default, StockWatcher uses two style sheets: the default GWT style sheet, standard.css (which is referenced via the inherited theme), and the application style sheet, StockWatcher.css which was generated by webAppCreator.


The host page references the path of JavaScript source code (generated by GWT) responsible for the dynamic elements on the page. The contents of the entire body element can be generated dynamically, for example, as it is with starter application.


The StockWatcher class implements the GWT interface EntryPoint. It contains the method onModuleLoad. Because the StockWatcher class is specified as the entry point class in StockWatcher's module definition, when you launch StockWatcher the onModuleLoad method is called.


The StockWatcher class inherits functionality via other GWT modules you included in StockWatcher's module definition (StockWatcher.gwt.xml). For example, when building the user interface, you'll be able to include types and resources from the package com.google.gwt.user.client.ui because it is part of the GWT core functionality included in the GWT module com.google.gwt.user.User.


A Root panel is the container for the dynamic elements of your application. It is at the top of any GWT user interface hierarchy. There are two ways you can use a Root panel, either to generate the entire body of the page or to generate specific elements embedded in the body.
The Root panel works by wrapping an element in the HTML host page. By default (that is, if you don't add any placeholders in the host page) the Root panel wraps the body element. However, you can wrap any element if you name it and then, when you call the Root panel, pass the name as a parameter.
RootPanel.get()             // Default. Wraps the HTML body element.
RootPanel.get("stockList")  // Wraps any HTML element with an id of "stockList"


For StockWatcher, you'll follow the preferred strategy. Rather than put links to the style sheets in the HTML host page, you'll use the module XML file. Then, when you compile StockWatcher, the GWT compiler will bundle all the static resources required to run your application including the style sheets. This mechanism is called Automatic Resource Inclusion.


In GWT, each class of widget has an associated style name (like gwt-Button) that binds it to a CSS style rule. This is the widget's primary style. Default values are defined for the primary style in the theme style sheet.
Type of Element            HTML Tag         CSS Selector
Buttons in static HTML and GWT-generated buttons    <button>         button
Only GWT-generated buttons          <button class="gwt-Button">    button.gwt-Button
Only my special GWT-generated button       <button class="gwt-Button my-button">  button.my-button


Dependent styles are powerful because they are automatically updated whenever the primary style name changes. In contrast, secondary style names that are not dependent style names are not automatically updated when the primary style name changes.
To do this, you'll use the addStyleDependentName method instead of the addStyleName method


If you launched the development mode server, you can run your application in production mode (after compiling it) by removing the gwt.codesvr parameter from the URL before loading the application.


The GWT RPC framework makes it easy for the client and server components of your web application to exchange Java objects over HTTP. The server-side code that gets invoked from the client is often referred to as a service. The implementation of a GWT RPC service is based on the well-known Java servlet architecture. Within the client code, you'll use an automatically-generated proxy class to make calls to the service. GWT will handle serialization of the Java objects passing back and forth—the arguments in the method calls and the return value.


In order to define your RPC interface, you need to write three components:


Define an interface (StockPriceService) for your service that extends RemoteService and lists all your RPC methods.
Create a class (StockPriceServiceImpl) that extends RemoteServiceServlet and implements the interface you created above.
Define an asynchronous interface (StockPriceServiceAsync) to your service to be called from the client-side code.


Every service implementation is ultimately a servlet, but rather than extending HttpServlet, it extends RemoteServiceServlet instead. RemoteServiceServlet automatically handles serialization of the data being passed between the client and the server and invoking the intended method in your service implementation.


Notice the @RemoteServiceRelativePath annotation. This associates the service with a default path relative to the module base URL.
In the <servlet-mapping> element, the url-pattern can be in the form of an absolute directory path (for example, /spellcheck or /common/login). If you specify a default service path with a @RemoteServiceRelativePath annotation on the service interface (as you did with StockPriceService), then make sure the url-pattern matches the annotation value.


To add an AsyncCallback parameter to all of our service methods, you must define a new interface as follows:


It must have the same name as the service interface, appended with Async (for example, StockPriceServiceAsync).
It must be located in the same package as the service interface.
Each method must have the same name and signature as in the service interface with an important difference: the method has no return type and the last parameter is an AsyncCallback object.


Often, you will need to integrate GWT with existing handwritten JavaScript or with a third-party JavaScript library. Occasionally you may need to access low-level browser functionality not exposed by the GWT class API's. The JavaScript Native Interface (JSNI) feature of GWT can solve both of these problems by allowing you to integrate JavaScript directly into your application's Java source code.


JSNI methods are declared native and contain JavaScript code in a specially formatted comment block between the end of the parameter list and the trailing semicolon. A JSNI comment block begins with the exact token /*-{ and ends with the exact token }-*/. JSNI methods are called just like any normal Java method. They can be static or instance methods.


generated GWT files. At compile time, the GWT compiler performs some syntax checks on the JavaScript inside the method, then generates interface code for converting method arguments and return values properly.


As of the GWT 1.5 release, the Java varargs construct is supported. The GWT compiler will translate varargs calls between 2 pieces of Java code. However, calling a varargs JavaScript method from Java will result in the callee receiving the arguments in an array.


When accessing the browser's window and document objects from JSNI, you must reference them as $wnd and $doc, respectively. Your compiled script runs in a nested frame, and $wnd and $doc are automatically initialized to correctly refer to the host page's window and document.


Calling Java methods from JavaScript is somewhat similar to calling Java methods from C code in JNI. In particular, JSNI borrows the JNI mangled method signature approach to distinguish among overloaded methods. JavaScript calls into Java methods are of the following form:
[instance-expr.]@class-name::method-name(param-signature)(arguments)
instance-expr. : must be present when calling an instance method and must be absent when calling a static method
class-name : is the fully-qualified name of the class in which the method is declared (or a subclass thereof)
param-signature : is the internal Java method signature as specified here but without the trailing signature of the method return type since it is not needed to choose the overload
arguments : is the actual argument list to pass to the called method


Calling Java constructors from JavaScript is identical to the above use case, except that the method name is alway new.


someTopLevelInstance.new InstanceInner(123) becomes @pkg.TopLevel.InstanceInner::new(Lpkg/TopLevel;I)(someTopLevelInstance, 123)
The enclosing instance of a non-static class is implicitly defined as the first parameter for constructors of a non-static class. Regardless of how deeply-nested a non-static class is, it only needs a reference to an instance of its immediately-enclosing type.


Static and instance fields can be accessed from handwritten JavaScript. Field references are of the form
[instance-expr.]@class-name::field-name


public class JSNIExample {
 
  String myInstanceField;
  static int myStaticField;
 
  void instanceFoo(String s) {
    // use s
  }
 
  static void staticFoo(String s) {
    // use s
  }
 
  public native void bar(JSNIExample x, String s) /*-{
    // Call instance method instanceFoo() on this
    this.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s );
 
    // Call instance method instanceFoo() on x
    x.@com.google.gwt.examples.JSNIExample::instanceFoo(Ljava/lang/String;)(s );
 
    // Call static method staticFoo()
    @com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);
 
    // Read instance field on this
    var val = this.@com.google.gwt.examples.JSNIExample::myInstanceField ;
 
    // Write instance field on x
    x.@com.google.gwt.examples.JSNIExample::myInstanceField = val + " and stuff";
 
    // Read static field (no qualifier)
    @com.google.gwt.examples.JSNIExample::myStaticField = val + " and stuff";
  }-*/;
}


Sometimes you need to access a method or constructor defined in GWT from outside JavaScript code. This code might be hand-written and included in another java script file, or it could be a part of a third party library. In this case, the GWT compiler will not get a chance to build an interface between your JavaScript code and the GWT generated JavaScript directly.
A way to make this kind of relationship work is to assign the method via JSNI to an external, globally visible JavaScript name that can be referenced by your hand-crafted JavaScript code.


package mypackage;
 
public MyUtilityClass
{
    public static int computeLoanInterest(int amt, float interestRate,
                                          int term) { ... }
    public static native void exportStaticMethod() /*-{
       $wnd.computeLoanInterest =
          $entry(@mypackage.MyUtilityClass::computeLoanInterest(IFI));
    }-*/;
}
Notice that the reference to the exported method has been wrapped in a call to the $entry function. This implicitly-defined function ensures that the Java-derived method is executed with the uncaught exception handler installed and pumps a number of other utility services. The $entry function is reentrant-safe and should be used anywhere that GWT-derived JavaScript may be called into from a non-GWT context.
On application initialization, call MyUtilityClass.exportStaticMethod() (e.g. from your GWT Entry Point). This will assign the function to a variable in the window object called computeLoanInterest.


Static String Internationalization
Static string initialization requires very little overhead at runtime and therefore is a very efficient technique for translating both constant and parameterized strings. It is also the simplest technique to implement. Static string internationalization uses standard Java properties files to store translated strings and parameterized messages, then implements strongly-typed Java interfaces to retrieve their values.


Dynamic String Internationalization
Dynamic string internationalization is slower than static string internationalization, but is very flexible. Applications using this technique look up localized strings in the module's host page; therefore, they do not need to be recompiled when you add a new locale. If you need to integrate a GWT application with an existing server-side localization system, dynamic string internationalization is the option to consider.


Localizable Interface
The most powerful technique is to implement the Localizable interface. Implementing Localizable allows you to go beyond simple string substitution and create localized versions of custom types. It's an advanced internationalization technique that you probably won't have to use very often.


First create the Java interface (StockWatcherConstants) that accesses the properties files which hold each translation. The interface uses annotations to specify the default translation. This interface implements the GWT Constants interface. This interface is bound automatically to any StockWatcherConstants*.properties files you create because of its name.


First create the Java interface (StockWatcherMessages). It accesses the properties files which hold the translations of each parameterized message. This interface implements the GWT Messages interface. Unlike the StockWatcherConstants interface, the methods in this interface contain parameters. When you call these methods, the arguments you pass will replace the placeholders you left in the strings in the properties files. This interface is bound automatically to any StockWatcherMessages*.properties files you create because of its name.

分享到:
评论

相关推荐

    gwt 练习 gwt学习

    GWT,全称为Google Web Toolkit,是一个开源的Java框架,用于构建高性能、可维护的富互联网应用程序(RIA)。GWT允许开发者使用Java语言编写客户端代码,然后通过编译器将其转换为优化过的JavaScript,以便在各种...

    GWT入门 GWT中文教程

    GWT,全称为Google Web Toolkit,是Google推出的一款开源JavaScript开发框架,允许Java开发者使用Java语言来编写Web应用。GWT通过编译器将Java代码转换为高效的JavaScript,从而在客户端浏览器上运行。这个“GWT入门...

    GWT快速开发(GWT) 是一个Java软件开发框架用于开发类似于Google Maps和Gmail的AJAX应用程序。GWT的设计参考Java AWT包设计,类命名规则、接口设计、事件监听等。你可以用Java编程语言开发你的界面,然后用GWT编译器将Java类转换成适合浏览器执行的...

    ### GWT快速开发知识点详解 #### 一、GWT简介 **Google Web Toolkit (GWT)** 是由Google推出的一款开源的Java开发框架,主要用于构建高度交互式的Web应用程序,特别是那些类似Google Maps和Gmail这样的AJAX应用。...

    Gwt中文手册,GWt入门

    Gwt中文手册,GWt入门Gwt中文手册,GWt入门Gwt中文手册,GWt入门

    GWT

    标题 "GWT" 指的是 Google Web Toolkit,这是一个开源的Java开发框架,主要用于构建高性能、跨浏览器的富互联网应用程序(Rich Internet Applications,简称RIA)。GWT通过将Java代码编译为JavaScript,使得开发者...

    GWT开发环境JAVA

    **GWT(Google Web Toolkit)开发环境JAVA** Google Web Toolkit(GWT)是一个开放源代码的Java框架,它允许开发者使用Java语言来编写富互联网应用程序(Rich Internet Applications,RIA),并将其编译成优化的...

    smartgwt最新版本GWT的DEMO

    SmartGWT是基于Google Web Toolkit (GWT) 的一个开源框架,它为开发富互联网应用程序(RIA)提供了丰富的组件库和高级功能。SmartGWT的最新版本进一步提升了用户体验,简化了开发过程,提供了更多现代Web设计的需求...

    GWT Demo,GWT学习时的一些小例子

    **GWT 概述** GWT(Google Web Toolkit)是由Google开发的一款开源的JavaScript开发框架,主要用于构建高性能、跨浏览器的Web应用程序。GWT通过Java语言进行开发,并将Java代码编译成高效的JavaScript,使得开发者...

    GWT入门和进阶

    GWT,全称为Google Web Toolkit,是Google推出的一款开源JavaScript开发框架,用于构建高性能的、客户端的Web应用程序。GWT允许开发者使用Java语言编写代码,然后自动编译为优化过的JavaScript,使得开发者能够利用...

    gwt-2.8.2 SDK 最新下载 google web toolkit

    Google Web Toolkit(GWT)是Google推出的一款开源的、基于Java的Web开发框架,它允许开发者使用Java语言来编写前端应用程序。GWT-2.8.2是该SDK的一个版本,提供了最新的特性和改进,旨在简化Web应用的开发流程,...

    gwt揭秘源代码

    GWT,全称为Google Web Toolkit,是一个开源的Java框架,用于构建高性能、跨浏览器的Web应用程序。这本书《GWT 揭秘》显然深入探讨了GWT的内部机制和使用技巧,通过源代码来帮助读者更好地理解和应用这个强大的工具...

    Google plugin sdk GWT

    此外,GWT社区还提供了许多第三方库,如GWT-Bootstrap和GWT-Platform,这些可以帮助开发者更轻松地构建现代、响应式的Web应用。 总之,Google插件SDK是开发GWT应用的关键组成部分,它与Eclipse IDE结合,为Java...

    GWT-API.rar

    GWT,全称为Google Web Toolkit,是一个开源的Java框架,用于构建高性能的、富交互式的Web应用程序。这个“GWT API.rar”压缩包包含了史上最全的GWT API文档,对于开发者来说是极其宝贵的资源,特别是在使用Java进行...

    GWT EXT 教程全集

    【GWT EXT 教程全集】是一份综合性的教程资源集合,主要涵盖了GWT(Google Web Toolkit)和EXT-GWT这两个技术领域的知识。GWT是Google推出的一个用于开发富互联网应用程序(RIA)的开放源代码框架,它允许开发者使用...

    GWT安装和使用

    ### GWT安装与使用详解 #### 一、GWT特性简介 **1. 动态、可重用的UI组件** GWT 提供了一系列丰富的 UI 组件库,这些组件不仅功能强大而且高度可定制,使得开发者能够轻松地构建美观且交互性强的用户界面。每个 UI...

    GWT简介.docx

    **GWT(Google Web Toolkit)简介** GWT,全称为Google Web Toolkit,是Google推出的一款开源的Java开发框架,主要用于构建高性能、跨浏览器的富互联网应用程序(Rich Internet Applications,简称RIA)。GWT允许...

    gwt入门-gwt从这里开始

    Google Web Toolkit (GWT) 是一个强大的开源框架,它允许开发者使用 Java 语言来构建复杂的、高性能的 web 应用程序。GWT 提供了一种高效的方法,将 Java 代码编译成 JavaScript 和 HTML,使其能在客户端浏览器中...

Global site tag (gtag.js) - Google Analytics