Before you begin you’ll need to download Roo from http://www.springsource.org/spring-roo#download and set up the ROO install directory in your environment variables. This will allow you to launch the Roo command shell from the command prompt.
1. Start up command prompt and move the directory that you’d like to build your application in. For this tutorial I’ll be building my app in C:\tutorial.
2. Once you've navigated to your chosen directory simply enter ‘roo’ to start up the Roo command shell. You should see the shell start up something like figure 1.0 below.
3. Enter the following command
project --topLevelPackage com.tutorial.roo --projectName RooDemo
This command will create a new Maven project skeleton using the specified package and project names. The project skeleton will contain a resources directory structure as well as basic log4J and Spring application context configuration files. See figure 1.1 below.
4. The next step is to setup our persistence tier. To do this type the following command.
jpa setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
This command sets up a JPA persistence tier using Hibernate as our JPA implementation. Pressing the tab key after --provider will list all available options but for the sake of this demo I’m going to stick with Hibernate as this is a common choice for many Spring applications right now. The --database portion of
the command tells Roo which database we want to use to persist our data. Again there are a range of
options here including Oracle, MySQL and SQLServer. For the sake of simplicity I’ve chosen a Hypersonic in Memory Database.
When you issue this command you’ll see that Roo updates the Spring application context, creates a new database properties file and adds a persistence.xml to the existing project structure. Our project now has the basic wiring in place required for Hibernate persistence.
You’ll also notice that Roo has added a number of dependencies to the projects POM file. Roo will only add dependencies as they are needed so as to keep the deployable artefact (WAR file) as lean as possible.
5. Now that we have a basic project structure in place (including our persistence configuration) its time to add some entities to our application. These are the actual business or domain objects that form the backbone of our application. To keep this tutorial simple my application is going to have just 2 entities, a Student entity and a Subject entity. As you’ve probably already guessed I’m modelling an ultra simple Student Management application. To create a student entity issue the following command.
entity jpa --class ~.domain.Student
You’ll see from figure 1.4 below that Roo has generated a number of new files. Student.java is a simple POJO representing a Student in our application. You’ll see that the class has a number of annotations, these allow the class to be managed by Roo as the project is modified. Aside from these annotations this is a plain Java bean that can be used to encapsulate Student related business logic. There are a number of related AspectJ files generated – Student_Roo_Jpa_Entity.aj and Student_Roo_Jpa_ActiveRecord.aj allow Student.java to behave as a JPA entity with basic CRUD functionality made available out of the box. This means that a Roo generated entity can be used to perform basic database CRUD operations without any coding from the developer. Obviously more useful applications will require custom data access functions (query by surname for example) but these can be added to the Student entity as required and will work alongside the basic CRUD functionally provided by default.
Student_Roo_Configurable.aj allows the Student bean to become a Spring managed bean and Student_Roo_ToString.aj provides a useful implementation of the toString method using reflection.
6. Next run the following command to generate a Subject entity
entity jpa --class ~.domain.Subject
7. Next we’ll start adding attributes to our domain objects. Use the following command to create a forename instance variable and the associated getter and setter method.
field string --fieldName forename --class ~.domain.Student
You’ll notice that Roo has generated a new file called Student_Roo_JavaBean.aj. This file contains the setters and getters for all fields defined on an entity.
8. Add 2 more fields to our Student entity by issuing the following commands.
field string --fieldName forename --class ~.domain.Student
field date --type java.util.Date --fieldName dateOfBirth --class ~.domain.Student
9. Now we’ll add 2 fields to our Subject entity by issuing the following commands.
field string --fieldName name--class ~.domain.Subject
field boolean --fieldName mandatory--class ~.domain.Subject
10. Next we need to create a relationship between the Student and Subject entity. This will allow us to find subjects that a particular student is registered for. We do this by issuing the following command
field set --type ~.domain.Subject --fieldName subjects --class ~.domain.Student --cardinality
11. Now that we’ve defined our entity model we can start looking at the application front end. Roo can generate all necessary Spring MVC scaffolding from an entity model allowing you to quickly build a simple CRUD web front end for your application in a matter of minutes. The following command will generate Spring MVC configuration files, tag libraries, JSPs, standard images and CSS for our application front end.
web mvc setup
12. The next step builds on top of our existing MVC skeleton by adding MVC Controllers for each entity and some top level JSPs that will provide basic CRUD functionality for each of the entities in our application. Run the following command
web mvc all –package ~.web
13. At this point all the core components required by a standard Spring MVC application are in place. We can now package the application into a WAR (web archive) so that in can be deployed into a web container. We package the application with the command below.
14. We can now exit the Roo shell using the ‘quit’ command – this will take us back to the command prompt. The WAR we built using the ‘perform package’ command can be deploy to any servlet container, for convenience I'm going to deploy it to Jetty using Maven. You’ll obviously need Maven installed and available on the command line. Running the following command should deploy the application to port 8080 on localhost as shown in the image below.
In my next post I'll cover the the Roo framework architecture in a little more detail and provide an insight into how Roo works along side existing Java technologies to enable rapid application development.