Book Image

PrimeFaces Cookbook

Book Image

PrimeFaces Cookbook

Overview of this book

PrimeFaces is the de facto standard in the Java web development. PrimeFaces is a lightweight library with one jar, zero-configuration, and no required dependencies. You just need to download PrimeFaces, add the primefaces-{version}.jar to your classpath and import the namespace to get started. This cookbook provides a head start by covering all the knowledge needed for working with PrimeFaces components in the real world. "PrimeFaces Cookbook" covers over 100 effective recipes for PrimeFaces 3.x which is a leading component suite to boost JSF applications. The book's range is wide‚Äí from AJAX basics, theming, and input components to advanced usage of datatable, menus, drag & drop, and charts. It also includes creating custom components and PrimeFaces Extensions.You will start with the basic concepts such as installing PrimeFaces, configuring it, and writing a first simple page. You will learn PrimeFaces' theming concept and common inputs and selects components. After that more advanced components and use cases will be discussed. The topics covered are grouping content with panels, data iteration components, endless menu variations, working with files and images, using drag & drop, creating charts, and maps. The last chapters describe solutions for frequent, advanced scenarios and give answers on how to write custom components based on PrimeFaces and also show the community-driven open source project PrimeFaces Extension in action.
Table of Contents (17 chapters)
PrimeFaces Cookbook
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

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:

  1. We will create the folder where the project will reside and then change directory to it.

    mkdir primefaces-cookbook-roo
    cd primefaces-cookbook-roo
    
  2. Now we can execute the roo command within the folder. Linux, Unix, or Mac OS users should execute roo.sh, and Windows users should execute the roo.bat file. Then, we should see the console output as shown in the following screenshot:

  3. 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
    
  4. 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 as HIBERNATE and stating that an in-memory database will be used for testing purposes.

  5. 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
    
  6. 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
    
  7. 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
    
  8. 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.