Ok, so I've had to work a lot with GWT over the last 12 months. First up, I was really impressed at what GWT provides, especially the fact that it eliminates having to write browser code and forms. This quickly was overridden by the extremely poor compile times, the bulkiness of the GWT shell, and the fact that it allows developers to write truly terrible applications (due to its asynchronous nature) if you don't constantly monitor what they are doing.
Designing a GWT application up front is important. It is essential to make strict use of the Model View Controller pattern and obviously the Observer pattern (due to the asynchronous nature of GWT). The projects I have been developing GWT client's for, all have SOAP backend services that provide their actual data access (using Apache Axis 2). The SOAP services are all code-first POJO based services. The easiest way to put this together is to let the backend service assemble the GWT beans and perform all the transport and marshalling work to get them into your browser via the GWT RPC server. This way, you don't need to do any conversion from SOAP in to GWT beans and you don't need data transfer objects, because the GWT beans go all the way through from your server to your browser. That is, this cuts out a truckload of marshalling code, as everything is talking in GWT, and the SOAP layer handles all the SOAP stuff.
To do this, you need to create a separate jar/project for your GWT beans, that only contains the beans. This project is nothing but a pure, basic Java project that builds a jar file. It should not have the GWT framework or libraries applied to it. There are two minor things you need to do so that your GWT client project can work with these beans.
- Include the Java source of your beans in the jar file, so the GWT compiler can create the appropriate browser/RPC code.
- Include a .gwt.xml in your jar so GWT knows it contains GWT resources that it needs to use.
1. If you are using NetBeans, you can override one of the NetBeans build targets to make sure your Java files get built into the jar. The -pre-jar target should be overridden in your NetBeans generated build.xml script as follows:
2. The gwt.xml file mentioned above is purely a standard .gwt.xml file, however there is one very important gotcha. GWT expects by default that your beans will be in a sub package of the package named client. This client package is in the same parent package as the gwt.xml file for your project. This is all well and good for most people, however I prefer to have more control over my package structure. I don't want to have my beans in a package called client, when the beans are a re-usable library that servers and clients both use.
To override this requirement, you simply add an extra line to your gwt.xml file with the name of the package that they can be found beneath. That is, if they are in a package called beans, then use the following definition line:
Note that you can have further packages beneath this folder, GWT just needs to know that it isn't the client folder.
If you fail to point this out to GWT, you will get the following exceptions during the GWT compile phase about it not being able to find the source code of your GWT beans:
[java] [ERROR] Line 206: No source code is available for type com.mym2.m2w.common.movements.Movement; did you forget to inherit a required module?
[java] [ERROR] Line 208: No source code is available for type com.mym2.m2w.common.movements.CashDeposit; did you forget to inherit a required module?
[java] Computing all possible rebind results for 'com.mym2.apps.client.LoginForm'
[java] Rebinding com.mym2.apps.client.LoginForm
[java] Checking rule
[java] [ERROR] Unable to find type 'com.mym2.apps.client.LoginForm'
[java] [ERROR] Hint: Previous compiler errors may have made this type unavailable
[java] [ERROR] Hint: Check the inheritance chain from your module; it may not be inheriting a required module or a module may not be adding its source path entries properly
[java] [ERROR] Build failed
So thats my first GWT blog, nice and simple to begin with. I will most likely be putting up further blogs as theres a lot of little tricks and techniques that should be known to get the most out of GWT. I'm also very interested in the architectures of GWT applications, as large applications absolutely require a good design in GWT.