Friday, August 3, 2007

Exploring JSF

JSF is the latest addition to the list of J2EE frameworks. Lets try to get an insight into what JSF is and why it is the hottest technology being used in the Java Enterprise applications.

JSF stands for "Java Server Faces" and this technology is a "server-side user interface component framework for Java technology-based web applications". The words do sound confusing.

So, in simple terms, JSF is a MVC based framework which brings forth a technology where the UI components are much more than being just normal HTML tags, as is the case with all other frameworks. Each component has its own class in the JSF API and thus every UI component is represented by a Server Side Object which stores the component state on the server. Then there is a “Renderer” for each component which generates the HTML for displaying the component. This renderer layer enables the use of JSF technology on different devices, without any change in the component API. If we want to use JSF with a mobile device, we can just change the renderer kit to one which generates WML instead of HTML. A UI Component class can combine with different renderers to generate different HTML components. So, a change in look-and-feel of a component can be affected by just adding a new renderer and more importantly without adding a new component or changing the API.

Also, the UI Components of JSF are listeners and events enabled, pretty much like the Java Swing Components. By storing the components as objects it becomes easier to associate events and listeners with them and thus JSF can also be seen as the introduction of Java Swing-style components into the web world.

The framework uses FacesServlet as the controller Servlet, so as is the principle of MVC architecture, every request first goes to the FacesServlet which then delegates it to the requisite page. This controller uses a configuration file faces-config.xml, where all the navigation and managed bean related declarations are done.

The UI layer mainly uses the JSPs but the introduction of Facelets has provided a new developer friendly alternative. The Facelets aim to make JSF a completely independent technology.
Facelets is a powerful templating system that allows you to define JSF views using HTML-style templates, reduces the amount of code necessary to integrate components into the view, and doesn't require a web container. While Java Server Faces and JSP are meant to be aligned, Facelets steps outside of the JSP specification and provides a highly per formant, JSF-centric view technology. Anyone who has created a JSP page will be able to do the same with Facelets and familiar XML-tag use. The difference is under the hood where all the burden of the JSP vendor API is removed to greatly enhance JSF as a platform and provide easy plug-and-go development without requiring JSP tag development.

Managed Beans are at the heart of JSF Technology. They are nothing but the Java Bean classes whose objects are instantiated by the framework as and when required and placed in the specified scope i.e. request, session or application. These are the classes which provided dynamism to a JSP. The request parameters may be set as the attributes of these objects and may be accessed on any other page. These classes may also contain methods containing the validators and listeners for the various components on the page.

Also, there are the features of Validators and Converters that can be used with UI Input type components. Converters are used to convert the data into other data types e.g. Date Converters and Number Converters. Validators as the name suggests are used to give the front end validation logic for the components e.g. email validation, string length validation etc.

The frame is fully extensible in the sense that a developer has full freedom to introduce a new component, renderer, converter or validators to suit his needs. Once written they just have to be registered in faces-config and then they are ready to use.

No comments: