2 Getting Started - Reference Documentation
Authors: Benjamin Wolff
Version: 1.0.1
Table of Contents
2 Getting Started
This short guide will get you started with all you need to know to create your first Grails-RWT application. You should be familiar with how to create a Grails application and artifacts like domain classes and services.The RWT setup process is based on the standalone guide. However, the Grails-RWT plugin does most of the setup for you, as you will see in this guide.Start with creating an example Grails application and then follow the instructions below. The next sections assume that the applications is calledgrails-rwt-example
. Replace the occurences in the example
URLs with the actual name of your example application.Example Code
There is an example project available on GitHub that can be used as a kickstart. It contains the example code shown in this guide and is properly configured to get started immediately.git clone https://github.com/bwolff/grails-rwt-example
2.1 Installation
The plugin is installed using the default Grails plugin installation mechanism. Simply add/modify the following lines to the plugin dependency section in thegrails-app/conf/BuildConfig.groovy
file:grails.project.dependency.resolution = { plugins { // Other plugin dependencies compile ':rwt:1.0' } }
2.2 Creating the Entry Point Bean
The first step is to create a so called RWT entry point class. An instance of this class needs to be registered as a Spring bean to be picked up by the RWT configuration. The suggested approach is to use a Grails service for that. Being a Grails artifact, a service is registered as a Spring bean by default and other dependencies and functionalities are added/injected out of the box.The following code snippet shows an example entry point service implementation (in thegrails-app/services
sub-folder). To demonstrate the usage,
another service (FooService
) and a domain class (Foo
) are used. They don't do anything useful and
their code can be obtained from the example application, as described above. The comments in the code
snippet highlight interesting points.package grails.rwt.exampleimport org.eclipse.jface.layout.GridDataFactory import org.eclipse.jface.layout.GridLayoutFactory import org.eclipse.jface.viewers.ArrayContentProvider import org.eclipse.jface.viewers.LabelProvider import org.eclipse.jface.viewers.ListViewer import org.eclipse.rap.rwt.application.EntryPoint import org.eclipse.swt.SWT import org.eclipse.swt.events.SelectionAdapter import org.eclipse.swt.events.SelectionEvent import org.eclipse.swt.widgets.Button import org.eclipse.swt.widgets.Composite import org.eclipse.swt.widgets.Display import org.eclipse.swt.widgets.Label import org.eclipse.swt.widgets.Shell// "Note the use of the Groovy syntax"class HelloEntryPointService implements EntryPoint { // "The service needs to implement EntryPoint" def fooService // "Other services will get injected by Grails" @Override public int createUI() { log.info("Creating RWT UI ...") // "Implicit logger object" Display display = new Display() Shell shell = new Shell(display) shell.setText("Hello RWT!") GridLayoutFactory.swtDefaults().applyTo(shell) final Label label = new Label(shell, SWT.NONE) GridDataFactory.swtDefaults().align(SWT.FILL, SWT.CENTER).grab(true, false).applyTo(label) // "JFace is available" ListViewer viewer = new ListViewer(shell, SWT.V_SCROLL | SWT.H_SCROLL) GridDataFactory.swtDefaults().hint(500, 300).applyTo(viewer.list) viewer.contentProvider = ArrayContentProvider.instance viewer.labelProvider = new LabelProvider() viewer.input = Foo.list() // "Use domain objects and GORM, no DTOs required!" Button button = new Button(shell, SWT.PUSH) GridDataFactory.swtDefaults().align(SWT.BEGINNING, SWT.CENTER).applyTo(button) button.text = "Click me!" button.addSelectionListener(new SelectionAdapter() { Integer num = 0 @Override public void widgetSelected(SelectionEvent e) { label.text = fooService.serviceMethod(num++) // "Call methods of other services" } }) // "Open the shell and start the UI event handling queue" shell.pack() shell.open() while(!shell.isDisposed()) { if(!display.readAndDispatch()) { display.sleep() } } display.dispose() return 0; } }
helloEntryPointService
will be registered
automatically on application start. The next step is to configure our newly created entry point.The given code snippet is just a very simple example to demonstrate the usage. You should develop your application using a cleaner separation of concerns, i.e. by employing the MVC or MVP pattern.
2.3 Configuring the Entry Point
The only thing that is missing now is the configuration of our new RWT entry point. This is done in a configuration block in thegrails-app/conf/Config.groovy
file.// RWT configuration rwt { // Entry points configuration entrypoints { // Configuration of an entry point named 'hello', the relative URL path will be '/hello'. hello { bean = 'helloEntryPointService' // The name of the entry point bean (required) pageTitle = 'Hello RWT!' // The browser title that will be displayed (optional) } } }
entrypoints
and provide the name of the
entry point bean, which is described above. We can also specify a string that will be displayed as
the browser's page title.More information about the configuration of the RWT application and the entry points is available
here.
2.4 Running the Application
The application can be started in a local development setup using the default run-app Grails command from the command line in the project root folder:grails run-app
http://localhost:8080/grails-rwt-example/hello
2.5 Deploying the Application
You can now package your application into a WAR file and deploy it to the servlet container of your choice. The deployment procedure is the same as for a default Grails application, by simply issuing the following command in the command line:grails war
target/
sub-folder and can now be deployed to e.g. a
Tomcat container. All necessary jar files for RWT/JFace will be
packaged and the web.xml will also be properly set up.Congratulations! You should now be able to create a more sophisticated Grails-RWT applications and
explore the features that RWT provides.