A step by step tutorial on how to create a new JSF 2.2 (Mojarra 2.2.2) maven project, add PrimeFaces v3.5 library to it, add Apache Tomcat application server to Eclipse and run the project on it.

Contents:
1. Creating and Configuring
2. Solving generated JSF 2.2 faces-config XML Problem
3. Adding JSF and PrimeFaces <dependencies> to pom.xml
4. Solving PrimeFaces 3.5 “missing FileUploadRenderer” issue with JSF 2.2
5. Configuring web.xml
6. Adding Apache Tomcat to Eclipse and running the example
7. Download the project
8. Importing and Building Maven Project

1. Creating and Configuring

At the time of writing this, Eclipse Kepler was the latest version, I’ll be using it. Select New > Maven Project. Check “Create a simple project (skip archetype selection)” and click “Next”.

Eclipse New Maven Project 1

In the next step fill out your project information, and if you intend to use this project as an independent running one, set Packaging to war. Click “Finish” when done.

Eclipse New Maven Project 2

Your project is now created, time for configurations, let’s set the project to use Java 1.7, so we can use Servlets 3.0; Right click > Properties > Java Compiler and set “Compiler compliance level” to 1.7.

Eclipse Project Java Compiler

From the menu on the left select Project Facets, check the next:
- Dynamic Web Module version 3.0
- Java version 1.7
- JavaServer Faces version 2.2
Click “Ok” when done, eclipse will then rebuild your project.

Eclipse Project Facets JSF

Note: If you didn’t find 1.7 as one of the drop down options in the previous preferences, then you have to add it to eclipse first.
Navigate to eclipse Preferences > Java > Installed JREs, click Add, and locate your installed Java path.

Adding Java in Eclipse

2. Solving generated JSF 2.2 faces-config XML Problem

For me the generated faces-config.xml file was showing an XML Problem in the “Markers” view tab:

Referenced file contains errors
(jar:file:/Applications/eclipse/plugins/org.jboss.tools.jst.web_3.5.0.
Final-v20130717-0309-B75.jar!/catalog/web-facesconfig_2_2.xsd).  For
more information, right click on the message in the Problems View and
select "Show Details..."

The problem that xsi:schemaLocation has a duplicate namespace http://xmlns.jcp.org/xml/ns/javaee, remove it (as highlighted in the snapshot) and all should be fine.

Eclipse JSF 2.2 faces-config XML Problem

3. Adding JSF and PrimeFaces <dependencies> to pom.xml

Open pom.xml, and add the next:

JSF <dependencies>

<dependencies>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-api</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>com.sun.faces</groupId>
        <artifactId>jsf-impl</artifactId>
        <version>2.2.2</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>3.0.1</version>
    </dependency>
</dependencies>

PrimeFace <repository> and <dependency>:

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

<dependencies>
    <dependency>
        <groupId>org.primefaces</groupId>
        <artifactId>primefaces</artifactId>
        <version>3.5</version>
    </dependency>
</dependencies>

When you set the project to using “Dynamic Web Module 3.0″ you might run into an error: “Dynamic Web Module 3.0 requires Java 1.6 or newer”, follow this solution to get it worked out.

Also if you want to use PrimeFaces themes you should include the desired one’s dependency in your pom.xml, for this example I’ll be using bootstrap theme.

<dependency>
    <groupId>org.primefaces.themes</groupId>
    <artifactId>bootstrap</artifactId>
    <version>1.0.9</version>
</dependency>

4. Solving PrimeFaces 3.5 “missing FileUploadRenderer” issue with JSF 2.2

PrimeFaces 3.5 with Mojarra 2.2.x requires a dependency at runtime, and throws this exception if not found.

SEVERE: Critical error during deployment:
com.sun.faces.config.ConfigurationException:
  Source Document: jar:file:/workspace/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/jsflab/WEB-INF/lib/primefaces-3.5.jar!/META-INF/faces-config.xml
  Cause: Class 'org.primefaces.component.fileupload.FileUploadRenderer' is missing a runtime dependency: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItem
...
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItem
...
Caused by: java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItem
...
Caused by: java.lang.ClassNotFoundException: org.apache.commons.fileupload.FileItem
...

adding commons-fileupload dependency to your pom.xml will solve it:

<dependency>
    <groupId>commons-fileupload</groupId>
    <artifactId>commons-fileupload</artifactId>
    <version>1.3</version>
</dependency>

This dependency will download commons-io v2.2 as well.

5. Configuring web.xml

Consider the next configurations:

<context-param>
    <param-name>javax.faces.DEFAULT_SUFFIX</param-name>
    <param-value>.xhtml</param-value>
</context-param>
<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>

The <servlet> and <servlet-mapping> tags ensure mapping our .jsf pages to javax.faces.webapp.FacesServlet
and how these pages are referred to, from http perspective.
The DEFAULT_SUFFIX <context-param> indicates what is the extension of our view files.

It’s helpful to set the PROJECT_STAGE to Development so you can see warnings or errors on the view.

<context-param>
    <param-name>javax.faces.PROJECT_STAGE</param-name>
    <param-value>Development</param-value>
</context-param>

And lastly to use the bootstrap PrimeFaces theme we’ve already included its dependency.

<context-param>
    <param-name>primefaces.THEME</param-name>
    <param-value>bootstrap</param-value>
</context-param> 

This will do the initial configurations needed for web.xml.

6. Adding Apache Tomcat to Eclipse and running the example

Feel free to skip this step is you know it. If not, download Apache Tomcat 7.0.x, and extract the folder. In eclipse select New > Server, select “Apache” > “Tomcat v7.0 Server”, click “Add…” and locate the extracted Tomcat folder.
Click “Finish” when done.

Add Apache Tomcat to Eclipse

In Eclipse “Servers” tab, right click your server > Add and Remove…
Your project should be in the “Available” list, select, and “Add >” it to “Configured” list. “Finish” when done.

Add Project to Apache Tomcat

Right click your server > Start will starts up your server.
Requesting http://localhost:8080/jsflab/ (change jsflab to your project’s name) in your web browser will get a small JSF and PrimeFaces testing page running.

JSFLab Snapshot

7. Download the project

Download the complete example jsflab.zip, extract it in your workspace, import it, and it should run fine.

8. Importing and Building Maven Project

If you’re just downloading the example project and you want to import it in eclipse for testing, here’s what you need to do.
Go to File > Import…, from the tree of import sources, select Maven > Existing Maven Project. In “Import Maven Projects” dialog, click Browse… next to “Root Directory” and select project’s folder.
When importing is finished, make sure you add the project to your server.
If it didn’t run fine, do a “Maven Build” by going to Run > Run Configurations…, select “Maven Build” from the left menu, create “New”. In the new configurations > “Main” tab, Browse Workspace… and select your project. Set Goals to “clean install -e” and click Run.

JSFLab Snapshot

When build is done, right click your project in “Project Explorer” view > Refresh.
Right click your server > Clean…
All is ready now to run, comment out and let me know if anything goes wrong.

This tutorial was tested using: JSF Mojarra v2.2.2, PrimeFaces v3.5, Maven v3.1, Apache Tomcat Server 7.0.42 and Eclipse Kepler on Mac OSX Lion.