PrimeFaces Beginner's Guide - K. Siva Prasad Reddy - E-Book

PrimeFaces Beginner's Guide E-Book

K. Siva Prasad Reddy

0,0
39,59 €

-100%
Sammeln Sie Punkte in unserem Gutscheinprogramm und kaufen Sie E-Books und Hörbücher mit bis zu 100% Rabatt.
Mehr erfahren.
Beschreibung

PrimeFaces is a lightweight UI component framework for JSF based applications. PrimeFaces is very easy to use because it comes as a single JAR file and requires no mandatory XML configuration. It provides more than 100 UI Components and an in-built AJAX support. It also provides theme support for UI components with more than 30 themes out-of-the-box. With PrimeFaces, developers can create rich user interfaces very easily.PrimeFaces Beginners Guide is a practical, hands-on guide that provides you with clear step-by-step exercises,that will help you to learn and explore the features of PrimeFaces.PrimeFaces Beginners Guide starts by showing you how to install PrimeFaces, create sample forms, and perform validations and then looks at various commonly used PrimeFaces utility components. Next, you will look into various basic text input components like form controls, Calendar, AutoComplete, and Rich Text Editor. Then you will learn about advanced UI components such as DataTables, panels, menus,and charts. Throughout the chapters we will be building a sample web application using PrimeFaces progressively that will give you a hands-on experience on using PrimeFaces effectively.You will learn how to create complex layouts using accordion panels, tab views, sophisticated menu navigations, breadcrumbs and much more. You will also learn how to display data using DataTable with pagination, filters, and lazy loading, and how to export data to Excel or PDF features. You will learn how to represent data in various formats like trees, charts, and TagCloud. You will also learn how to build an application supporting multiple themes.With this PrimeFaces Beginner's Guide , you will learn how to use PrimeFaces easily and effectively.

Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:

EPUB
MOBI

Seitenzahl: 359

Veröffentlichungsjahr: 2013

Bewertungen
0,0
0
0
0
0
0
Mehr Informationen
Mehr Informationen
Legimi prüft nicht, ob Rezensionen von Nutzern stammen, die den betreffenden Titel tatsächlich gekauft oder gelesen/gehört haben. Wir entfernen aber gefälschte Rezensionen.



Table of Contents

