Grails is an open source platform that uses Groovy for developing Java Enterprise Applications, it’s used for handling everything from the view layer down into your persistence layer. Grails has leveraged the most popular open source technologies by adopting them in its platform inherently, Hibernate, Spring, SiteMesh, Tomcat, and H2. All of these frameworks, platforms and database engine are built inside the Grails platform without any need of doing additional configuration steps. This tutorial walks you through the writing simple hello world program using the Grails framework.
also read:
Grails introducing another layer of abstraction via Groovy language, you will not be aware of all of these mentioned technologies, when it comes to deal with Grails platform, but you will have basic touch of those technologies. Look at the below picture to know the architecture of Grails stack.
As you’ve seen, Grails isn’t a web framework, is a full featured environment presented for helping all Java Developers, taking the full advantage of Java and JVM and at the same time working with dynamic, popular and flexible language like Groovy.
No other dynamic language on the JVM integrates with the Java like Groovy, it’s designed to work seamlessly with Java at every level, starting from the syntax reaching into other similarities that can be summarized as follow:
- The Groovy grammar is derived from the Java 5 grammar, making most valid Java code also valid Groovy code.
- Groovy objects are Java objects, it is one of the great advantage.
- Through Groovy’s joint compiler you can have circular references between Groovy and Java without getting issues while compilation.
Grails platform has firstly initiated at July 2005, where the final stable release was 2.4.0 which released at May 2014. Because of its robustness and maturity of Grails platform, many popular sites have adopted it such as LinkedIn, Atlassian,Sky, eHarmony and others. This tutorial intended to introduce you the Grails platform and stepping you into getting Grails installed and making a HelloWorld application.
1. Grails Installation
By following the below steps you will be able to installing the Grails platform into your machine.
- Download JDK 1.6+ and configure the JAVA_HOME.
- Configure PATH system variable for containing the value of JAVA_HOME\bin. That’s making the Java compiler and interpreter accessible from anywhere into your machine.
- Make sure that you are able to execute the java on the command line.
- Download Grails platform.
- Unzip the Grails platform into your machine’s directory. We’ve unzipped into D:\Grails\grails-2.4.0.
- Configure your PATH system variables for the Grails.
- Make sure that grails command is recognized by the command console by opening a new command console and type grails and press enter. Grails will be opened in an interactive mode as you would see below. In case you’ve provided grails -version you would see a direct response. One big advantage for the interactive mode is that the Tab Completion.
- In case you would be asking help about any of the Grails commands, just type the grails followed with the help instruction plus your inquired command like grails help create-app.
2. Creating the Grails Hello World Application
For creating your own first Grails application, which will include a simple controller that would use the render scriptlet for providing a message for the browser, you have to follow the below steps.
- It’s easy step, and for it’s the first step you must go through at every time you want creating a Grails application.
- Either you’re using the Grails interactive mode or the default mode you have to type grails create-app <<application-name>> or you could use grails create-app and the Grails platform would prompt you asking about the name of the application.
- Regardless of the used way, your windows console should look like
Upon completion, the command have created your Grails application and the necessary directory structure. You can see the mentioned structure by executing tree command.
As you’ve seen the directories have created by the Grails. The same folder structures will be used for the next coming tutorials. But it’s noticed that Grails application contains directories for controllers, domain objects (models) and views where all of those are located beneath the grails-app folder.
3. Creating Controller
- Grails is an excellent MVC platform, which means it has models, views and controllers for implementing the Separate of Concern (SoC) concept.
- Controllers, are those components in the MVC architecture that responsible for manipulating the data that received from the view and determining the next view that should be rendered.
- For creating controllers within Grails, you have simple command create-controller, which will do creation easily. This command isn’t mandatory as in the next coming tutorials, I will explain how to define and create the controllers through IDE like Eclipse.
- Type grails create-controller HelloWorldController into your command console, and make sure that you’re beneath the Grails application that you’ve created.
- If you’ve missed your Grails application and continue creating your controller, you will get an error tells you You aren’t inside Grails Application.
- Once the create-controller command has finished running, Grails will have created two classes for you; a new controller called HelloWorldController within grails-app/controllers directory and an associated test case in the test/unit directory (Grails assumes you’ll need a test if you’re writing a controller). Already Grails defaulted you the created controller within helloworldapplication package, since you’ve not specified the package name parameter while creating.
It’s good for you to know that dynamic language like Groovy doesn’t provide you as much compile-time sensation as typed language like Java. Some errors that you might expect to be caught at compile time are actually left to runtime. For that purpose, Grails provides you a Test Unit file listed above.
4. Grails Project Structure
Now, it’s the time for seeing the whole structure of the Grails application.
- Customizing Controller Response: Since the controller HelloWorldController.groovy was created by Grails platform, it was contained the default single action called index.
HelloWorldController.groovy
package helloworldapplication class HelloWorldController { def index() { } }
By the convention, index declaration instructs Grails to try render a view called grails-app/views/helloWorld/index.gsp automatically. Even, the controller looking for index.gsp but you can customize the controller for custom display. Open the controller and type into render ‘JavaBeat ! Hello World !’ inside the braces of index method like below:
HelloWorldController.groovy
package helloworldapplication class HelloWorldController { def index() { render 'JavaBeat ! Hello World !' } }
Now, it’s time for getting the application running.
5. Run Grails Application
To run the application using Grails, command like run-app will make things easy. Make sure you’re locating at the root folder of the application (i.e. HelloWorldApplication) and execute run-app command. Wait for some time, Grails platform for providing you the URL for accessing your deployed application like below.
Copy the url mentioned above and paste it into your browser’s address. After pasting that URL, you should see the Grails main page which gives you an indicator of successful deployment.
As well as, if you’re looking into the available controllers you should find your created controller HelloWorldController listed above. Just, press into your controller to see the custom response that was developed using the render scriptlet.
Now, what should happened if we’ve left the index method blanked and at the same time we develop a new Groovy Server Page index.gsp.
index.gsp
<!DOCTYPE html> <html> <head> <meta name="layout" content="main"/> <title>Welcome to Grails</title> </head> <body> JavaBeat ! Hello World ! </body> </html>
- For stopping the Grails server, just type stop-app from the same location that you’ve initiated the run-app.
- Make sure that you are created the Groovy Server Page (index.gsp) and it’s located under grails-app/views/helloWorld.
- Run the application again to see different view like below.
6. Summary
Grails is a platform created for developing Java EE applications in an easy manner. Grails depends on the Dynamic language like Groovy which is considered as a JVM language with less restrictions in comparison to Java. This tutorial intended for providing you a clear informative about Grails platform and guiding you through steps for creating your own first Grails application. Feel free to contribute us by commenting below.