Using Multiple Modules in GWT

In this tutorial we are going to work through creating multiple modules for a GWT web application. Multiple modules are a great way to organize and reuse your GWT code, and it is a good way to develop multiple GWT pages with only one entry point. If you are not familiar with the structure of a GWT application then you should probably read this tutorial first.

There are two ways in which you can load multiple modules in a GWT application, and they are as follows:

  1. Compile each module separately and then include each module with a script tag in your html host page.
  2. Create a top level module XML definition that includes all the modules you want to include. Compile the top level module to create a single set of JavaScript output.

The GWT team states “The first approach may seem the easiest and most obvious. However, the second approach will lead to much better end-user performace. The problem with loading multiple modules is that each module has to be downloaded seperately by the end-user’s browser. In addition, each module will contain redundant copies of GWT library code and could possibly conflict with each other during event handling. The second approach is strongly recommended.” Since the second approach is recommended, then that is the approach we are going to take. So let’s get started.

A GWT web application is organized into Java packages, and because of this we need to understand the package hierarchy. The first package in a GWT application is the (default web application packages) “org.yournamehere” package. This package contains your Main.gwt.xml file, and you should notice that this file is used to inherit other GWT modules. Primarily you must inherit the “com.google.gwt.user.User” package to get GWT to work in the first place. Based on the package structure, I think you can probably figure out that this is how we are going to inherit our own modules. The second package we are going to concern ourselves with is the org.yournamehere.client package, which is the package that contains you client side code.

So because of the packaging structure we can create our own modules that can be used in multiple GWT applications. The example we are going to use for this tutorial is to develop a module for providing security for our application. The security package may contain client side code for developing a log in, creating an account, and perhaps even some verification code. Me personally, I like to name my packages similar to the way in which GWT has named their packages, and you want to make sure your package names have a meaning for what you are developing. This is just simple naming practices.

For our security module we are going to create a new package in Netbeans called “org.yournamehere.security”. This package will contain our Security.gwt.xml file, and this file should look like the following:

<?xml version="1.0" encoding="UTF-8"?>
<module>
   <inherits name="com.google.gwt.user.User"/>
   <!--You can add other modules as well by inheriting them-->
</module>

Now that we have created our Security.gwt.xml file, we can create our client package, which we will call “org.yournamehere.security.client”. The next step would be to add class files to the client package, and develop out your module.

Once you have finished developing your module, you will need to inherit the module in your Main.gwt.xml file. So you just need to write out the following inherits statement, so your Main.gwt.xml file will look like below.

<?xml version="1.0" encoding="UTF-8"?>
<module>
   <inherits name="com.google.gwt.user.User"/>
   <inherits name="org.yournamehere.security.Security"/>
</module>

Once you have inherited the module in your Main.gwt.xml file, you are ready to use the module in your web application. It’s really that easy.

I now want to cover something important I found while doing research for this tutorial. You are able to include multiple entry points into your web application. Why you would want to do this is beyond me, but you are certainly capable of doing so. I bring this up because you want to be careful in including an entry point in a module that is going to be inherited by either another module or the main module itself. The GWT team states “If you are loading multiple GWT modules within the same page, each module’s EntryPoint will be called as soon as both that module and the outer document is ready. Two modules’ EntryPoints are not guaranteed to fire at the same time, or in the same order in which their selection scripts were specified in the host page.” You should be aware of this if you are considering using multiple entry points, but I would recommend against this practice.

As always if you have any questions, please leave a comment.

