GWT on Grails

Posted on January 12, 2010

Notes for gwt plugin version 0.5

As donruby said in the comments: I was able to get the project working by upgrading to GWT-0.5-SNAPSHOT. I had to delete the grails cache (~/.grails on MacOSX) and then do a "grails clean" and recompile. I did not have to remove the DTO plugin.


So... I spent a big part of my holydays figuring out how to configure a project using GWT and Grails. This took me quite a while because I could not get past the transfer object problem: I kept getting errors and I had to search for hints scattered over the net, well yes, kind of fun :D, I learnt quite a lot from this. And damn it works! :P
I also like the way it works, hopefully other posts on the subject will follow.
I wrote this gwt on grails tutorial to help others in case they get stuck.

You will find the project hosted at google projects, so you can follow this post with the code at hand, it will be easier, and will help you if you'll get stuck in case I've missed something :P

Basic configuration

Let's start, I've used Grails 1.1.2 (though this works with 1.2.0 also), Gwt 2.0.0 and SpringSource Tool Suite 2.3.0 because of its support for the frameworks.
Tipically all grails commands are issued from the project home folder, for example in my case it was /home/montecristo/grails-projects/GrailsGwtDto
Set the following environment variables: GRAILS_HOME and GWT_HOME.
Create a grails project issuing grails create-app GrailsGwtDto or using the plugin in your ide.
Add the grails gwt plugin with grails install-plugin gwt, then add the gwt library to your project.
Create a gwt module, this is done with the following command: grails create-gwt-module com.lazywithclass.grailsgwtdto, when I issued it it created me a folder structure, not recognized as source, to fix this I created a new source folder name src/gwt, to hold the frontend classes.

Now you have set up your project, ready to be filled with classes and all your stuff.

Creating the business logic

Create the grails service that will be called by gwt with the following command: grails create-service SimpleRpc then make it do something so that you notice it when it gets called.
A Grails service can be configured to act as it would the servlet in a typical Gwt project, this can be done using the static expose property, for a detailed description refer to the gwt plugin page.

The client side

At this point create the rpc interfaces, let the plugin generate them for you: grails generate-gwt-rpc remember that in this way they get automatically managed, if you want to manage them just edit one and save it, this way you'll get the control.
Here's a picture to get the whole thing (btw sorry for its ugliness :D):



Gwt and groovy are separated, client on one side, server on the other, rpcs and data transfer objects will reside in the middle, the classes will be used by both sides.
Back to the gwt side, I attached a button to the RootPanel then called the method in the service, using rpc.

Run it!

Start the server with grails run-app and the client with grails run-gwt-client Remember that Object is not an allowed return type from the server, as stated by the error message if you do it, it is a too generic type. In the async interface always set the return type to void, the return type will be handled in the onSuccess method of the callback.
Exceptions launched in the client will be found int the web development console, Exceptions in the server side will be found in the console from which you launched the server.

Browse the archive

blog comments powered by Disqus