PrimeFaces Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Time for action – heading
What just happened?
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
1. Introduction to PrimeFaces
Introducing the features of PrimeFaces
Installing and configuring PrimeFaces
Time for action – installing and configuring PrimeFaces
What just happened?
Creating a HelloWorld application using PrimeFaces
Time for action – creating a HelloWorld program using PrimeFaces
What just happened?
Creating simple forms using PrimeFaces
Time for action – creating a user registration form
What just happened?
Performing form validations
Time for action – validating the user registration form
What just happened?
Performing client-side validations
Time for action – client-side e-mail validation
What just happened?
Understanding partial page rendering (PPR)
Time for action – partial processing on the user details form
What just happened?
Submitting partial data to the server
Updating the view using AJAX
Time for action – updating the view using AJAX
What just happened?
Updating the view using AJAX listeners
Time for action – validate the UserName using AJAX listeners
What just happened?
Performing tasks periodically using a poll component
Time for action – using the poll component to display the current time
What just happened?
Controlling the polling process using external triggers
Time for action – manually start and stop polling
What just happened?
Invoking server-side methods from JavaScript using RemoteCommand
Time for action – validate e-mail using RemoteCommand
What just happened?
Summary
2. Introducing Sample Application TechBuzz
Understanding the requirements of the TechBuzz application
Understanding the high-level design of the TechBuzz application
Understanding the TechBuzz data model
Looking at TechBuzz screenshots
User authentication
TechBuzz home page
User account screen
Creating a post screen
User roles management
Posts in tree view
Posts frequency charts
Setting up of the development environment for TechBuzz
Time for action – installing the required software
What just happened?
Summary
3. Using PrimeFaces Common Utility Components
Introducing the Message component
Time for action – displaying FacesMessage using <p:message>
What just happened?
Displaying messages with HTML content
Time for action – displaying FacesMessage with HTML content
What just happened?
Introducing the Messages component
Time for action – displaying FacesMessage using <p:messages>
What just happened?
Displaying notifications using the growl component
Time for action – displaying FacesMessages with growl
What just happened?
Displaying tooltips using the Tooltip component
Time for action – displaying tooltip for UI components
What just happened?
Using global tooltips
Time for action – using global tooltips
What just happened?
Partial Processing and Rendering using the Fragment component
Time for action – using the Fragment component
What just happened?
Introducing the Sticky component
Introducing the RequestContext utility
Updating UI components
Time for action – updating UI components using RequestContext.update()
What just happened?
Executing JavaScript from server-side code
Time for action – executing JavaScript using RequestContext.execute()
What just happened?
Adding AJAX callback parameters
Time for action – adding callback parameters using RequestContext.addCallbackParam()
What just happened?
Scrolling to a component
Displaying pop-up dialogs using the Dialog component
Time for action – displaying a dialog
What just happened?
Using the Dialog component's client-side callbacks
Time for action – client-side callbacks for onShow and onHide
What just happened?
Handling the dialog close event
Time for action – the Dialog component close event listener
What just happened?
Working with a Dialog component containing forms
Time for action – creating dialog with a registration form
What just happened?
Introducing dialog framework
Time for action – showing the search users screen in dialog
What just happened?
Passing data from the dialog back to the source page
Time for action – passing data from the search users dialog to the source page
What just happened?
Displaying FacesMessage in dialog
Introducing the ConfirmDialog component
Time for action – creating a confirmation dialog
What just happened?
Using the global ConfirmDialog component
Displaying notifications using the NotificationBar component
Time for action – displaying notification messages
What just happened?
Hiding NotificationBar automatically
Time for action – automatically hiding NotificationBar
What just happened?
Blocking a region using the BlockUI component
Time for action – blocking the form
What just happened?
Understanding PrimeFaces selectors
Time for action – updating UI components using jQuery Selectors
What just happened?
Introducing the search expression framework
Summary
4. Introducing the PrimeFaces Client Side Validation Framework
Configuring and using the Client Side Validation framework
Time for action – performing client-side validations
What just happened?
Triggering client-side validations based on events
Supporting I18N for validation messages
Extending the CSV framework with custom JSF validators
Time for action – using the custom JSF validator on the client side
What just happened?
Extending the CSV framework with custom JSF converters
Time for action – using the custom JSF converter on the client side
What just happened?
Using the CSV framework with the Bean Validation API
Time for action – using the CSV framework with the Bean Validation API
What just happened?
Extending the CSV framework with custom Bean Validation annotations
Time for action – using the custom Bean Validation annotation with the CSV framework
What just happened?
Summary
5. Introducing Text Input Components
Getting text input with the InputText component
Time for action – using the InputText component
What just happened?
Introducing the InputTextarea component
Time for action – using InputTextarea
What just happened?
Getting formatted input using the InputMask component
Time for action – reading formatted input using InputMask
What just happened?
Introducing the Password component
Time for action – using password strength indicator and match mode
What just happened?
Introducing the Editor component
Time for action – using editor client-side API methods
What just happened?
Inplace editing using the Inplace component
Time for action – using the Inplace editor
What just happened?
Providing completion suggestions using the AutoComplete component
Time for action – using basic AutoComplete
What just happened?
Using AutoComplete with POJO support
Time for action – using AutoComplete with POJO support
What just happened?
Selecting multiple items
Time for action – selecting multiple items using AutoComplete
What just happened?
Handling the ItemSelect and ItemUnselect Events
Time for action – using the ItemSelect and ItemUnselect events
What just happened?
Displaying tooltips using the Itemtip facet
Time for action – displaying tooltips on suggested items using the Itemtip facet
What just happened?
Summary
6. Working with Selection Input Components
Creating the toggle button using SelectBooleanButton
Time for action – using the SelectBooleanButton component
What just happened?
Creating the On or Off options using SelectBooleanCheckbox
Time for action – using the SelectBooleanCheckbox component
What just happened?
Introducing SelectManyCheckbox
Time for action – using the SelectManyCheckbox component
What just happened?
Introducing SelectOneRadio
Time for action – using the SelectOneRadio component
What just happened?
Introducing SelectOneButton
Time for action – using the SelectOneButton component
What just happened?
Introducing SelectManyButton
Time for action – using the SelectManyButton component
What just happened?
Creating drop-down lists using SelectOneMenu
Time for action – using SelectOneMenu with editable and filter features
What just happened?
Using SelectOneMenu with POJOs
Time for action – using SelectOneMenu with POJOs
What just happened?
Grouping options in SelectOneMenu
Time for action – grouping options in SelectOneMenu
What just happened?
Introducing SelectOneListbox
Time for action – using SelectOneListbox with POJOs
What just happened?
Introducing SelectManyMenu
Time for action – using SelectManyMenu
What just happened?
Creating the overlay menu using SelectCheckboxMenu
Time for action – using SelectCheckboxMenu
What just happened?
Creating the overlay menu with a default command using SplitButton
Time for action – using SplitButton
What the just happened?
Introducing the PickList component
Time for action – using the basic PickList component
What just happened?
Using the PickList component with POJO support
Time for action – using the advanced PickList component
What just happened?
Introducing the MultiSelectListbox component
Time for action – using the MultiSelectListbox component
What just happened?
Summary
7. Introducing Advanced Input Components
Introducing the Calendar component
Time for action – displaying a pop-up Calendar with navigator
What just happened?
Understanding Internationalization (I18N) and Localization (L10N)
Time for action – displaying a multipage calendar with Localization and the dateSelect event listener
What just happened?
Restricting the date selection within the date range
Time for action – using Calendar with a date range
What just happened?
Using Calendar with advanced customization options
Time for action – using Calendar's advanced customization options
What just happened?
Using the Calendar component's time picker options
Time for action – Calendar with the time picker options
What just happened?
Star-based rating using the Rating component
Time for action – using the Rating component
What just happened?
Introducing the Spinner component
Time for action – using the Spinner component
What just happened?
Getting input on a scale using the Slider component
Time for action – using the Slider component
What just happened?
Preventing spam and bots using CAPTCHA validation
Time for action – using CAPTCHA for user registration
What just happened?
Uploading files using the FileUpload component
Time for action – using the FileUpload component
What just happened?
Downloading files using the FileDownload component
Time for action – using the FileDownload component
What just happened?
Summary
8. Working with Data Components
Introducing the DataList component
Time for action – displaying unordered and ordered data using DataList
What just happened?
Using DataList with pagination support
Time for action – using DataList with pagination
What just happened?
Displaying tabular data using the DataTable component
Using pagination support
Time for action – using DataTable with pagination
What just happened?
Using columns sorting support
Using column filtering support
Time for action – using DataTable with filtering
What just happened?
Selecting DataTable rows
Selecting a single row
Selecting rows using a row click
Selecting rows using a radio button / checkbox
Time for action – using DataTable with multiple row selection support
What just happened?
Using the row expansion feature
Using the inline row editing feature
Time for action – using DataTable with row editing support
What just happened?
Using the cell editing feature
Time for action – using DataTable with cell editing support
What just happened?
Loading data leisurely
Time for action – loading the DataTable data leisurely
What just happened?
Using the column grouping support
Using the scrolling feature
Using the frozenRows feature
Applying custom styles for rows
Using the resizable and draggable columns feature
Displaying data in the grid layout using the DataGrid component
Exporting data into PDF/XLS/XML/CSV formats using the DataExporter component
Time for action – using DataExporter to export data into XLS and PDF formats
What just happened?
Summary
9. Introducing Advanced Data Visualization Components
Displaying data with sliding effects using the Carousel component
Time for action – creating Carousel using tabs
What just happened?
Introducing the TagCloud component
Time for action – displaying tags as TagCloud
What just happened?
Time for action – choosing a tag in TagCloud with the select Event
What just happened?
Displaying hierarchical data using the Tree component
Time for action – creating a Tree component
What just happened?
Loading tree nodes dynamically using AJAX
Selecting tree nodes
Handling node expand, collapse, select and unselect events
Rendering the tree horizontally
Displaying nodes with icons
Reordering Tree nodes using Drag and Drop
Introducing the TreeTable component
Time for action – displaying posts using the TreeTable component
What just happened?
Introducing the Schedule component
Time for action – creating the Schedule component
What just happened?
Summary
10. Working with Layout Components
Introducing the Panel component
Time for action – using Panel with event listeners
What just happened?
Using Panel with pop-up menu
Using Panel with custom actions
Time for action – creating Panel with custom actions
What just happened?
Introducing the PanelGrid component
Time for action – using PanelGrid with rowspan and colspan features
What just happened?
Displaying overflowed content using ScrollPanel
Time for action – using a ScrollPanel component
What just happened?
Creating workflows using a Wizard component
Time for action – using Wizard to implement workflows
What just happened?
Creating stacked panels using the AccordionPanel component
Loading tabs content dynamically
Creating a dynamic number of tabs
Handling tabChange and tabClose events
Time for action – creating AccordionPanel with dynamic tabs and event listener
What just happened?
Creating a tabbed panel using a TabView component
Loading tabs content dynamically
Creating TabView with closable tabs
Tab headers with different orientations
Creating TabView with a dynamic number of tabs
Creating TabView with scrollable tabs
Handling TabView component's events
Time for action – using TabView with dynamic tabs and event listeners support
What just happened?
Creating complex layouts using the Layout component
Time for action – creating FullPage layout
What just happened?
Creating an element-based layout
Creating nested layouts
Handling layout events
Creating portal like layout using a Dashboard component
Time for action – creating a Dashboard layout
What just happened?
Summary
11. Introducing Navigation Components
Understanding MenuModel
Building menus declaratively
Building menus programmatically
Introducing the Menu component
Time for action – creating a simple Menu
What just happened?
Displaying Menu based on trigger
Time for action – displaying an overlay menu with trigger
What just happened?
Introducing the MenuButton component
Time for action – creating MenuButton
What just happened?
Displaying multilevel nested menus using TieredMenu
Time for action – displaying a multilevel menu using TieredMenu
What just happened?
Creating nested menus with SlideMenu
Time for action – creating the SlideMenu component
What just happened?
Creating a horizontal navigation menu using Menubar
Time for action – creating a Menubar component
What just happened?
Creating a multicolumn menu using MegaMenu
Time for action – creating MegaMenu with multiple columns
What just happened?
Creating tab-based menus using TabMenu
Time for action – creating the TabMenu component
What just happened?
Time for action – tracking an active tab dynamically
What just happened?
Introducing the PanelMenu component
Time for action – creating the PanelMenu component
What just happened?
Introducing ContextMenu
Time for action – creating ContextMenu
What just happened?
Integrating DataTable with ContextMenu
Time for action – creating ContextMenu for DataTable
What just happened?
Integrating Tree with ContextMenu
Time for action – creating ContextMenu for the Tree component
What just happened?
Introducing the Breadcrumb navigation menu
Time for action – creating the Breadcrumb menu
What just happened?
Time for action – creating the Breadcrumb menu programmatically
What just happened?
Summary
12. Drawing Charts
Creating a Line chart
Time for action – creating a Line chart
What just happened?
Creating an Area chart
Creating a Bar chart
Time for action – creating a Bar chart
What just happened?
Creating a Pie chart
Time for action – creating a Pie chart
What just happened?
Creating a Donut chart
Exporting charts as images
Rendering dynamic charts using the JFreeChart API
Time for action – creating a Pie chart using the JFreeChart API
What just happened?
Creating interactive charts using the ItemSelect AJAX event
Summary
13. Using PrimeFaces Themes
Configuring and using themes
Using stateless ThemeSwitcher
Time for action – using the stateless ThemeSwitcher component
What just happened?
Using stateful ThemeSwitcher
Time for action – applying a user-specific theme using stateful ThemeSwitcher
What just happened?
Creating and using a custom theme
Time for action – creating a new theme
What just happened?
Drag-and-drop support
Working with GoogleMaps using the GMap component
Multimedia components
PrimeFaces push
The PrimeFaces extensions
Time for action – installing and configuring PrimeFaces extensions
What just happened?
PrimeFaces mobile
PrimeUI
Summary
Index

