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

Preface

PrimeFaces Cookbook is the first PrimeFaces book that provides a head start to its readers by covering all the knowledge needed for working with the PrimeFaces components in the real world. It's a quick, practical guide to learn PrimeFaces—the rapidly evolving, leading JSF component suite. It is written in a clear, comprehensible style and addresses a wide audience interested in modern, trend-setting Java or JEE web development.

What this book covers

Chapter 1, Getting Started with PrimeFaces, provides details on the setup and configuration of PrimeFaces along with the basics of the PrimeFaces AJAX mechanism and the integration with other JSF component libraries. The goal of this chapter is to provide a sneak preview on some of the features of PrimeFaces, such as the AJAX processing mechanism and resource handling with internationalization and localization, along with the necessary steps to go through for implementing a simple web application powered by PrimeFaces, which will give a head start to the user.

Chapter 2, Theming Concept, introduces the PrimeFaces themes and the concepts involved. Readers will learn about the theming of PrimeFaces components. The difference between structural and skinning CSS, installing and customizing of PrimeFaces themes, along with creating new themes, will be detailed. Readers will also see how to adjust font family and font size throughout PrimeFaces components to provide a consistent look and feel. Discussion about two variants of the theme switcher—stateless versus stateful—finish this chapter.

Chapter 3, Enhanced Inputs and Selects, explains how to work with the input and select components available in PrimeFaces. Such components are the main parts of every web application. PrimeFaces provides nearly 25 components for data input, which extend standard corresponding JSF components with skinning capabilities and useful features such as user-friendly interfaces, AJAX interactions, and validation.

Chapter 4, Grouping Content with Panels, covers various container components such as Panel, Accordion, ScrollPanel, and TabView, which allow grouping of the JSF components. Various settings to configure panel components are detailed in this chapter.

Chapter 5, Data Iteration Components, covers the basic and advanced features to visualize data with data iteration components provided by PrimeFaces. Such data iteration components are, for instance, DataTable, Tree, and TreeTable. The discussed features include sorting, pagination, filtering, lazy loading, and single and multiple selections.

Chapter 6, Endless Menu Variations, explains about several menu variations. PrimeFaces' menus fulfill all major requirements. They come with various facets—static, dynamic, tiered, iPod-styled, and so on—and leave nothing to be desired. Readers will face a lot of recipes that discuss the menus' structure, configuration options, customizations, and integration with other components. At the end of this chapter, readers will know what kind of menu to choose and how to put it on a page for this or that use case.

Chapter 7, Working with Files and Images, provides ways of managing operations on files such as uploading and downloading, and image operations such as capturing, cropping, and comparing. Readers will learn basic as well as advanced configuration of components and use cases.

Chapter 8, Drag Me, Drop Me, explains how the drag-and-drop utilities in PrimeFaces allow you to create draggable and droppable user interfaces efficiently. They abstract developers from dealing with implementation details on the browser level. In this chapter, readers will learn about PrimeFaces' drag-and-drop utilities—Draggable and Droppable. AJAX-enhanced drag-and-drop and a special integration with data iteration components will be explained as well.

Chapter 9, Creating Charts and Maps, covers the ways to create visual charts with PrimeFaces' extensive charting features, and create maps based on Google Maps. PrimeFaces offers basic and advanced charting with its easy-to-use and user-friendly charting infrastructure. Throughout the chapter, mapping abilities such as drawing polylines, polygons, handling markers, and events are also covered.

Chapter 10, Miscellaneous, Advanced Use Cases, introduces more interesting features of the PrimeFaces library. You will learn about RequestContext—a helpful utility that allows marking components as updatable targets at runtime, adding AJAX callback parameters, and more. In this chapter, a number of real-world samples will also be developed—blocking UI during AJAX calls, periodic polling, focus handling, controlling form submission, and targetable messages, to name a few. Furthermore, after reading this chapter, readers will be aware of pitfalls of menus within layout units and nested panels.

Chapter 11, Writing Custom Components, explains the complete process of building reusable PrimeFaces components. First of all, readers will learn the component's structure and constituent parts. After that, a custom Layout component as an alternative implementation to the PrimeFaces one, will be developed. At the end of this chapter, readers will be equipped with necessary knowledge to be able to create different custom components for the next few JSF or web applications.

You can download this chapter from http://www.packtpub.com/sites/default/files/downloads/9281OS_Chapter_11_Writing_Custom_Components.

Chapter 12, PrimeFaces Extensions in Action, introduces PrimeFaces Extensions, a community-driven, open source project. The aim of this chapter is the presentation of some additional components, such as InputNumber, CodeMirror, DynaForm, and Timeline. This young project can spark interest in developing rich web applications.

You can download this chapter from http://www.packtpub.com/sites/default/files/downloads/9281OS_Chapter_12_PrimeFaces_Extensions_in_Action.

What you need for this book

PrimeFaces only requires Java 5+ runtime. With the help of the Maven tool, you can easily get the artifact for PrimeFaces library (for more information on installing Maven, visit http://maven.apache.org). Please note that Maven demands readers Java Development Kit is installed on your local environment instead of the Java Runtime Environment.

To get the Cookbook showcase up and running, you need to pull the source code with a Git client. The details on how to run the showcase web application in your local environment can be found on GitHub (https://github.com/ova2/primefaces-cookbook).

Who this book is for

This book is for everybody who would like to learn modern Java web development based on PrimeFaces and is looking for a quick introduction to this matter. Prerequisites for this book are basic JSF, jQuery, and CSS skills.

Conventions

In this book, you will 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 are shown as follows: "For the first page, we also needed to provide the contentType parameter for f:view."

A block of code is set as follows:

<repository>
  <id>prime-repo</id>
  <name>PrimeFaces Maven Repository</name>
  <url>http://repository.primefaces.org</url>
</repository>

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

<p:contextMenu for="..." nodeType="...">
    <p:menuitem value="..." update="..."
        icon="..." oncomple="..."/>
</p:contextMenu>

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

-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

New terms and important words are shown in bold. Words that you see on the screen, in menus or dialog boxes for example, appear in the text like this: "The add-on can be opened after its installation by navigating to Tools | Web Developer | Firebug."

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 , and mention the book title via the subject of your message.

If there is a book that you need and would like to see us publish, please send us a note in the SUGGEST A TITLE form on www.packtpub.com or e-mail .

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/support, selecting your book, clicking on the errata submission form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded on our website, or added to any list of existing errata, under the Errata section of that title. Any existing errata can be viewed by selecting your title from http://www.packtpub.com/support.

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 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 if you are having a problem with any aspect of the book, and we will do our best to address it.