Web technologies/2014-2015/Laboratory 9

Java Server Faces
Java Server Faces (JSFs) is a Web application framework intended to simplify development integration of user interfaces and the operation on the values the UI hold for Java EE application usage activity.

The current version is 2.3.2 as of August 20, 2019.

It includes:


 * Java Web APIs for:
 * user interface components and managing their state
 * handling events (an example is javax.faces.event.ActionEvent)
 * input validation
 * defining page navigation
 * A default set of UI components
 * Two custom tag libraries for interfacing with JSP
 * Managed beans

IMPORTANT: JSF cannot handle Get requests

Creating a simple JSF
A typical JSF adheres to the following control flow:


 * display form
 * submit form to itself
 * instantiate bean
 * action controller method is invoked
 * the method returns a condition
 * a result page is displayed

NetBeans offers the possibility of creating a JSF projects: go to New Project -> Web -> Web Application -> select Java Server Faces from the Frameworks tab -> Finish.

Creating the form
JSF offers several tags for handling content:


 * : used to create top level view and is a container for all JSF component tags on a page
 * : used to create the HTML form
 * : used to create an HTML table with specified number of columns
 * : used to create a component for displaying formatted output as text
 * : used to create text input control (single line)
 * : used to create password fields
 * : used for validating HTML fields

The following example (login.jsp) shows a simple login form:

Form validation
IMPORTANT: In order to display the validation messages use either  or  tags.

Form validation can be achieved in many ways:

by using the  tag together with the custom Validator interface
The following steps are required:


 * Create a class implementing Validator:


 * Modify faces-config.xml to register the custom validator:


 * use the  inside an  tag to validate your input:

by using a Backing bean
The following steps are required:


 * create a method for validation inside the bean as follows:


 * set the validator attribute associated with the inputText tag requiring validation to call the validation method:

IMPORTANT: Sometimes the error messages do not get displayed. Instead in your Tomcat log you get the following info message: INFO: WARNING: FacesMessage(s) have been enqueued, but may not have been displayed.. The problem could be related to certain platform versions.

Creating the Java bean
IMPORTANT: when using several validation you can put them in different beans each extending the Validator class.

Adding navigation rules
Navigation are used for redirecting users after their form is processed. For example they can be redirected to an error page in case their login failed, or to a success page otherwise.

The two pages could look as follows:


 * the success.jsp page:


 * the failed.jsp page:

Editing the faces-config.xml file
Every JSF has a faces-config.xml file attached. users usually will have to modify this file to match their bean and navigation rules. A typical file could look as following:

Using the bean in another JSP
After the JSF has taken care of the form by validating the data and populating the bean one could also require to reuse the bean data in a different JSP. You can accomplish that by using the  tag for importing a bean:

Links:


 * JSF tutorial
 * Short JSF tutorial
 * JSF Examples
 * JSF Tutorial New

Exercises

 * The same exercise as in Laboratory 7 but with JSFs instead of servlets.
 * Validate fields using JSF and not Javascript!