Core GWT concepts such as compiling Java source into JavaScript, debugging, cross-browser support, and defining modules.
byte, char, short,
int, long, float,
double, Object, String,
and arrays are supported. However, there is no 64-bit integral type in
JavaScript, so variables of type long are mapped onto
JavaScript double-precision floating point values. To ensure maximum
consistency between hosted mode and web mode, we recommend that you use
int variables.try, catch, finally
and user-defined exceptions are supported as normal, although
Throwable.getStackTrace() is not supported for web mode.
See Throwable for additional details.assert statements, but
it does not emit code JavaScript code for them.synchronized keyword, it has no
real effect. Synchronization-related library methods are not available,
including Object.wait(), Object.notify(),
and Object.notifyAll()
strictfp keyword. GWT does not support the
strictfp keyword and can't ensure any particular degree
of floating-point precision in translated code, so you may want to
avoid calculations in client-side code that require a guaranteed level
of floating-point precision.java.lang and
java.util, which lists supported
classes and contains notes on behavioral differences from the standard
Java runtime.
Some specific areas in which GWT emulation differs from the standard Java runtime:
Whenever possible, GWT defers to browsers' native user interface
elements. For example, GWT's Button widget is a true HTML
<button> rather than a synthetic button-like widget
built, say, from a <div>. That means that GWT
buttons render appropriately in different browsers and on different
client operating systems. We like the native browser controls because
they're fast, accessible, and most familiar to users.
When it comes to styling web applications, CSS is ideal. So, instead of attempting to encapsulate UI styling behind a wall of least-common-denominator APIs, GWT provides very few methods directly related to style. Rather, developers are encouraged to define styles in stylesheets that are linked to application code using style names. In addition to cleanly separating style from application logic, this division of labor helps applications load and render more quickly, consume less memory, and even makes them easier to tweak during edit/debug cycles since there's no need to recompile for style tweaks.
To launch a hosted mode session, your startup class should be
com.google.gwt.dev.GWTShell, found in
gwt-dev-windows.jar (or gwt-dev-linux.jar).
To create a web mode version of your module, you compile it using either
the "Compile/Browse" button available in the hosted browser window or the
command-line compiler com.google.gwt.dev.GWTCompiler.
Web mode demonstrates what makes GWT unusual: when your application is launched in web mode, it runs completely as JavaScript and does not require any browser plug-ins or JVM.
<html>
<head>
<!-- Properties can be specified to influence deferred binding -->
<meta name='gwt:property' content='locale=en_UK'>
<!-- Stylesheets are optional, but useful -->
<link rel="stylesheet" href="Calendar.css">
<!-- Titles are optional, but useful -->
<title>Calendar App</title>
</head>
<body>
<!-- The fully-qualified module name, followed by 'nocache.js' -->
<script language="javascript" src="com.example.cal.Calendar.nocache.js"></script>
<!-- Include a history iframe to enable full GWT history support -->
<!-- (the id must be exactly as shown) -->
<iframe src="javascript:''" id="__gwt_historyFrame" style="width:0;height:0;border:0"></iframe>
</body>
</html>
The structure was designed to make it easy to add GWT functionality to
existing web applications with only minor changes.If you are starting a GWT project from scratch, you should use the standard GWT package layout, which makes it easy to differentiate client-side code from server-side code. For example, suppose your new project is called "Calendar". The standard package layout would look like this:
| Package | Purpose |
|---|---|
com/example/cal/ | The project root package contains module XML files |
com/example/cal/client/ | Client-side source files and subpackages |
com/example/cal/server/ | Server-side code and subpackages |
com/example/cal/public/ | Static resources that can be served publicly |
and examples files would be arranged like so:
| File | Purpose |
|---|---|
com/example/cal/Calendar.gwt.xml | A common base module for
your project that inherits com.google.gwt.user.User module |
com/example/cal/CalendarApp.gwt.xml | Inherits the com.example.cal.Calendar module (above)
and adds an entry point class |
com/example/cal/CalendarTest.gwt.xml | A module defined by your project |
com/example/cal/client/CalendarApp.java | Client-side Java source for the entry-point class |
com/example/cal/client/spelling/SpellingService.java | An RPC service interface defined in a subpackage |
com/example/cal/server/spelling/SpellingServiceImpl.java | Server-side Java source that implements the logic of the spelling service |
com/example/cal/public/Calendar.html | An HTML page that loads the calendar app |
com/example/cal/public/Calendar.css | A stylesheet that styles the calendar app |
com/example/cal/public/images/logo.gif | A logo |
When module inherit other modules, their source paths are combined so that each module will have access to the translatable source it requires.
When module inherit other modules, their public paths are combined so that each module will have access to the static resources it expects.
.gwt.xml. Module XML files should reside in your
project's root package.
If you are using the standard project structure, your module XML can be as simple as this:
<module>
<inherits name="com.google.gwt.user.User"/>
<entry-point class="com.example.cal.client.CalendarApp"/>
</module>
Modules are always referred to by their logical names. The logical name
of a module is of the form pkg1.pkg2.ModuleName
(although any number of packages may be present) and includes neither
the actual file system path nor the file extension. For example, the
logical name of a module XML file located at
~/src/com/example/cal/Calendar.gwt.xmlis
com.example.cal.Calendar
If no <source> element is defined in a module XML
file, the client subpackage is implicitly added to the
source path as if <source path="client"> had been
found in the XML. This default helps keep module XML compact for
standard project layouts.
<public> element
supports
pattern-based filtering
to allow fine-grained control over which resources get copied into the
output directory during a GWT compile.
If no <public> element is defined in a module XML
file, the public subpackage is implicitly added to the
public path as if <public path="public"> had been
found in the XML. This default helps keep module XML compact for
standard project layouts.
/spellcheck).
Your client code then specifies this URL mapping in a call to
ServiceDefTarget.setServiceEntryPoint(String). Any number of
servlets may be loaded in this manner, including those from inherited
modules.<script src="js-url"/>The script is loaded into the namespace of the host page as if you had included it explicitly using the HTML
<script>
element. The script will be loaded before your
onModuleLoad()
is called.
<stylesheet src="css-url"/>You can add any number of stylesheets this way, and the order of inclusion into the page reflects the order in which the elements appear in your module XML.
<public> element supports certain attributes
and nested elements to allow pattern-based inclusion and exclusion. It
follows the same rules as Ant's
FileSet element. Please see the documentation
for FileSet for a general overview.
The <public> element does not support the full
FileSet semantics. Only the following attributes and
nested elements are currently supported:
includes attributeexcludes attributedefaultexcludes attributecasesensitive attributeinclude tagsexclude tagsdefaultexcludes is
true. By default, the patterns listed here
are excluded.projectCreator could be used to make an Eclipse
project for one of the samples that comes with GWT.
projectCreator [-ant projectName] [-eclipse projectName] [-out dir] [-overwrite] [-ignore]
-ant | Generate an Ant buildfile to compile source (.ant.xml
will be appended) |
-eclipse | Generate an eclipse project |
-out | The directory to write output files into (defaults to current) |
-overwrite | Overwrite any existing files |
-ignore | Ignore any existing files; do not overwrite |
~/Foo> projectCreator -ant Foo -eclipse Foo Created directory src Created directory test Created file Foo.ant.xml Created file .project Created file .classpath
Running ant -f Foo.ant.xml will compile src
into bin. The buildfile also contains a
package target for bundling the project into a jar.
.project can be imported into an Eclipse workspace.
applicationCreator [-eclipse projectName] [-out dir] [-overwrite] [-ignore] className
-eclipse | Creates a debug launch configuration for the named eclipse project |
-out | The directory to write output files into (defaults to current) |
-overwrite | Overwrite any existing files |
-ignore | Ignore any existing files; do not overwrite |
className | The fully-qualified name of the application class to create |
~/Foo> applicationCreator -eclipse Foo com.example.foo.client.Foo Created directory src/com/example/foo/client Created directory src/com/example/foo/public Created file src/com/example/foo/Foo.gwt.xml Created file src/com/example/foo/public/Foo.html Created file src/com/example/foo/client/Foo.java Created file Foo.launch Created file Foo-shell Created file Foo-compileRunning
Foo-shell brings up the new app in hosted mode.
Foo-compile translates the Java app to JavaScript,
creating a web folder under www.
Foo.launch is a launch configuration for Eclipse.
junitCreator -junit pathToJUnitJar [-eclipse projectName] [-out dir] [-overwrite] [-ignore] className
-junit | Specify the path to your junit.jar (required) |
-module | Specify name of the application module to use (required) |
-eclipse | Creates a debug launch configuration for the named eclipse project |
-out | The directory to write output files into (defaults to current) |
-overwrite | Overwrite any existing files |
-ignore | Ignore any existing files; do not overwrite |
className | The fully-qualified name of the test class to create |
~/Foo> junitCreator -junit /opt/eclipse/plugins/org.junit_3.8.1/junit.jar
-module com.example.foo.Foo
-eclipse Foo com.example.foo.client.FooTest
Created directory test/com/example/foo/test
Created file test/com/example/foo/client/FooTest.java
Created file FooTest-hosted.launch
Created file FooTest-web.launch
Created file FooTest-hosted
Created file FooTest-web
Running FooTest-hosted tests as Java bytecode in a JVM.
FooTest-web tests as compiled JavaScript. The launch
configurations do the same thing in Eclipse.
i18nCreator [-eclipse projectName] [-out dir] [-overwrite] [-ignore] [-createMessages] interfaceName
-eclipse | Creates a debug launch config for the named Eclipse project |
-out | The directory to write output files into (defaults to current) |
-overwrite | Overwrite any existing files |
-ignore | Ignore any existing files; do not overwrite |
-createMessages | Generate scripts for a Messages interface rather than a Constants one |
interfaceName | The fully-qualified name of the interface to create |
~/Foo> i18nCreator -eclipse Foo -createMessages com.example.foo.client.FooMessages Created file src/com/example/foo/client/FooMessages.properties Created file FooMessages-i18n.launch Created file FooMessages-i18n ~/Foo> i18nCreator -eclipse Foo com.example.foo.client.FooConstants Created file src/com/example/foo/client/FooConstants.properties Created file FooConstants-i18n.launch Created file FooConstants-i18n
Running FooMessages-i18n will generate an interface from
FooMessages.properties that extends
Messages (The messages will take parameters,
substituting {n} with the nth parameter).
Running FooConstants-i18n will generate an interface
from FooConstants.properties that extends
Constants (The constants will not take parameters).
benchmarkViewer [path]
path | Specify the path to the XML benchmark reports. If the
path is not specified, it defaults to the current working directory. |
~/Foo> benchmarkViewer my/benchmark/resultsLooks for report XML files in the folder
my/benchmark/results
and displays them in the viewer.