Java Server Faces

1. INTRODUCTION

Java Server Faces (JSF) is a Java-based framework for creating web-based user interfaces. It combines the power of Struts and Swing and follows Model-View-Controller (MVC) architecture which is considered to be the best for web frameworks. In an MVC framework, the controller decides which view to show next for a user request. JSF provides a powerful controller servlet for managing the life cycle of web-applications as well as provides a rich component model complete with event handling and component rendering. JSF uses lightweight POJO approach and is hence well-designed and easy-to-use, clean and simple. In a nutshell, using JSF has the following benefits:

  • Allows us to create user interfaces using a set of standard, reusable server-side components accessible through tags
  • It saves state information of forms components transparently and repopulates forms when they are re-displayed
  • Allows us to create custom components
  • Encapsulates event handling and component rendering

In this chapter we shall discuss primary concepts with extensive examples starting with a simple example JSF application.

2. FIRST APPLICATION

It is good to understand a technology with an application. In our first application, we shall develop a simple JSF application having a page that displays a message “Hello world”. In practice, a JSF application typically consists of several pages often backed by Java beans. This application merely helps us in understanding the philosophy of a JSF application.

2.1. Installing JSF

We shall set up a basic environment that will facilitate the development, deploy and testing of JSF applications. Professionals often use IDE such as NetBeans, Eclipse, Rational Application Developer etc. However, it is important to know what and how things happen behind the scene to get out of situations if anything goes wrong. Our environment requires

  • Java Development Kid (JDK), version 1.5 or higher installed,
  • a web server (Tomcat 8) and
  • the Mojarra JSF libraries from Oracle.
  • A text editor notepad or wordpad
  • The command prompt to compile Java programs.
  • A web browser to test your JSF applications

Truly speaking, JSF is just a specification. To develop JSF-based web applications, we must obtain and install a reference implementation of this specification. There are many such implementations and any one can be used. The obvious choice is the JSF reference implementation by Oracle. It was developed by the well-known Mojarra project and is freely downloadable from https://javaserverfaces.java.net/. This link will guide you to a download page with many download options. The entire library is now included in a single JAR file. Download the most recent version of the JAR file. We downloaded the version javax.faces-2.2.8-04.jar. (Release on Dec 09 2014) from https://maven.java.net/content/repositories/releases/org/glassflsh/ javax.faces/2.2.8-04/.

There are many ways to make this library available to web applications running under Tomcat. One way to make it available to a specific application is to place this JAR file into the web application’s WEB-INF\lib directory. Note that in this case, only this specific web application will have access to that library. If another application needs access to this library, put the JAR file in its own WEB-INF\lib directory. Alternatively, we can put the JAR files into a common location so that all web applications can access it. For Tomcat, this location is <TOMCAT_HOME>\lib. In this case, all applications running under Tomcat will have access to library. Sometimes, Tomcat requires it to be restarted so that it can load the new JAR files. We put it in our application’s directory. WEB-INF\lib

JSF applications are simply standard web applications. So, every JSF application has an application root. Create a directory somewhere in your file system. We created a directory jsf under E:\ajp. So E:\ajp\jsf (hereafter referred to as jsf_home) will act as the root of our JSF application. Every web application root should have a structure. So, we create the following directory structure.

The Java code that gets compiled and distributed with your JSF application will go in a ciasses\ directory. Although, we shall not use any Java code in our first example, it’s worthwhile creating this folder for further examples.

The iib\ folder contains resources (such as JAR, xml files) that a web application might need at runtime. Our JSF application needs the JSF library file javax.faces-2.2.8-04.jar, which is not a part of Tomcat. So we put it in lib/ folder.

Every Java-based web application must have a special XML file known as a deployment descriptor, whose name is always web.xmi and resides in the web-inf\ folder. This extremely important file provides information to the servlet engine such as Tomcat about how to configure the environment in which the application will run.

JSF is not a part of servlet and JSP specification, a servlet engine like Tomcat cannot handle JSF requests. So, let us first understand how to make an ordinary servlet engine JSF-compliant. Since, Tomcat itself cannot handle JSF centric requests, it only identifies JSF requests (having a specific pattern) and routes them to a special servlet capable of handling them. So, we must tell the Tomcat which requests are JSF requests and who it should route them to. To distinguish JSF requests from others, a common extension is used rather than having extensions such as .jsp file or. html. JSF 2.0 favors the use of extension .xhtml for JSF files and we also use it. The servlet class that understands JSF requests is javax.faces.webapp.FacesServlet which is incorporated in javax. faces-2.2.8-04.jar.

