Three years ago we started working with GWT for the front of my web applications and have since lived the evolution of the library through the different versions,implementation of the MVP pattern, use of libraries as smartgwt widgets, etc …
For a year I’ve been considering myself a follower and lover of Grails and Groovy. Make use of groovy on the server think it is much more productive and agile than use java. For this reason I decided to use both technologies to develop web applications :
- GWT for the front
- Grails / Groovy for the server side
Set up development environment
1 -First make sure Groovy, Grails, and GWT are installed. In my case I use STS to create the grails project but is necessary install GWT (Set the environment variable GWT_HOME to your GWT distribution)
Grails Tools > Grails Plugin Manager > find and select Gwt Plugin.
2- Create GWT module
Open Grails command prompt and type : create-gwt-module gwtGrails.Application
3-Create the page which exposes the module we just created
Open Grails command prompt and type: create-gwt-page main/index.gsp gwtGrails.Application
The prompt ask you if you want to create the main controller. Choose y for yes.
4- Edit grails-app/conf/UrlMappings.groovy. Change “/”(view:”/index”) to “/”(view:”/main/index”) to set default grails page our new gwt page.
Service – side RPC
When communicating with the server side, uses GWT RPC services. In aconventional application would need to create a GWT servlet for each RPC service interface that we implement. Using Grails integration with server side will use thetypical Grails services which will expose static property (in which we specify the package that hosts the service interface on the client gwt:<package>) that allows us to link the interface of the RPC services with Grails service implementation on the server.
class DataService {
static transactional = true
static expose = [ ‘gwt:gwtGrails.client’ ]
String helloWorld() {
return “Hello Sergi!”
}
}
Next, this script will create the required normal and asynchronous interfaces under the src/java
directory:
Open Grails command prompt and type : grails generate-gwt-rpc
Accessing the service from your client GWT code:
DataServiceAsync myService = (DataServiceAsync) GWT.create(DataService.class);
ServiceDefTarget endpoint = (ServiceDefTarget) myService;
// Note the URL where the RPC service is located! String moduleRelativeURL = GWT.getModuleBaseURL() + “rpc”; endpoint.setServiceEntryPoint(moduleRelativeURL);
// Call a method on the service! myService.helloWorld()
String message = myService.helloWorld();
Compiling the GWT modules
Open Grails command prompt and type : grails compile-gwt-modules
Run the GWT hosted mode
Open Grails command prompt and type : grails run-gwt-client
Creating a WAR
Open Grails command prompt and type : grails war
The following link you can find a sample project where you can find both integrated technologies: