Samstag, 28. November 2009

A very simple "Hello World" example

Usually introductory books start with a "Hello World" application - one or two lines of code that print the message "Hello World" to the screen. This makes you familiar with the development environment and demonstrates how to embed your code into the larger class structure.

With GWT it's different. Google's introductory "Hello World" has not two, but more than 200 lines of code.

I believe that a "Hello World" app should demonstrate only the very basic functionality and not confuse with RPC and event handling right away. So in this post I will reduce Google's "Hello World" to the bare minimum in order to demonstrate what the core of a GWT application is.

Create your first application
Follow the instructions on the Google page for building your first web-application with the Eclipse-plugin. Name your project "HelloWorld" and the package "com.example.mywebapp". You end up with a very sophisticated "Hello World" application which we will strip down to something less complex below.

Where is main?
In GWT there is no "main" method. Program execution starts in a method named "onModuleLoad" in the class "HelloWorld". You find this class in the package "com.example.mywebapp.client".

Stripping down "HelloWorld"
Now erase all the content of the "onModuleLoad" method and replace it by the following two lines of code:

public void onModuleLoad() {
Label label = new Label("Hello World!");

Right-click on the project-name in Eclipse's Project Explorer window and choose "Run As -> Web Application". Now two new windows open up. One shows the server log, the other one being your "Hosted Mode Browser" (all that will probably change in GWT 2.0). A detailed description of "Hosted Mode" can be found here.

The output of your "Hello World" program is shown on the left side of the Hosted Mode Browser window. You'll notice a lot of other stuff in the browser that seems to stem from a different source. We'll cover that later.

There can be only one webserver running at a time. If your local webserver (jetty) shows up with an error message, you probably already have a hosted mode browser open. In this case just close the window with the error message, switch to the browser and hit "Refresh".

Keine Kommentare:

Kommentar veröffentlichen