问题
What is FacesContext
used for?
And where exactly is it implemented?
回答1:
Quote from chapter 6.1 of JSF 2.0 specification:
FacesContext
JSF defines the
javax.faces.context.FacesContext
abstract base class for representing all of the contextual information associated with processing an incoming request, and creating the corresponding response.A
FacesContext
instance is created by the JSF implementation, prior to beginning the request processing lifecycle, by a call to thegetFacesContext
method ofFacesContextFactory
, as described in Section 6.6 “FacesContextFactory”.When the request processing lifecycle has been completed, the JSF implementation will call the release method, which gives JSF implementations the opportunity to release any acquired resources, as well as to pool and recycle FacesContext instances rather than creating new ones for each request.
It is specified in class javax.faces.context.FacesContext and in Mojarra implemented in class com.sun.faces.context.FacesContextImpl and in MyFaces implemented in class org.apache.myfaces.context.servlet.FacesContextImpl.
See also:
- What is the difference between FacesContext and ExternalContext
回答2:
FacesContext contains all of the per-request state information related to the processing of a single JavaServer Faces request, and the rendering of the corresponding response. It is passed to, and potentially modified by, each phase of the request processing lifecycle.
A FacesContext instance is associated with a particular request at the beginning of request processing, by a call to the getFacesContext() method of the FacesContextFactory instance associated with the current web application. The instance remains active until its release() method is called, after which no further references to this instance are allowed. While a FacesContext instance is active, it must not be referenced from any thread other than the one upon which the servlet container executing this web application utilizes for the processing of this request.
reference:
http://docs.oracle.com/javaee/6/api/javax/faces/context/FacesContext.html
回答3:
A new instance of FacesContext gets created with every new request. The main task of FacesContext is to provide all the information regarding the particular request with which it is associated. For example : to add messages after certain validation or conversion failure, you can simply add the message to the FacesContext which ultimately displays the desired message on the screen. Also you can take a look and even stop the JSF lifecycle at any point by calling
FacesContext.responseComplete();
回答4:
The Facescontext is the anchor point for connecting to framework-and container- specific services. For example, you can obtain the underlying httprequest and context objects for the page request that's accessing your backing bean by going through the Facescontext. That includes the security context a web page request is running under.
The general philosophy behind JSF is that the user-provided components should be abstract enough not to depend on such details as to whether they're running in a traditional JSP environment or something more exotic, so if there's a Facescontext reference in the bean, it's a potential sign of bad design, especially since beans are expected to have things injected into them, not go out looking for stuff.
However, sometimes for one reason or another, it's convenient to reach out of the backing bean and obtain the outlying information, so it's there if you need it.
Reference: What is the use of Facescontext Context Object
来源:https://stackoverflow.com/questions/5865353/what-is-facescontext-used-for