PrimeFaces Beginner's Guide

PrimeFaces Beginner's Guide

Copyright © 2013 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.

Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.

Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.

First published: November 2013

Production Reference: 1181113

Published by Packt Publishing Ltd.

Livery Place

35 Livery Street

Birmingham B3 2PB, UK.

ISBN 978-1-78328-069-8

www.packtpub.com

Cover Image by Prashant Timappa Shetty (<[email protected]>)

Credits

Author

K. Siva Prasad Reddy

Reviewers

Thomas Andraschko

Mauricio Fenoglio

Sudheer Jonna

Nilesh Namdeo Mali

Acquisition Editor

Joanne Fitzpatrick

Lead Technical Editor

Ritika Dewani

Technical Editors

Gauri Dasgupta

Jalasha D'costa

Dipika Gaonkar

Siddhi Rane

Project Coordinator

Kranti Berde

Proofreaders

Julie Jackson

Joanna McMahon

Indexer

Tejal R. Soni

Graphics

Sheetal Aute

Production Coordinator

Nilesh R. Mohite

Cover Work

Nilesh R. Mohite

About the Author

K. Siva Prasad Reddy is a Senior Software Engineer living in Hyderabad, India, and having more than seven years of experience in developing enterprise applications with Java and JavaEE technologies. Siva is a Sun Certified Java Programmer and has a lot of experience in server-side technologies such as Java, JavaEE, Spring, Hibernate, MyBatis, JSF, PrimeFaces, and WebServices (SOAP/REST). Siva is also the author of Java Persistence with MyBatis 3, Packt Publishing.

