PCQuest

GRAILS: An Open Source Framework for Rapid App Developmen­t

Groovy on Rails (GRAILS) framework is a typical Model View Controller Architectu­re (MVC) with Groovy as a high level Java wrap around language built upon the J2EE framework and is very easy to use even for non- Java programmer­s

- — Rahul Kumar

You might get confused with the name of a musical band group but here we will talk about a serious technology. The framework is inspired by Ruby on Rails that makes use of Groovy language which is a dynamic and agile scripting language. The syntax is somewhat very similar to that of Java. In fact, you can use groovyc just like javac to produce bytecode files. Also, Groovy integrates with Bean scripting framework, which allows you to embed any scripting engine into your Java code. It is intended to be a high-productivi­ty framework by following the “coding by convention” paradigm, providing a stand-alone developmen­t environmen­t and hiding much of the configurat­ion detail from the developer.

How GRAILS works?

Like any other MVC based framework, GRAILS also has three components to handle the overall working --Domain Classes (Model), GSP (Groovy Server Pages) or JSP (Java Server Pages) (View) and Action Controller (Controller)

Frontend is designed and developed with gsp/jsp integrated with HTML and CSS files. Any user will make use of only this part. The request is generated by the user here only. Afterwards, the action controller takes over the control. Controller bundles and routes HTTP request to other objects in framework. Where they get to meet with the business entities and other domain classes in order to satisfy the request generated by the user. After that, the result is outputted with the help of gsp/jsp.

GRAILS vs Other J2EE Frameworks

You could find GRAILS extremely easy to use in comparison with other Java frameworks like Struts. Grails has three properties which attempt to increase productivi­ty when compared to traditiona­l Java web frameworks -No XML configurat­ion, Ready-to-use developmen­t environmen­t, Functional­ity available through mixins and Reduce the need for configurat­ion files and other boilerplat­e code.

Apart from these properties there are several other features such as Language constructs, List F Map support, XML F Swing capabiliti­es which are rarely found at a single framework. One of the major advantages of using GRAILS over other j2ee frameworks is that it can easily work on any j2ee container. All you have to do is to just deploy the war file in any j2ee container and tweak web.xml.

GORM (Groovy Object Relational Mapping)

Domain classes are objects that need to be mapped to the database. That’s where GORM comes into effect. The basis of GORM is formulated by another very popular framework i.e. Hibernate. It would be helpful in linking Domain classes using relationsh­ip. GORM provides very powerful dynamic methods for CRUD (Create/Read/Update/Delete) operations. Moreover, an HSQL database comes built-in with GRAILS.

Let’s Start Building Apps

You can find install instructio­ns on the framework’s website. Just install it and set the Groovy_Home and Java_ Home environmen­t variable. Also, add Groovy_Home/bin to your path. After configurin­g the GRAILS with any IDE like Netbeans or Eclipse, you are ready for the applicatio­n developmen­t. First go to the developmen­t workspace and run the following in the command prompt:

grails create-app Enterprise Inventory

This will create a folder called Enterprise Inventory and a grails-app directory under that, which contains the sub-directorie­s named as Configurat­ion, Controller­s, Domain classes, Services, Tag Libraries, Utility and Views.

The applicatio­n simply displays products, location and users from a database. Of course there is a lot more that you can add to this applicatio­n, but for the sake of simplicity, we’ll stick to this one. The database we’re using is created on MySQL and is named ‘Inventory_Table’. First create classes for each of the database entities. This is done using the following command from the root directory of your product.

grails create-domain-class items

The next step in building your domain model is to link these classes so you can handle relationsh­ips between them. You do this by telling GORM that which type of relationsh­ip exists between the database entities. After that you will add the functional­ity and validation rules to the different domain classes.

class items { static hasMany = [location:location, category:category] int items_id String version Date lastUpdate­d Boolean recorded= false SortedSet comments static constraint­s = { title(nullable:false, blank:false, length:1..50) version (length:0..100) lastUpdate­d(nullable:true) recorded(nullable:false) } }

The next step is to expose your domain objects to users through some controller­s and views. This will allow you to manipulate your domain objects through a user interface. One important thing is that, you don’t have to use getters

and setters of Java Bean classes like we use to do in other j2ee framework like Struts.To create the controller­s for your domain, go back to the command line and run the following commands:

grails create-controller items

Once you are through with your classes, you can configure the applicatio­n to use the MySQL database. As mentioned earlier, the whole working revolves around the MVC architectu­re and the user will interact through the gsp/jsp page.

 ??  ?? Code snippet and Sub-directory structure of the applicatio­n developmen­t configured on Netbeans IDE.
Code snippet and Sub-directory structure of the applicatio­n developmen­t configured on Netbeans IDE.
 ??  ?? Process diagram shows how a user interacts with the system and how his request is processed & output is displayed.
Process diagram shows how a user interacts with the system and how his request is processed & output is displayed.
 ??  ??
 ??  ?? Different stages of inventory applicatio­n, login page at top left, data entry page at the bottom and report generation page at the right
Different stages of inventory applicatio­n, login page at top left, data entry page at the bottom and report generation page at the right
 ??  ??

Newspapers in English

Newspapers from India