Rating 3.50 out of 5
  1. Hi,
    First of all, thanks for writing on GWT with Netbeans…

    “You are able to include multiple entry points into your web application. Why you would want to do this is beyond me ”

    Because, i am not using GWT for creating single page (Gmail like) WebApp.. But i am using it in 1 page, lets say for Menu (1st entry point), Comments (2nd Entry Point), Friends List (3rd entry point)….

    Does it make sense??? :)

    Cheers…

    • admin
    • August 11th, 2009

    If you are using GWT in one page of your web application then why not set up your project something like this instead of using multiple entry points.

    <div id="menu"></div>
    <div id="comments"></div>
    <div id="friends"></div>

    Then in your onModuleLoad function you would do something like this:


    RootPanel.get("menu").add(new Menu());
    RootPanel.get("comments").add(new Comments());
    RootPanel.get("friends").add(new Friends());

    The primary issue with multiple entry points is that there is no guarantee as to which module is going to load first. GWT documents state “Two modules’ EntryPoints are not guaranteed to fire at the same time, or in the same order in which their selection scripts were specified in the host page.” I think the above example would benefit you better than having multiple entry points, but of course without seeing how your project is set up then I cannot say which is the best technique for using multiple modules.

    • Romeo
    • August 11th, 2009

    What happens when your two modules have entrypoints? Is there a way to open two different hosted mode windows to display each one of them?

    I followed your tutorial under Eclipse. In my case, both the security module and the main module have entrypoints. Each one serves a different web page (ie. main.html, and security.html).

    I want to be able to debug, test and develop both projects at the same time under Eclipse. So, I had to add the following step to your guidance.
    - Added the security module in the build path of the main module as a required project.

    Once I did this, I was able to run the main application. However, GWT only opened one hosted browser window, and it put the buttons and text from the two different hmtl pages into the single hosted browser window.

    Then, I tested the functionality of the main module and it worked. However, if I test some of the buttons that correspond to the security module it throws me an error about trying to connect the server.

    It seems that it is not able to load the server functionality in the security module. Something is kind of missing.

    Here are my questions?
    Is it possible to do this?

    I mean, can I load two modules with entry points in hosted mode under Eclipse?

    Is there a way to specify the ports where each one of the modules is supposed to run?

    Can I visualize two hosted-mode browser windows, one for each module (running in two different ports) in hosted mode?

    The whole idea is that if I click in a button in the main module window, something will appear in the security window.

    • haydar
    • August 11th, 2009

    Hi,
    I have two gwt applications. One for the security side (login, account
    authentication etc.), the other one for displaying menus and screens
    according to the permissions that logged in user got.
    That was done like this and I mustn’t change the architecture. Now I
    am wanted to upgrade the two projects from gwt1.5.3 to gwt 2.0. It was
    working quite fine with gwt 1.5.3 but when I changed it into gwt 2.0
    it is not working properly now.
    Two gwt applications have their own entry points. The second app is
    the main one. You run it but the page of the first one comes to screen
    and you log in then the sec. app. runs. My problem is this how can i
    compile both in one run dialog?
    And one more thing, the app.s contact each other by a service of the
    first app. But now (after gwt 2.0) it wants me to give the mapping as
    fistApp/secAppService. But when I give it like this it doesn’t work.
    When I don’t it says it is recommended.
    I don’t know what to do, please help me…
    By the way it runs fine with GWTShell option, these errors are from
    DevMode. And without DevMode I can’t compile them or I don’t know how
    to do it…

  2. Good work, keep us posting, you are very good writer.

    • Hartimer
    • August 11th, 2009

    Hi
    While reading your entry i realised how to develop multi-module GWT applications, thank you for that.

    I’m still playing around with it, but i stepped into an “issue”.

    Given your security example, lets say that i don’t want the Main module to draw or execute anything for that matter before the security module asserts the user’s authenticity.

    How would i be able to halt the execution of a module, making it wait for “something” provided by other module?

    Using cookies seems bad (since they might be maliciously changed), not to mentioned that i would require a cycle or sleep approach (might cause problems with the single-threaded policy of browsers).

    Eventualy the module to be protected will be so through unauthorised access to servlets, but still, how to i avoid that module accessing servlets before time in the first place?

  1. No trackbacks yet.

Spam Protection by WP-SpamFree