Siva normally shares the knowledge he has acquired on his blog at www.sivalabs.in. If you want to find out more about his work, you can follow him on Twitter (@sivalabs) and GitHub (https://github.com/sivaprasadreddy).

I would like to thank my wife Neha, for all the support she gave me at every step of the process; without her this wouldn't have been possible.

Also, thanks to my parents and sister for their moral support in completing this dream.

About the Reviewers

Thomas Andraschko was born in 1989 in Bavaria, Germany. He has over seven years of expertise in software development, wherein his first JSF experience was in 2008 with Apache MyFaces 1.2 and Apache MyFaces Trinidad.

Over the years, he has managed various projects, with other technologies as well, such as JSP, Servlets, Spring MVC, .NET WinForms, and even Ninject and ASP.NET.

Thomas is the founder of PrimeFaces Extensions, an Apache OpenWebBeans committer since 2012, and a PrimeFaces core developer and committer since 2013.

Thomas normally shares his knowledge on his blog at http://tandraschko.blogspot.de.

Thomas is currently employed at the Institut für Vorsorge und Finanzplanung GmbH as Software Architect.

This is the first time he has been involved in the publication of a book.

I would like to thank my friends Çağatay Çivici and Oleg Varaksin for their work on PrimeFaces and PrimeFaces Extensions, and their great teamwork in the last few years.

Also, a big thanks to all other PrimeFaces Extensions team members, the Apache MyFaces community, and the Apache OpenWebBeans community.

Mauricio Fenoglio is a Senior Software Engineer and Architect living in Montevideo, Uruguay. He is a Computer Engineering graduate who studied Informatics at The University of the Republic.

He has over five years of experience developing enterprise web applications, especially using JEE.

He is also an active contributor to the open source project PrimeFaces Extensions. He enjoys using JSF, PrimeFaces, and the most up-to-date web technologies.

When he isn't coding, he likes to hang out with his girlfriend and practice kitesurfing at the nearest beach.

This is the first time he has been involved in the publication of a book as a reviewer.

Sudheer Jonna was born in Andhra Pradesh, India, in 1987. Currently, he is working as a software developer in Chennai, India. He has completed his Master's degree in Computer Applications from JNTU University. His main occupation in the last three years has been building web applications based on Struts, JSF, Spring, jQuery, and JPA—all relating to the banking and financial securities domain.

He is an experienced JSF expert and has been working with the component library of PrimeFaces since 2011. He is also a well-known, recognized member of the PrimeFaces community and project member of the PrimeFaces Extensions project—additional JSF 2 components for PrimeFaces.

Besides these mentioned technologies, he writes technical articles, online training, book reviews (for Packt Publishing), provides suggestions through forums and blogs, and involves himself in optimizing performance in web applications during his free time. He is interested in the R&D of various popular J2EE frameworks and many other technologies.

Sudheer can be contacted on his Gmail account, <[email protected]>, where he also shares his knowledge.

Nilesh Namdeo Mali has completed his B.E. in Computer Engineering from Pune University. He has more than five years of experience in software development, especially in J2EE, JSF, and PrimeFaces. He is also committer to the PrimeFaces Extensions open source project.

I would like to thank my family for supporting me.

www.PacktPub.com

Support files, eBooks, discount offers and more

You might want to visit www.PacktPub.com for support files and downloads related to your book.

Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at <[email protected]> for more details.

At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.

http://PacktLib.PacktPub.com

Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. 

Why Subscribe?

Fully searchable across every book published by PacktCopy and paste, print and bookmark contentOn demand and accessible via web browser

Free Access for Packt account holders

If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access.

Preface

PrimeFaces is a leading open source user interface (UI) component library for Java Server Faces (JSF)-based web applications. PrimeFaces provides more than 100 UI components with a rich look and feel and theming support. At the time of writing this book, the latest version of PrimeFaces is 4.0, which works well with JSF 2.x.

PrimeFaces Beginner's Guide will take you through the process of installing, configuring, and using PrimeFaces in your JSF-based web applications. Usage of PrimeFaces components are explained through simple and practical examples with step-by-step instructions. By the end of the book, you will have a hands-on experience on how to use PrimeFaces effectively.

What this book covers

Chapter 1, Introduction to PrimeFaces, introduces PrimeFaces, along with details on how to install and configure it by creating a simple Hello World application. This chapter also provides an overview of various PrimeFaces features, such as AJAX support, Partial Page Rendering (PPR), and explains Poll and Remote Command components, which come in very handy at times.

Chapter 2, Introducing Sample Application TechBuzz, provides an overview of the sample application TechBuzz, which we are going to build incrementally throughout the book, and explains what PrimeFaces components will be used for various requirements. This chapter also provides an overall picture on setting up the development environment with all the required software, such as JDK, IDE, database, and so on.

Chapter 3, Using PrimeFaces Common Utility Components, introduces various commonly used utility components provided by PrimeFaces, such as Growl, Tooltip, BlockUI, NotificationBar, and so on. This chapter also explains new features introduced in PrimeFaces 4.0, such as Fragment, Sticky, Search Expression Framework, and Dialog Framework.

Chapter 4, Introducing PrimeFaces Client Side Validation Framework, covers PrimeFaces Client Side Validations (CSV) Framework in detail, explaining how to configure and enable client-side validations, integrating the CSV framework with the Bean Validation API, and extending the CSV framework to support custom JSF Validators/-Converters and custom Bean Validation Annotations.

Chapter 5, Introducing Text Input Components, covers various input text components such as InputText, InputTextArea, Password, InputMask, Inplace Editor, and Rich Text Editor. This chapter also explains the AutoComplete component in detail, explaining how to use it with POJO support, multiple item selection, and so on.

Chapter 6, Working with Selection Input Components, covers several PrimeFaces selection components, including SelectOneMenu, SelectManyMenu, SelectOneRadio, SelectManyCheckbox, PickList, MultiSelectListbox, and so on.

Chapter 7, Introducing Advanced Input Components, explains how to work with advanced input components, such as Calendar, Rating, Spinner, and Slider. Readers will also learn about uploading and downloading files using FileUpload and FileDownload components. This chapter also explains how to prevent span and bots using CAPTCHA validation.

Chapter 8, Working with Data Components, covers various data components, such as DataList, DataGrid, and explains DataTable features in depth, including pagination, sorting, filtering, row/cell editing, and so on. Readers will also learn about exporting DataTable data to Excel, PDF, and XML using the DataExporter component.

Chapter 9, Introducing Advanced Data Visualization Components, introduces advanced data visualization components, such as Carousel, TagCloud, and Schedule. Readers will also learn about displaying data in a Tree structure using Tree and TreeTable components.

Chapter 10, Working with Layout Components, explains how to create simple and complex layouts using Panel, PanelGrid, AccordionPanel, TabView, and Layout components, and describes various customization options. This chapter also covers creating workflow-style forms using the Wizard component and Portal-like Layouts using the Dashboard component.

Chapter 11, Introducing Navigation Components, explains creating different styles of menus using various navigation components, such as Menu, SlideMenu, TieredMenu, Menubar, MegaMenu, TabMenu, and Breadcrumb.

Chapter 12, Drawing Charts, covers how to visualize data in a graphical format using a variety of chart formats, such as Line, Bar, Pie, and Donut charts. This chapter also introduces how to render charts created by the JFreeChart API.

Chapter 13, Using PrimeFaces Themes, introduces how to use built-in themes provided by PrimeFaces and how to create custom themes using the online ThemeRoller tool. Readers will also learn about how to dynamically change themes using the ThemeSwitcher component. This chapter also gives a brief overview of various PrimeFaces sibling projects, such as PrimeFaces Extensions, PrimeUI, PrimeFaces Mobile, and PrimeFaces Push.

What you need for this book

You will need the following software to follow the examples:

Java JDK 1.5+ (http://www.oracle.com/technetwork/java/javase/downloads/index.html)PrimeFaces latest version (http://www.primefaces.org/downloads.html)MySQL (http://www.mysql.com/) or any other relational database that has a JDBC driverEclipse (http://www.eclipse.org) or any of your favorite Java IDEApache Maven build tool (http://maven.apache.org/)Browser tools: as we are going to learn a lot more about UI design using the PrimeFaces component, it would be really helpful to have tools such as the FireBug plugin for Firefox or Chrome Developer Tools for the Google Chrome browser

Who this book is for

This book is for you if you would like to learn and use PrimeFaces for your JSF-based applications, and if you are looking for a practical guide with an easy-to–follow, step-by-step approach along with plenty of examples. Prerequisites required for this book are basic JSF and jQuery skills.

Conventions

In this book, you will find several headings appearing frequently.

To give clear instructions of how to complete a procedure or task, we use:

Time for action – heading

Action 1Action 2Action 3

Instructions often need some extra explanation so that they make sense, so they are followed with:

What just happened?

This heading explains the working of tasks or instructions that you have just completed.

You will also find a number of styles of text that distinguish between different kinds of information. Here are some examples of these styles, and an explanation of their meaning.

Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: "We can disable AJAX submit by setting attribute ajax="false"."

A block of code is set as follows:

<h:panelGrid columns="2"> <p:outputLabel value="UserName"/> <p:inputText value="#{userController.userName}"/> <p:outputLabel value="Password"/> <p:password value="#{userController.password}"/> <p:commandButton action="#{userController.login}" value="Login" /> </h:panelGrid>

When we wish to draw your attention to a particular part of a code block, the relevant lines or items are set in bold:

<h:panelGrid columns="2"> <p:outputLabel value="UserName"/> <p:inputText value="#{userController.userName}"/> <p:outputLabel value="Password"/> <p:password value="#{userController.password}"/> <p:commandButton action="#{userController.login}" value="Login" /> </h:panelGrid>

Any command-line input or output is written as follows:

cd /ProgramData/Propeople rm -r Drush git clone --branch master http://git.drupal.org/project/drush.git

Newterms and importantwords are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "On the Select Destination Location screen, click on Next to accept the default destination."

Note

Warnings or important notes appear in a box like this.

Tip

Tips and tricks appear like this.

Reader feedback

Feedback from our readers is always welcome. Let us know what you think about this book—what you liked or may have disliked. Reader feedback is important for us to develop titles that you really get the most out of.

To send us general feedback, simply send an e-mail to <[email protected]>, and mention the book title through the subject of your message.

If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide on www.packtpub.com/authors.

Customer support

Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

Errata

Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books—maybe a mistake in the text or the code—we would be grateful if you would report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the erratasubmissionform link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website, or added to any list of existing errata, under the Errata section of that title.

Piracy

Piracy of copyright material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works, in any form, on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.

Please contact us at <[email protected]> with a link to the suspected pirated material.

We appreciate your help in protecting our authors, and our ability to bring you valuable content.

Questions

You can contact us at <[email protected]> if you are having a problem with any aspect of the book, and we will do our best to address it.

Chapter 1. Introduction to PrimeFaces

Java Server Faces (JSF) is a component-oriented framework for building web application using Java. JSF makes it easy to build web application by automating common tasks such as populating input parameter values into Java Beans by parsing input parameters, performing validations, and rendering or updating views. But building web application with rich User Interfaces (UI) still remains a challenge as Java developers may or may not be good at building fancy UI components using HTML, JavaScript directly. As JSF is a component-based framework, it allows building custom UI components with rich look and feel and can be reusable in any project.

The good news is that there are many open source and proprietary frameworks providing readymade reusable UI components, which can be used in any JSF-based applications. Among the several UI component libraries available today, PrimeFaces is an outstanding UI component library in terms of features and ease of use.

In this chapter, we will cover:

Introducing the features of PrimeFacesInstalling and configuring PrimeFacesCreating a HelloWorld application using PrimeFacesCreating simple forms using PrimeFacesPerforming form validationsPerforming client-side validationsUnderstanding partial page rendering (PPR)Updating view using AJAXUpdating view using AJAX listenersPerforming tasks periodically using the poll componentInvoking server-side methods from JavaScript using RemoteCommand

In this chapter, we will see what features make PrimeFaces an outstanding library, how to install and configure PrimeFaces and start using some of its basic components.

PrimeFaces is an open source JSF component library with 100+ rich UI components support. It has built-in AJAX support based on standard JSF 2.0 AJAX APIs.

Introducing the features of PrimeFaces

PrimeFaces provides the following set of features, which makes it powerful UI component library yet easy to use:

More than 100 rich UI componentsBuilt-in AJAX supportZero configurationsDoes not require any third-party library dependencies for most of the componentsIntegrated with ThemeRoller30+ readily available themesSupports IE8+, Chrome, Firefox, Safari, and Opera browsers

Installing and configuring PrimeFaces

The PrimeFaces library comes as a single jar file and doesn't have any mandatory third-party library dependencies. So to use PrimeFaces, it is sufficient to add PrimeFaces jar along with a JSF implementation library such as Oracle's Mojarra or Apache's MyFaces.

However, based on the PrimeFaces features that you want to use, you may need to add some third-party libraries. The following table describes library dependency needed for a particular feature:

Dependency

Type

Version

Description

JSF runtime

2.0, 2.1, or 2.2

Required

Apache MyFaces or Oracle Mojarra

itext

2.1.7

Optional

DataExporter (PDF)

apache poi

3.7

Optional

DataExporter (Excel)

rome

1.0

Optional

FeedReader

commons-fileupload

1.2.1

Optional

FileUpload

commons-io

1.4

Optional

FileUpload

The preceding table contains the third-party library versions, which are tested and known to be working fine with PrimeFaces-4.0 version. Other versions might also work fine but they are not officially tested.

Tip

If you are using a servlet container such as Apache Tomcat, then you need to add JSF implementation library dependencies such as Oracle's Mojarra or Apache MyFaces. If you are using any JavaEE application servers such as, JBoss AS, Glassfish, WebLogic, and so on; then there is no need to add JSF implementation libraries explicitly as they come in-built with application server. Some application servers may not have latest JSF implementation libraries. So, check whether your application server has JSF 2.0 or 2.1 or 2.2 implementation libraries or not. Consult your application server specific documentation to see how to override server libraries with latest versions.

In this book, we will be using PrimeFaces-4.0, which is latest version at the time of writing along with Oracle's Mojarra-2.2.3 JSF implementation.

Time for action – installing and configuring PrimeFaces

Perform the following steps to install and configure PrimeFaces to your web application:

Configure JSF FacesServlet in web.xml:
<servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.jsf</url-pattern> </servlet-mapping>

Tip

Downloading the example code

You can download the example code files for all Packt books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

If you are not using Maven, then you can download PrimeFaces-4.0.jar from http://www.primefaces.org/downloads.html and add it to classpath.If you are using Maven, then add PrimeFaces maven repository to the repository list, and add PrimeFaces-4.0 dependency in pom.xml.
<repository> <id>prime-repo</id> <name>Prime Repo</name> <url>http://repository.primefaces.org</url> </repository> <dependency> <groupId>org.primefaces</groupId> <artifactId>primefaces</artifactId> <version>4.0</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-api</artifactId> <version>2.2.3</version> </dependency> <dependency> <groupId>com.sun.faces</groupId> <artifactId>jsf-impl</artifactId> <version>2.2.3</version> </dependency>

What just happened?

We have configured PrimeFaces repository and primefaces-4.0.jardependency in our maven pom.xml, so that the PrimeFaces library is available to the web application classpath. We have configured FacesServlet and mapped to the URL pattern *.jsf, other popular URL patterns used are /faces/*, *.faces, and *.xhtml.

Tip

The PrimeFaces libraries from 4.0 version will be available in Maven Central, so you need not configure the PrimeFaces repository. But if you want to use snapshot versions, you need to configure the PrimeFaces repository in pom.xml.

Creating a HelloWorld application using PrimeFaces

In previous section, we have configured and installed PrimeFaces. To start using PrimeFaces components, all we need to do is adding the namespace xmlns:p=http://primefaces.org/ui to JSF pages.

Let us create a simple JSF page using PrimeFaces to see whether we have successfully installed and configured PrimeFaces.

Time for action – creating a HelloWorld program using PrimeFaces

Let us create a helloworld.xhtml file with the PrimeFaces namespace configuration, and use the PrimeFaces editor component to display a rich HTML editor. Perform the following steps:

To start using PrimeFaces components all we need to do is add the namespace xmlns:p=http://primefaces.org/ui in JSF facelets page.
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"> <f:view contentType="text/html"> <h:head> <title>First PrimeFaces Page</title> </h:head> <body> <h:form> <p:editor value="Hello World, PrimeFaces Rocks!!"/> </h:form> </body> </f:view> </html>
Run the application and point your browser to http://localhost:8080/chapter01/helloworld.jsf. We can see the rich text editor as follows:

What just happened?

We have tested PrimeFaces configuration by using PrimeFaces editor component <p:editor/>. As we have configured PrimeFaces properly, we are able to see rich HTML editor.

Make sure you have the <h:head> tag in your facelets page to avoid "PrimeFaces not found" JavaScript error.To make PrimeFaces work properly on webkit layout engine-based browsers such as, Chrome/Safari, enforce contentType to text/html using <f:view contentType="text/html">.

Creating simple forms using PrimeFaces

PrimeFaces provides various input elements such as inputText, password, inputTextarea, commandButton, commandLink, and so on, which are extensions to the standard JSF components providing additional features and theming support.

To get a feel of how to use PrimeFaces components, let us create a simple user registration form using PrimeFaces components.

Performing form validations

Validating user submitted data is very common and a crucial part of any web application. JSF itself provides support for UI component validation and PrimeFaces enhances it with additional features.

In the previous section, we have created a sample user registration form but we did not validate the form for any mandatory fields. Let us enhance the registration form with the validations for mandatory fields.

Assume we have the following validation rules for the registration form:

UserName should not be blank.Password should not be blank and should be at least four characters.FirstName should not be blank.

Time for action – validating the user registration form

We can use JSF validations for performing the earlier mentioned validations on the registration form. We can also perform validations using PrimeFaces AJAX-based validations by hooking up with JavaScript events, for this perform the following steps:

Update registrationWithVal.xhtml to build a user registration form along with validation support using the following code:
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:p="http://primefaces.org/ui"> <h:head> <title>Registration</title> </h:head> <body> <h:form id="registrationForm"> <p:panel header="Registration Form" style="width: 800px;"> <p:messages /> <h:panelGrid columns="3"> <p:outputLabel value="UserName:*"/> <p:inputText id="userName" value="#{userController.registrationUser.userName}" required="true" label="UserName" > <p:ajax event="keyup" update="userNameMsg"/> </p:inputText> <p:message id="userNameMsg" for="userName"/> <p:outputLabel value="Password:*"/> <p:password id="password" value="#{userController.registrationUser.password}" required="true" label="Password"> <f:validateLength minimum="4"/> <p:ajax update="passwordMsg" event="keyup"/> </p:password> <p:message id="passwordMsg" for="password"/> <p:outputLabel value="FirstName:*"/> <p:inputText id="firstName" value="#{userController.registrationUser.firstName}" required="true" label="FirstName"> </p:inputText> <p:message id="firstNameMsg" for="firstName"/> <p:outputLabel value="LastName:"/> <p:inputText id="lastName" value="#{userController.registrationUser.lastName}"/> <p:message id="lastNameMsg" for="lastName"/> <p:outputLabel value="Email:"/> <p:inputText id="email" value="#{userController.registrationUser.email}"/> <p:message id="emailMsg" for="email"/> <p:outputLabel value=""/> <p:commandButton action="#{userController.register}" value="Register" update="registrationForm"/> <p:outputLabel value=""/> </h:panelGrid> </p:panel> </h:form> </body> </html>
Run the application and point your browser to http://localhost:8080/chapter1/registrationWithVal.jsf. Leave UserName and FirstName blank and enter password with less than four characters and submit the form. Then Registration Form will be redisplayed with errors as shown in the following screenshot:

What just happened?

We have used input validations such as required, minimum length, and so on. We have used PrimeFaces AJAX-based validations using <p:ajax> on the keyup JavaScript event. So as you type in the password, it will get validated and update the validation message immediately. You can also use <p:ajax> with other JavaScript events such as keydown, keypress, mouseover, and so on.

Tip

Beware of registering AJAX event listeners for JavaScript events such as keyup, keydown, and so on; as it may increase processing overhead and degrade performance. So use this feature cautiously.

Performing client-side validations

PrimeFaces also supports hooking up client-side JavaScript event handlers (onclick, onblur, onchange, and so on) for many of its UI Components. We can use this feature to perform client-side validations and reduce the server validation roundtrips.

Time for action – client-side e-mail validation

In our Registration Form,