×
☰ See All Chapters

JSF FacesContext

  • FacesContext is an abstract class in javax.faces.context package. 

  • It is the gateway for JSF technology. 

  • When you send a request for a Facelets for the first time then FacesServlet will create an object of FacesContext at the beginning of the request processing. For each request new FacesContext object will be created. It is passed to each phase of the request processing lifecycle, and potentially modified by each phase of the request processing lifecycle. 

  • FacesContext will create a component tree for the Facelets and the component tree will be displayed to the client. 

  • All the information related to the request processing will be stored in the FacesContext object. 

  • Each JSF application must store information about the request it is processing. The FacesContext holds all contextual information necessary for processing a request and generating a response. More specifically, it manages a queue for messages, the current component tree, application configuration objects, and the life-cycle flow control methods. It also provides generic access to the external context in which the JSF application is running (which in most cases will be the servlet container environment or context).  

  • The FacesContext is basically a wrapper around the servlet context (ServletContext, Request, Response, Session.., and are made available through what is called the ExternalContext). Additionally the FacesContext provides per request state and information specific to JSF. So, for each request there is one and only one FacesContext available (as per its static nature), and can be accessed with: FacesContext.getCurrentInstance(); 

  • FecesServlet uses the FacesContext object to store the following request related information.  

    1. ViewRoot object 

    2. Error message related to conversions and validations. 

    3. Renderer kit information. 

    4. Response complete status. 

    5. ExternalContext object. 

JSF ExternalContext

You will sometimes wish to interact with the external environment in which our JSF implementation runs. This environment is usually a servlet context (for Web applications), but it may also be a Portlet or other container. The FacesContext provides generic access to the external environment via the getExternalContext() method. It returns an ExternalContext object that provides access to the external environment via a standard interface. Object of ExternalContext will be used at event handling. ExternalContext object has methods to obtain request, response, cookie objects etc….

 


All Chapters
Author