JavaServer Faces (JSF) is a server side component framework for building Java Technology web-based application. JavaServer Faces (JSF) consists of two main parts:
- The JavaServer Faces API for representing components, managing their states, handling events, handling server side validations, and achieving data conversion; defining pages navigation, supporting internationalization and accessibility and providing extensibility for all these features.
- Tag libraries for adding components to the web pages and connecting those components into their corresponding server side objects.
This post should clarify how a JSF framework could be installed and configured by using Eclipse IDE support.
This post will use the following tools for achieving the required installation and configuration:
- Eclipse Java EE IDE Kepler
- JSF 2.2.
Create JavaServer Faces Project
Eclipse IDE doesn’t provide a specific project for creating a JavaServer Faces (JSF), so we need to create a general dynamic project and adding a JavaServer Faces (JSF) facet into it. The facets define a characteristics and requirements for Java EE projects and are used as a part of the runtime configuration, so adding a JSF facet into created project will help us installing the required JSF libraries and allow us to adhere the required view for dealing with the different parts of JSF application development.
The steps required for creating and configuring a JavaServer Faces project using Eclipse IDE are as follow:
- Create a new dynamic project.
- Right-Click on the created project -> select properties.
- While in a properties window, select project facets.
- Make sure that the version of your Dynamic Web Module is at least 2.5.
- Tick at the Java and select the version of 1.6 (it depends on your JDK that you are using)
- Tick at the JavaServer Faces and select the version of 2.2
By end of the previous steps, you should be informed about further configuration is required. Hover over the link and click it.
Create JavaServer Faces Library
By clicking on the Further configuration required link, it’s the time for creating the JavaServer Faces library. To create library you have to follow the below steps:
- While a window of JSF Capabilities is opened, click on the download library.
- The JavaServer Faces library is being searched and you should at least see a JSF 2.2 (Mojarra 2.2.0) library. Mojarra is one of the implementations exist for JavaServer Faces (JSF 2).
- Tick the JSF 2.2 (Mojarra 2.2.0) library.
- Specify the library name that you would use it as a reference name for the installed JavaServer Faces library.
- Specify the Download destination that you would use a location for saving the binaries of the installed JavaServer Faces library.
You should get the same dialog as shown below
- Click the next button for complete the installation.
- Tick the i accept the terms of this license.
- The JavaServer Faces library is being installed.
- Once the installation has been finished, you should see the JSF 2.2 (Mojarra 2.2.0) library is listed as a choice in the JSF Implementation Library.
- Tick the JSF 2.2 (Mojarra 2.2.0) listed library.
- Click Okay to close the dialog.
- Click Okay to close the project facets dialog.
By ending the above steps, you are ready for using JavaServer Faces in your project.
Configure Apache Tomcat
If you’ve ever been configured the Apache Tomcat for executing a JSF samples, you’ve requested to install and configure it for next coming use. Follow the below steps for making Apache Tomcat for use.
- From the dynamic project and underneath of Target Runtime -> Select New Runtime
- Select Apache Tomcat v7.0 -> Click Next
- Type the name of the server that you’ve been installing.
- Navigate into the destination path of the Apache Tomcat 7.0 (Download the Apache Tomcat 7.0).
- Click on the server pane by Window -> show view -> Servers or Other -> Servers.
- You should find the following message (No Servers are available. Click this link to create a new server …)
- Choose Tomcat 7.0
- Let’s the remaining configuration without touch.
- Click next and finish.
- The server is installed and is ready for use
Create JavaServer Faces Page
After creating, configuring and installing the all libraries that required to start using of JavaServer Faces, it’s time to create the first Hello World sample.
- Right-Click the project’s WebContent.
- Select New -> HTML File.
- Name your file with an index.xhtml -> Click Next
- Select New XHTML File (1.0 strict) -> Click Finish
The page that you will create it must be as below
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets" xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"> <h:outputText value="Hello World !"></h:outputText> </html>
To run the index.xhtml you should follow the below steps:
- Righ-Click on the index.xhtml
- Select Run As
- Select the configured Apache Server that you’ve done before.
- Click finish.
- You should see the message Hello World !