Saturday, July 21, 2012

JSF: The JavaServer Faces Technology

JavaServer Faces Technology

JavaServer Faces technology is a server-side component framework for building Java technology-based web applications.

JavaServer Faces technology consists of the following:
--------------------------------------------------------------------
  1. An API for representing components and managing their state; handling events, server-side validation, and data conversion; defining page navigation; supporting internationalization and accessibility; and providing extensibility for all these features.

  2. Tag libraries for adding components to web pages and for connecting components to server-side objects JavaServer Faces technology provides a well-defined programming model and various tag libraries. These features significantly ease the burden of building and maintaining web applications with server-side user interfaces (UIs). With minimal effort, you can complete the following tasks.
    • Create a web page.
    • Drop components onto a web page by adding component tags.

    • Bind components on a page to server-side data.

    • Wire component-generated events to server-side application code.

    • Save and restore application state beyond the life of server requests.

    • Reuse and extend components through customization.

What Is a JavaServer Faces Application?
--------------------------------------------------

The functionality provided by a JavaServer Faces application is similar to that of any other Java web application. A typical JavaServer Faces application includes the following parts:
  • A set of web pages in which components are laid out.

  • A set of tags to add components to the web page.

  • A set of managed beans, which are lightweight container-managed objects (POJOs) with minimal requirements. They support a small set of basic services, such as resource injection, lifecycle callbacks and interceptors.

  • A web deployment descriptor (web.xml file).

  • Optionally, one or more application configuration resource files, such as a faces-config.xml file, which can be used to define page navigation rules and configure beans and other custom objects, such as custom components.

  • Optionally, a set of custom objects, which can include custom components, validators, converters, or listeners, created by the application developer.

  • A set of custom tags for representing custom objects on the page.
Figure 1 shows the interaction between client and server in a typical JavaServer Faces application. In response to a client request, a web page is rendered by the web container that implements JavaServer Faces technology.


Figure 1: Responding to a Client Request for a JavaServer Faces Page.

The web page, index.xhtml, is built using JavaServer Faces component tags. Component tags are used to add components to the view (represented by helloUI in the diagram), which is the server-side representation of the page. In addition to components, the web page can also reference objects, such as the following:
  • Any event listeners, validators, and converters that are registered on the components.

  • The JavaBeans components that capture the data and process the application-specific functionality of the components.

On request from the client, the view is rendered as a response. Rendering is the process whereby, based on the server-side view, the web container generates output, such as HTML or XHTML, that can be read by the client, such as a browser.

JavaServer FacesTechnology Benefits
----------------------------------------------

One of the greatest advantages of JavaServer Faces technology is that it offers a clean separation between behavior and presentation for web applications. A JavaServer Faces application can map HTTP requests to component-specific event handling and manage components as stateful objects on the server. JavaServer Faces technology allows you to build web applications that implement the finer-grained separation of behavior and presentation that is traditionally offered by client-side UI architectures.

The separation of logic from presentation also allows each member of a web application development team to focus on a single piece of the development process and provides a simple programming model to link the pieces. For example, page authors with no programming expertise can use JavaServer Faces technology tags in a web page to link to server-side objects without writing any scripts.

Another important goal of JavaServer Faces technology is to leverage familiar component and web-tier concepts without limiting you to a particular scripting technology or markup language. JavaServer Faces technology APIs are layered directly on top of the Servlet API, as shown in Figure 2.



Figure 2: Java Web Application Technologies.

This layering of APIs enables several important application use cases, such as using different presentation technologies, creating your own custom components directly from the component classes, and generating output for various client devices.

Facelets technology, available as part of JavaServer Faces 2.0, is now the preferred presentation technology for building JavaServer Faces technology-based web applications.

Facelets technology offers several advantages:
  • Code can be reused and extended for components through the templating and composite component features.

  • When you use the JavaServer Faces Annotations feature, you can automatically register the managed bean as a resource available for JavaServer Faces applications. In addition, implicit navigation rules allow developers to quickly configure page navigation. These features reduce the manual configuration process for applications.

  • Most important, JavaServer Faces technology provides a rich architecture for managing component state, processing component data, validating user input, and handling events.

For more information on JavaServer Faces technology, see:
---------------------------------------------------------------------------

3 comments :

  1. Hi mohamed,
    WoooW very good explanation topic :)

    actually i need to know how to get servletPath of the forward page in filter class
    i tried request.getAttribute("javax.servlet.forward.request_uri") and i get null, i tried to get it from externalContext.getRequestMap().get(RequestDispatcher.FORWARD_REQUEST_URI) and of course i got nullPointer because it's not a jsf context

    Thanks

    ReplyDelete
    Replies
    1. In Servlet filter, the server pass the generic ServletRequest, you should cast it to HttpServeletrequest first to get the http properties, then you can get all the URIs paths required for example:

      public void doFilter(ServletRequest request, ServletResponse response,
      FilterChain chain) throws IOException,
      ServletException {

      //parse request to Http Request.
      HttpServletRequest httpRequest = (HttpServletRequest)request;
      httpRequest.getServletPath();// this method is what you need.
      }

      see also: http://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html for more info.

      Delete
    2. Thanks for your replay,

      I already cast it, but i need the forward page not the current url

      I'm using JSF framework and when you return the name of page without "?faces-redirect=true" from method like "main" for example the content of page will changed but the url will be the same like index.xhtml for example, so when getServletPath i will get /index.xhtml not /main.xhtml

      I need to know the forward page url

      Thanks

      Delete