# 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:
* <f:view>: used to create top level view and is a container for all JSF component tags on a page
* <h:form>: used to create the HTML form
* <h:panelGrid>: used to create an HTML table with specified number of columns
* <h:outputText>: used to create a component for displaying formatted output as text
* <h:inputText>: used to create text input control (single line)
* <h:inputSecret>: used to create password fields
* <f:validator>: 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 <h:messages> or <h:message for="inputTextID"> tags.
Form validation can be achieved in many ways:

###### by using the <f:validator> 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 <f:validator> inside an <f:inputText> 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.

###### by using the in-built validator tags
#### 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 <jsp:useBean> 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!