Table of Contents
Introduction
The JAVA Server Faces framework, like all other frameworks in JAVA was developed using the JAVA Community Process to help build and manage web-based applications and user interfaces. As the name suggests, this framework puts great precision in creating applications that have a visually attractive appearance. The easier to navigate an interface, the more users tend to download such an application.
What is the JAVA Server Faces?
JAVA Server Faces uses the tried and tested MVC structure to give user interfaces of applications a robust platform to stand on. The framework facilitates the usage of reusable UI (user interface) components. The framework provides developers with tools in the form of UI components that support the program’s API (Application Programming Interface). If you want to create an application that has to be catchy to the eye yet function just as good, this is the framework to go with.
Advantages
One of the major advantages of using this framework is that you can reuse UI components. As the architecture of JSF is so well-knitted, dataflow between the various UI components takes place smoothly. It also becomes easy to keep track of requests from multiple servers.
The JAVA Server Faces Architecture
The JAVA Server Faces Architecture implements the MVC Architecture that comprises of the Model, View, and Controller components. This framework mainly deals with the creation of server-side components. Each component of the MVC is responsible for a particular action:
- Model – This is the first point of contact with clients. The model component deals with data handling and dictates the dataflow of the entire architecture.
- View – View is responsible for developing the user interface of the application. Once the response has been generated, the controller sends the information to the View component that organizes the data and forms a presentable output.
- Controller – The Controller is the main brain of the application that receives a request from the Model and finally turns into a response and sends it to View.
The JAVA Server Faces Architecture takes into consideration abstraction such that web designers and code developers can work on separate components without hindering each other’s work. While designers are only concerned with View, developers mainly work on the controller. Clients only have to interact with the Model component without having to know the complexity in developing a response to the request.
Various Parts of the JAVA Server Faces Architecture
- The Models are represented as JAVA Beans components. Each part of the model contains techniques that solve particular types of problems.
- A custom tag library – It deals with event handlers and validators.
- A custom lag library – Manages user interface components.
- Stateful Objects – These also deal with the interface of the application.
- Server-side Helper Classes
- Validators, event handlers and navigator handles.
- Faces Servlet, JSP/JSF Components, and Faces-config.xml are all part of the controller.
If you want to render a single part or component of the architecture, the best way to do it is by using the JSF. The abstraction involved in the following process allows web designers to develop a website anyhow they want it irrespective of how the controller has been configured.
Example and Code
//This is a simple application that converts temperature readings from Celsius to Fahrenheit. public class Sampletemp{ private double cel; private double far; private boolean flag = true; public double setCel(double cel) { this.cel = cel; } public double serFar(double far) { this.far = far; } public void Covert () { flag = false; far = (cel*9/5)+32; System.out.println (“Temperature in Fahrenheit is:” + far); } } //Below is the html file to the following application <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> <%@ taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@ taglib prefix="h" uri="http://java.sun.com/jsf/html"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> <title>Celsius to Fahrenheit Convertor</title> </head> <body> <f:view> <h:form> <h:panelGrid columns="2"> <h:outputLabel value="Celsius"> </h:outputLabel> <h:inputText value="# {temperatureConvertor.celsius}"></h:inputText> </h:panelGrid> <h:commandButton action="# {temperatureConvertor.celsiusToFahrenheit}" value="Calculate"></h:commandButton> <h:commandButton action="# {temperatureConvertor.reset}" value="Reset"> </h:commandButton> <h:messages layout="table"></h:messages> </h:form> <h:panelGroup rendered="# {temperatureConvertor.initial!=true}"> <h3> Result </h3> <h:outputLabel value="Fahrenheit "> </h:outputLabel> <h:outputLabel value="# {temperatureConvertor.fahrenheit}"></h:outputLabel> </h:panelGroup> </f:view> </body> </html>
Similar to most of the popular Web app frameworks, JSF implements an MVC design pattern. But How MVC works in JSF? I am still unclear on how practical implementation works here.