Navigation:  Grails Integration >

Tutorial

Previous pageReturn to chapter overview

Follow the steps below for building and running an example demonstrating a complete cycle from creation of a grails application to accessing its services from various clients:

 

1.Make sure to install the latest version of Grails (WebORB support starts with version 1.3.6).
2.Download the WebORB plugin for Grails from the Midnight Coders download center (see the instructions in the Deployment section).
3.Create a Grails application using the command below (this example will assume the name of the application is myapp):

grails create-app myapp

4.Change the current directory to myapp:

cd myapp

5.Copy the plugin file into the current directory and install the plugin:

grails install-plugin grails-web-orb-0.2.zip

6.Create a service class (service classes will be automatically exposed by WebORB as remotely accessible services):

grails create-service Product

7.Create a domain class (domain classes represent entities application's services work with):

grails create-domain-class Product;

8.Open the following file in a text editor: grails-app/domain/myapp/Product.groovy
9.Modify the contents of the file so it looks as shown below. This step creates a data structure for the domain product.

package myapp
 
class Product {
 String name
 String description
 String image
 String category
 Double price
 Integer qtyInStock
}

10.Open the following file in a text editor grails-app/services/myapp/ProductService.groovy.
11.Modify the contents of the file so it looks as shown below. The getProducts method returns a list of available products (collection of products will be set up later in the tutorial)

package myapp
 
class ProductService {
 
   def getProducts() {
     return Product.list()
   }
}

12.Open the following file in a text editor: grails-app/conf/BootStrap.groovy
13.Modify the contents of the file so it looks as shown below. This step sets up a collection of Products which the service will operate with.

class BootStrap {
 
def init = { servletContext ->
      new myapp.Product(name: 'Nokia 6010', category: '6000', image: 'Nokia_6010.gif', price: 99.00, description: 'Easy to use without sacrificing style, the Nokia 6010 phone offers functional voice communication supported by text messaging, multimedia messaging, mobile internet, games and more.', qtyInStock: 21).save()
      new myapp.Product(name: 'Nokia 3100 Blue', category: '9000', image: 'Nokia_3100_blue.gif', price: 109.00, description: 'Light up the night with a glow-in-the-dark cover - when it is charged with light you can easily find your phone in the dark. When you get a call, the Nokia 3100 phone flashes in tune with your ringing tone. And when you snap on a Nokia Xpress-on gaming cover, you will get luminescent light effects in time to the gaming action.', qtyInStock: 99).save()
      new myapp.Product(name: 'Nokia 3100 Pink', category: '3000', image: 'Nokia_3100_pink.gif', price: 139.00, description: 'Light up the night with a glow-in-the-dark cover - when it is charged with light you can easily find your phone in the dark. When you get a call, the Nokia 3100 phone flashes in tune with your ringing tone. And when you snap on a Nokia Xpress-on gaming cover, you will get luminescent light effects in time to the gaming action.', qtyInStock: 30).save()
      new myapp.Product(name: 'Nokia 3120', category: '3000', image: 'Nokia_3120.gif', price: 159.99, description: 'Designed for both business and pleasure, the elegant Nokia 3120 phone offers a pleasing mix of features. Enclosed within its chic, compact body, you will discover the benefits of tri-band compatibility, a color screen, MMS, XHTML browsing, cheerful screensavers, and much more.', qtyInStock: 10).save()
   }
   def destroy = {
   }
}

14.Change the current directory to the root of the application and run the following command to start the application:

grails run-app

15.When the application starts, it will display the following message at the end of the output log. (The WebORB version number may be different).

WebORB Presentation Server 4.1.1 (c) 2003-2011 Midnight Coders, Inc.
Server running. Browse to http://localhost:8080/myapp

16.Open the WebORB management console at the following URL:

http://localhost:8080/myapp/weborb.wo

17.Click the "Services" tab and expand the "Grails services" node in the service browser tree as shown below:
deployed_grails_services
18.The ProductService node represents the service class created in step 8 and modified in step 12. Expand the ProductService node. The service browser will display a list of methods available in the service. Select the getProducts method. When a method is selected, the service browser switches to the "Test Drive" mode.
grails_test_drive
19.TestDrive provides a graphical method invocation facility. If the selected method contains any arguments, a form is dynamically constructed where user can enter argument values.  To invoke the method, click the Invoke button. The result of the method invocation is rendered in the response data grid:
grails.invocation.result
20.Back in the service browser tree, select the ProductService node. The console switches to the Code Generator mode. The code generator includes support for a variety of clients and frameworks (see a list with radio buttons on the right):
codegen_styles
21.For the demonstration purposes this tutorial will describe an AJAX/JavaScript invocation. However, the process of code generation for any other client type is identical. Click the "AJAX Client" radio button. WebORB generates the code and displays it in the "Generated code structure" section. Click the "Download Code" button to get a zip file with the generated code:
download_ajax_code_grails
22.Extract the contents of the downloaded file and open ProductServiceInvoker.html in a browser (it does not need to be loaded from a webserver). Select the getProducts() method and click the "Invoke method" button. The WebORB AJAX client sends a method invocation request of the selected method and when the result arrives, it is rendered in the browser:
ajax-grails-invocation