Friday, 1 February 2013

Spring Roo simple hello world example in STS - What is spring roo ?

Today we are going to discuss about Spring Roo, in this blog we will come to know what spring roo is ? and how can we use it in a productive environment to achieve industry best practices. We are going to implement a simple spring mvc application using spring roo and come to know about automatic creation of domain, controller and views in our application.

Roo makes application development easy in a fundamental and sustainable improved way without compromising to productivity, engineering integrity and flexibility.

It means that spring roo makes you implement your business ideas implementation fast and easy. The most interesting part of spring roo is that you are not bound to accept the generated code as it is, rather you can change the code at any point of time to make it fit according to you needs.

What is Spring Roo ?
Spring roo is simply a tool that sits there in the background and handles the things that you do not want to worry about. Using Roo you can focus on your domain model and business need and everything else will be handled by spring itself.

There is no roo.jar dependency to add in your application explicitly, so it is purely a tool that helps you things going with a less effort.

Spring Roo simply makes the developer's work easy without getting in their way. Roo provides tools to fetch the application out of it completely and after that you are free with your application to use any coding patter you choose or any technology you want to add to your application.

Spring Roo in STS
If you are using STS you do not need to add something else, everything is already added to it for you to start working with roo. STS has almost every plugin installed in it that you would typically need and you can add more plugins to it as per your need. STS is based on Eclipse IDE with a better Spring MVC development environment.

Lets start with some demo application with STS IDE and let the real magic begin.The first thing we will do is to invoke a wizard and start a spring roo application as shown in picture :

Click finish and it will create a project ready for us, a project in typical maven style having a pom.xml in it.

Now the wondeful thing about roo is that it will guides you through the project creation process, every time. If you do not know what to do, just write hint in roo shell and you will get things to choose like the screen below.

Adding database and JPA details
Choose your db type from a list of all available databases and add JPA tool like hibernate if you want to add one :

Provider means the jpa implementation that you want to use, for now lets choose hibernate and click ok. Roo will add all required dependencies according to your db and Jpa.

Adding Domain Entity to your database

Write entity-jpa and Ctrl+Space, now choose the package and domain name that you want to add:

UseCtrl+Space every time to get a autocomplete for your commands.

Roo will add all folders and details for your domain classes as shown in the figure below:

As the figure shown below indicates that roo will create a domain class for you automatically in your project structure:

Adding fields to Database Entity
Don't know what to do next ? No worries ! Just ask roo again what to do now, simply write hint again and hit enter. Its suggesting to add some fields to your database table, simple write fields and Ctrl+space. Choose a datatype for your field from list of parameters and give a name for field.

Every time you want to see available parameters or command list hit Ctrl+Space and choose one from suggesting list.Adding Controllers
Write controller and press Ctrl+Space, now select your root package, add a sub package and controller name that you want to add, roo will add all files and packages to your project as shown in the figure below :

Adding Views

Views are automatically added to your project structure according to domain and controller you have added so far as shown in th figure below

Note that the generated code for domains, controller and views can be edited manually at any point of time without any interrupt.