Setting up and configuring the PrimeFaces library
PrimeFaces is a lightweight JSF component library with one JAR file, which needs no configuration and does not contain any required external dependencies. To start with the development of the library, all we need is the artifact for the library.
Getting ready
You can download the PrimeFaces library from http://primefaces.org/downloads.html, and you need to add the primefaces-{version}.jar
file to your classpath. After that, all you need to do is import the namespace of the library that is necessary to add the PrimeFaces components to your pages to get started.
If you are using Maven (for more information on installing Maven, please visit http://maven.apache.org/guides/getting-started/maven-in-five-minutes.html), you can retrieve the PrimeFaces library by defining the Maven repository in your Project Object Model XML file, pom.xml
, as follows:
<repository> <id>prime-repo</id> <name>PrimeFaces Maven Repository</name> <url>http://repository.primefaces.org</url> </repository>
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.
Add the dependency configuration as follows:
<dependency> <groupId>org.primefaces</groupId> <artifactId>primefaces</artifactId> <version>5.2</version> </dependency>
At the time of writing this book, the latest and most stable version of PrimeFaces was 5.2. To check whether this is the latest available version or not, please visit http://primefaces.org/downloads.html. The code in this book will work properly with PrimeFaces 5.2. In prior versions or the future versions, some methods, attributes, or components' behaviors may change.
How to do it…
In order to use PrimeFaces components, first we need to add the namespace declaration to our pages. The namespace for PrimeFaces components is as follows:
xmlns:p="http://primefaces.org/ui"
That is all there is to it. Note that the p
prefix is just a symbolic link, and any other character can be used to define the PrimeFaces components. Now you can create your first XHTML page with a PrimeFaces component, as shown in the following code snippet:
<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:p="http://primefaces.org/ui"> <f:view contentType="text/html"> <h:head /> <h:body> <h:form> <p:spinner /> </h:form> </h:body> </f:view> </html>
This will render a spinner component with an empty value, as shown in the following screenshot:
A link to the working example for the given page is given at the end of this recipe.
How it works…
When the page is requested, the p:spinner
component is rendered with the SpinnerRenderer
class implemented by the PrimeFaces library. Since the spinner component is an input component, the request-processing life cycle will get executed when the user inputs data and performs a post back on the page.
Note
For the first page, we also needed to provide the contentType
parameter for f:view
since WebKit-based browsers, such as Google Chrome and Safari, request for the content type application/xhtml+xml
by default. This would overcome unexpected layout and styling issues that might occur.
There's more…
PrimeFaces only requires a Java 5+ runtime and a JSF 2.x implementation as mandatory dependencies. There are some optional libraries for certain features. All of these are listed in this table:
Dependency |
Version |
Type |
Description |
---|---|---|---|
JSF runtime |
2.0, 2.1, or 2.2 |
Required |
Apache MyFaces or Oracle Mojarra |
itext |
2.1.7 |
Optional |
|
apache-poi |
3.7 |
Optional |
|
rome |
1.0 |
Optional |
|
commons-fileupload |
1.3 |
Optional |
|
commons-io |
2.2 |
Optional |
|
atmosphere |
2.2.2 |
Optional |
PrimeFaces Push |
barcode4j-light |
2.1 |
Optional |
Barcode Generation |
qrgen |
1.4 |
Optional |
QR code support for barcode |
hazelcast |
2.6.5+ |
Optional |
Integration of the |
ehcache |
2.7.4+ |
Optional |
Integration of the |
Note
Please ensure that you have only one JAR file of PrimeFaces or a specific PrimeFaces theme in your classpath in order to avoid any issues regarding resource rendering.
Currently, PrimeFaces fully supports nonlegacy web browsers with Internet Explorer 10, Safari, Firefox, Chrome, and Opera.
The PrimeFaces Cookbook Showcase application
This recipe is available in the demo web application on GitHub (https://github.com/ova2/primefaces-cookbook/tree/second-edition). Clone the project if you have not done it yet, explore the project structure, and build and deploy the WAR file on application servers compatible with Servlet 3.x, such as JBoss WildFly and Apache TomEE.
The showcase for the recipe is available under http://localhost:8080/pf-cookbook/views/chapter1/yourFirstPage.jsf
.