Advertisment

GRAILS: An Open Source Framework for Rapid App Development

author-image
PCQ Bureau
New Update


Advertisment

Advertisment

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-productivity framework by following the “coding by convention” paradigm, providing a stand-alone development environment and hiding much of the configuration 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)

Advertisment



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 productivity when compared to traditional Java web frameworks -No XML configuration, Ready-to-use development environment, Functionality available through mixins and Reduce the need for configuration files and other boilerplate code.

Advertisment





Apart from these properties there are several other features such as Language constructs, List & Map support, XML & Swing capabilities 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 relationship. GORM provides very powerful dynamic methods for CRUD (Create/Read/Update/Delete) operations. Moreover, an HSQL database comes built-in with GRAILS.

Advertisment



Let's Start Building Apps



You can find install instructions on the framework's website. Just install it and set the Groovy_Home and Java_Home environment variable. Also, add Groovy_Home/bin to your path. After configuring the GRAILS with any IDE like Netbeans or Eclipse, you are ready for the application development. First go to the development workspace and run the following in the command prompt:

grails create-app Enterprise Inventory

Advertisment

This will create a folder called Enterprise Inventory and a grails-app directory under that, which contains the sub-directories named as Configuration, Controllers, Domain classes, Services, Tag Libraries, Utility and Views.



The application simply displays products, location and users from a database. Of course there is a lot more that you can add to this application, 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

Advertisment

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



class items {

static hasMany =

int items_id

String version

Date lastUpdated

Boolean recorded= false

SortedSet comments

static constraints = {

title(nullable:false, blank:false, length:1..50)

version (length:0..100)

lastUpdated(nullable:true)

recorded(nullable:false)

}

}



The next step is to expose your domain objects to users through some controllers 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 controllers 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 application to use the MySQL database. As mentioned earlier, the whole working revolves around the MVC architecture and the user will interact through the gsp/jsp page.



Advertisment