PrimeFaces scaffolding with Spring Roo
Spring Roo is a next-generation rapid application development tool that uses Convention over Configuration principles. It is a text-based open source tool that can be used for creating and managing Spring-based applications. It uses mature libraries such as Spring framework, Java Persistence API, Java Server Pages (JSP), Spring Security, Spring Web Flow, Log4J, and Maven. Spring Roo also provides scaffolding for web-based applications that are powered by PrimeFaces.
Getting ready
At the time of writing this book, the latest version of Spring Roo was version 1.2.2. It can be downloaded at http://www.springsource.com/download/community?project=Spring%20Roo. More information on Spring Roo, along with the list of commands, can be found at http://www.springsource.org/spring-roo.
Also, Maven—the Apache build manager for Java projects—needs to be installed as a prerequisite. At the time of writing this book, Maven v3.0.4 was used. For more information on this, please visit http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html.
How to do it...
For PrimeFaces scaffolding with Spring Roo, follow these steps:
We will create the folder where the project will reside and then change directory to it.
mkdir primefaces-cookbook-roo cd primefaces-cookbook-roo
Now we can execute the
roo
command within the folder. Linux, Unix, or Mac OS users should executeroo.sh
, and Windows users should execute theroo.bat
file. Then, we should see the console output as shown in the following screenshot:Then, in the command line, we can create the sample
roo
project by specifying its name and its packaging structure as follows:roo> project --projectName primefaces-cookbook-roo --topLevelPackage org.primefaces.cookbook
After that, we can add a persistency layer to the project structure as follows:
roo> persistence setup --provider HIBERNATE --database HYPERSONIC_IN_MEMORY
With the
persistence
command, we are specifying the provider asHIBERNATE
and stating that an in-memory database will be used for testing purposes.Create
enum
for the type of manufacturer of the car.roo> enum type --class ~.domain.Manufacturer roo> enum constant --name Volkswagen roo> enum constant --name Mercedes roo> enum constant --name BMW roo> enum constant --name Audi
Then we can create the class for the car.
roo> entity jpa --class ~.domain.Car roo> field number --fieldName yearOfManufacture --type java.lang.Integer --notNull roo> field string --fieldName name --notNull roo> field enum --fieldName manufacturer --type ~.domain.Manufacturer –-notNull
After that, we can create the project and package it as a
.war
file:roo> web jsf setup --implementation APACHE_MYFACES --library PRIMEFACES --theme EGGPLANT roo> web jsf all --package org.primefaces.cookbook
Then, finally exit Spring Roo console with:
roo> quit
When we list the directory for the files, we have two files and a source directory created as follows in the Linux, Unix, or Mac OS environment:
-rw-r--r-- 1 primeuser staff 834 May 21 16:45 log.roo -rw-r--r-- 1 primeuser staff 17650 May 21 16:45 pom.xml drwxr-xr-x 3 primeuser staff 102 May 21 16:45 src
Now our project is ready to run. From the command line, we can execute jetty
to get the web application context up and running.
mvn jetty:run
Now we can request for the URL on local via browser (http://localhost:8080/primefaces-cookbook-roo
), which will bring up the scaffold user interface of the web application, as shown in the following screenshot:
How it works...
With the commands provided, Spring Roo does all the scaffolding to create a web application powered by PrimeFaces. In the end, it will create the Car
and Manufacturer
domain classes, the converter for the Car
class, and the web pages for providing the CRUD operations on the Car
class.