The entire information is supplied to the Tomcat using application’s web.xml file by adding the following lines between, <web-app> and </web-app> tags.

<web-app

<servlet>

<servlet-name>Faces Servlet</servlet-name>

<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>Faces Servlet</servlet-name>

<url-pattern>*.xhtml</url-pattern>

</servlet-mapping>

</web-app>

Note that value of <servlet-name> tag in the <servlet> and <servlet-mapping> must match. The above codes essentially tell servlet engine that any request that comes in with an extension .xhtml should be forwarded to the “Faces Servlet”, which is implemented through the Java class named javax.faces.webapp.FacesServlet. The class file is included in javax.faces-2.2.8-04.jar file placed in application’s lib/ directory.

2.2. Writing a JSF Page

The basic configuration is over. It is time to write a simple JSF page. A JSF page, in addition to the html tags, contains namespace qualified special tags, called JSF tags. For these tags we need to use the following namespaces of URI in html node.

<html xmlns=”http://www.w3.org/1999/xhtml”

xmlns:h=”http://java.sun.com/jsf/html” >

The following JSF page (HelloWorld.xhtml) contains only one JSF tag <h:outputText> that renders a text specified in its value attribute.

<?xml version=”1.0″ encoding=”UTF-8″?>

<JDOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”

“http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”

xmlns:h=”http://java.sun.com/jsf/html” >

<head>

<title>Simple JSF page</title>

</head>

<body>

<h:outputText value=”Hello World!” />

</body>

</html>

This is a very rudimentary JSF page that introduces a few important concepts and helps us to verify that indeed, a basic JSF application is working in our local environment.

Create this file using a standard text editor, such as Microsoft’s Notepad, and save the file as HeiioWorid.xhtml in the application root folder, which for our case is jsf\. Note that the extension to this file should be .xhtml. The application is now ready to deploy and test.

2.3. Deploying the Application

The final directory structure is shown here. A summary of the entire directory structure is shown in Table 28.1:

There are many ways to deploy the application. One way to deploy is to place the entire jsf/ directory in the Tomcat’s webapps folder. We can also create a WAR (Web Application aRchive) file and place it in the Tomcat’s webapps directory.

A WAR file has a standard format for distributing and deploying Java EE, web-based applications. The jar utility that comes with the JDK can be used to create deployable WAR files.

To create a WAR file, open a terminal, go the jsf/ directory and use the following command:

jar cvf jsf.war *

We assume that the Java installation home directory is included in your PATH environment variable. If everything goes fine, an output as shown will be generated:

added manifest

adding: HelloWorld.xhtml(in = 385) (out= 257)(deflated 33%)

adding: WEB-INF/(in = 0) (out= 0)(stored 0%)

adding: WEB-INF/classes/(in = 0) (out= 0)(stored 0%)

adding: WEB-INF/lib/(in = 0) (out= 0)(stored 0%)

adding: WEB-INF/lib/javax.faces-2.2.8-04.jar(in = 2572391) (out= 2349858)(deflated 8%)

adding: WEB-INF/web.xml(in = 748) (out= 344)(deflated 54%)

The command creates a WAR file jsf.war which may be found in jsf\ directory. This jsf.war file contains all the necessary files needed for the HelloWorld JSF application. Put it in the Tomcat’s webapps directory. Don’t forget to restart the Tomcat server. If everything goes fine, a sample screen will appear as follows. The message for our JSF application has been highlighted.

2.4. Testing the Application

It is time to open up a web browser and navigate to our JSF application. Use the following URL to open the JSF page:

http://127.0.0.1:8080/jsf/HelloWorld.xhtml

We are assuming that the Tomcat server is running on port 8080. See the conf\server.xml file under Tomcat’s installation home directory to find the port it is running on. A sample screen shot is shown below:

Source: Uttam Kumar Roy (2015), Advanced Java programming, Oxford University Press.

Leave a Reply

Your email address will not be published. Required fields are marked *