Class FacesContext
- Direct Known Subclasses:
FacesContextWrapper
FacesContext contains all of the per-request state information related to the processing of a single Jakarta Server 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 Jakarta Servlet container executing this web application
utilizes for the processing of this request.
A FacesContext can be injected into a request scoped bean using @Inject FacesContext facesContext;
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract voidaddMessage(String clientId, FacesMessage message) Append aFacesMessageto the set of messages associated with the specified client identifier, ifclientIdis notnull.abstract ApplicationReturn theApplicationinstance associated with this web application.Return a mutableMaprepresenting the attributes associated wth thisFacesContextinstance.Return anIteratorover the client identifiers for which at least oneFacesMessagehas been queued.static FacesContextReturn theFacesContextinstance for the request that is being processed by the current thread.Return the value last set on thisFacesContextinstance whensetCurrentPhaseId(jakarta.faces.event.PhaseId)was called.Return theELContextinstance for thisFacesContextinstance.Return theExceptionHandlerfor this request.abstract ExternalContextabstract FacesMessage.SeverityReturn the maximum severity level recorded on anyFacesMessages that has been queued, whether or not they are associated with any specificUIComponent.LikegetMessages(), but returns aList<FacesMessage>, enabling use from Jakarta Expression Language expressions.getMessageList(String clientId) LikegetMessages(java.lang.String), but returns aList<FacesMessage>of messages for the component with client id matching argumentclientId.abstract Iterator<FacesMessage>Return anIteratorover theFacesMessages that have been queued, whether or not they are associated with any specific client identifier.abstract Iterator<FacesMessage>getMessages(String clientId) Return anIteratorover theFacesMessages that have been queued that are associated with the specified client identifier (ifclientIdis notnull), or over theFacesMessages that have been queued that are not associated with any specific client identifier (ifclientIdisnull).charReturn the result of callingUINamingContainer.getSeparatorChar(jakarta.faces.context.FacesContext), passingthisas the argument.Return thePartialViewContextfor this request.abstract RenderKitReturn theRenderKitinstance for the render kit identifier specified on ourUIViewRoot, if there is one.abstract booleanReturntrueif therenderResponse()method has been called for the current request.Return the list of resource library contracts that have been calculated to be appropriate for use with this view, or an empty list if there are no such resource library contracts.abstract booleanReturntrueif theresponseComplete()method has been called for the current request.abstract ResponseStreamReturn theResponseStreamto which components should direct their binary output.abstract ResponseWriterReturn theResponseWriterto which components should direct their character-based output.abstract UIViewRootReturn the root component that is associated with the this request.booleanThis utility method simply returns the result ofResponseStateManager.isPostback(FacesContext).booleanReturns a flag indicating whether or not the runtime should publish events when asked to do so.booleanisProjectStage(ProjectStage stage) Returntrueif the currentProjectStageas returned by theApplicationinstance is equal tostage, otherwise returnfalsebooleanReturn a flag indicating if the resources associated with thisFacesContextinstance have been released.booleanReturntrueif thevalidationFailed()method has been called for the current request.abstract voidrelease()Release any resources associated with thisFacesContextinstance.abstract voidSignal the Jakarta Server Faces implementation that, as soon as the current phase of the request processing lifecycle has been completed, control should be passed to the Render Response phase, bypassing any phases that have not been executed yet.abstract voidSignal the Jakarta Server Faces implementation that the HTTP response for this request has already been generated (such as an HTTP redirect), and that the request processing lifecycle should be terminated as soon as the current phase is completed.protected static voidsetCurrentInstance(FacesContext context) Set theFacesContextinstance for the request that is being processed by the current thread.voidsetCurrentPhaseId(PhaseId currentPhaseId) The implementation must call this method at the earliest possble point in time after entering into a new phase in the request processing lifecycle.voidsetExceptionHandler(ExceptionHandler exceptionHandler) Set theExceptionHandlerfor this request.voidsetProcessingEvents(boolean processingEvents) Allows control of wheter or not the runtime will publish events whenApplication.publishEvent(FacesContext, Class, Object)orApplication.publishEvent(FacesContext, Class, Class, Object)is called.voidsetResourceLibraryContracts(List<String> contracts) Set the resource library contracts calculated as valid to use with this view.abstract voidsetResponseStream(ResponseStream responseStream) Set theResponseStreamto which components should direct their binary output.abstract voidsetResponseWriter(ResponseWriter responseWriter) Set theResponseWriterto which components should direct their character-based output.abstract voidsetViewRoot(UIViewRoot root) Set the root component that is associated with this request.voidSets a flag which indicates that a conversion or validation error occurred while processing the inputs.
-
Constructor Details
-
FacesContext
public FacesContext()
-
-
Method Details
-
getApplication
Return the
Applicationinstance associated with this web application.It is valid to call this method during application startup or shutdown. If called during application startup or shutdown, returns the correct current
Applicationinstance.- Returns:
- the
Applicationinstance associated with this web application. - Throws:
IllegalStateException- if this method is called after this instance has been released
-
getAttributes
Return a mutable
Maprepresenting the attributes associated wth thisFacesContextinstance. ThisMapis useful to store attributes that you want to go out of scope when the Faces lifecycle for the current request ends, which is not always the same as the request ending, especially in the case of Jakarta Servlet filters that are invoked after the Faces lifecycle for this request completes. Accessing thisMapdoes not cause any events to fire, as is the case with the other maps: for request, session, and application scope. Whenrelease()is invoked, the attributes must be cleared.The
Mapreturned by this method is not associated with the request. If you would like to get or set request attributes, seeExternalContext.getRequestMap().The default implementation throws
UnsupportedOperationExceptionand is provided for the sole purpose of not breaking existing applications that extend this class.- Returns:
- mutable
Maprepresenting the attributes associated wth thisFacesContextinstance. - Throws:
IllegalStateException- if this method is called after this instance has been released- Since:
- 2.0
-
getPartialViewContext
Return the
PartialViewContextfor this request. ThePartialViewContextis used to control the processing of specified components during the execute portion of the request processing lifecycle (known as partial processing) and the rendering of specified components (known as partial rendering). This method must return a newPartialViewContextif one does not already exist.- Returns:
- the instance of
PartialViewContextfor this request. - Throws:
IllegalStateException- if this method is called after this instance has been released- Since:
- 2.0
-
getClientIdsWithMessages
Return an
Iteratorover the client identifiers for which at least oneFacesMessagehas been queued. If there are no such client identifiers, an emptyIteratoris returned. If any messages have been queued that were not associated with any specific client identifier, anullvalue will be included in the iterated values. The elements in theIteratormust be returned in the order in which they were added withaddMessage(java.lang.String, jakarta.faces.application.FacesMessage).- Returns:
- the
Iteratorover the client identifiers for which at least oneFacesMessagehas been queued. - Throws:
IllegalStateException- if this method is called after this instance has been released
-
getELContext
Return the
ELContextinstance for thisFacesContextinstance. ThisELContextinstance has the same lifetime and scope as theFacesContextinstance with which it is associated, and may be created lazily the first time this method is called for a givenFacesContextinstance. Upon creation of the ELContext instance, the implementation must take the following action:-
Call the
ELContext.putContext(java.lang.Class<?>, java.lang.Object)method on the instance, passing inFacesContext.classand thethisreference for theFacesContextinstance itself. -
If the
Collectionreturned byApplication.getELContextListeners()is non-empty, create an instance ofELContextEventand pass it to eachELContextListenerinstance in theCollectionby calling theELContextListener.contextCreated(jakarta.el.ELContextEvent)method.
- Returns:
- instance of
ELContext. - Throws:
IllegalStateException- if this method is called after this instance has been released- Since:
- 1.2
-
-
getExceptionHandler
Return the
ExceptionHandlerfor this request.- Returns:
- instance of
ExceptionHandler.
-
setExceptionHandler
Set the
ExceptionHandlerfor this request.- Parameters:
exceptionHandler- theExceptionHandlerfor this request.
-
getExternalContext
Return the
ExternalContextinstance for thisFacesContextinstance.It is valid to call this method during application startup or shutdown. If called during application startup or shutdown, this method returns an
ExternalContextinstance with the special behaviors indicated in the javadoc for that class. Methods document as being valid to call during application startup or shutdown must be supported.- Returns:
- instance of
ExternalContext - Throws:
IllegalStateException- if this method is called after this instance has been released
-
getMaximumSeverity
Return the maximum severity level recorded on any
FacesMessages that has been queued, whether or not they are associated with any specificUIComponent. If no such messages have been queued, returnnull.- Returns:
- the maximum severity level.
- Throws:
IllegalStateException- if this method is called after this instance has been released
-
getMessages
Return an
Iteratorover theFacesMessages that have been queued, whether or not they are associated with any specific client identifier. If no such messages have been queued, return an emptyIterator. The elements of theIteratormust be returned in the order in which they were added with calls toaddMessage(java.lang.String, jakarta.faces.application.FacesMessage).- Returns:
Iteratorover theFacesMessages that have been queued.- Throws:
IllegalStateException- if this method is called after this instance has been released
-
getMessageList
Like
getMessages(), but returns aList<FacesMessage>, enabling use from Jakarta Expression Language expressions.The default implementation throws
UnsupportedOperationExceptionand is provided for the sole purpose of not breaking existing applications that extend this class.- Returns:
- an immutable
Listwhich is effectively a snapshot of the messages present at the time of invocation. - Throws:
IllegalStateException- if this method is called after this instance has been released- Since:
- 2.0
-
getMessageList
Like
getMessages(java.lang.String), but returns aList<FacesMessage>of messages for the component with client id matching argumentclientId.The default implementation throws
UnsupportedOperationExceptionand is provided for the sole purpose of not breaking existing applications that extend this class.- Parameters:
clientId- the client id of a component.- Returns:
- an immutable
Listwhich is effectively a snapshot of the messages present at the time of invocation. - Throws:
IllegalStateException- if this method is called after this instance has been released- Since:
- 2.0
-
getMessages
Return an
Iteratorover theFacesMessages that have been queued that are associated with the specified client identifier (ifclientIdis notnull), or over theFacesMessages that have been queued that are not associated with any specific client identifier (ifclientIdisnull). If no such messages have been queued, return an emptyIterator. The elements of theIteratormust be returned in the order in which they were added with calls toaddMessage(java.lang.String, jakarta.faces.application.FacesMessage).- Parameters:
clientId- The client identifier for which messages are requested, ornullfor messages not associated with any client identifier- Returns:
Iteratorover theFacesMessages.- Throws:
IllegalStateException- if this method is called after this instance has been released
-
getNamingContainerSeparatorChar
public char getNamingContainerSeparatorChar()Return the result of calling
UINamingContainer.getSeparatorChar(jakarta.faces.context.FacesContext), passingthisas the argument. Note that this enables accessing the value of this property from the Jakarta Expression Language expression#{facesContext.namingContainerSeparatorChar}.- Returns:
- the separator char.
-
getRenderKit
Return the
RenderKitinstance for the render kit identifier specified on ourUIViewRoot, if there is one. If there is no currentUIViewRoot, if theUIViewRootdoes not have a specifiedrenderKitId, or if there is noRenderKitfor the specified identifier, returnnullinstead.- Returns:
- instance of
RenderKitassociated with theUIViewRoot. - Throws:
IllegalStateException- if this method is called after this instance has been released
-
getRenderResponse
public abstract boolean getRenderResponse()Return
trueif therenderResponse()method has been called for the current request.- Returns:
- flag indicating whether the
renderResponse()has been called. - Throws:
IllegalStateException- if this method is called after this instance has been released
-
getResponseComplete
public abstract boolean getResponseComplete()Return
trueif theresponseComplete()method has been called for the current request.- Returns:
- the boolean indicating whether
responseComplete()method has been called. - Throws:
IllegalStateException- if this method is called after this instance has been released
-
getResourceLibraryContracts
Return the list of resource library contracts that have been calculated to be appropriate for use with this view, or an empty list if there are no such resource library contracts. The list returned by this method must be immutable. For backward compatibility with implementations of the specification prior to when this method was introduced, an implementation is provided that returns an empty list. Implementations compliant with the version in which this method was introduced must implement this method as specified.
- Returns:
- the list of resource library contracts.
- Throws:
IllegalStateException- if this method is called after this instance has been released- Since:
- 2.2
-
setResourceLibraryContracts
Set the resource library contracts calculated as valid to use with this view. The implementation must copy the contents of the incoming
Listinto an immutableListfor return fromgetResourceLibraryContracts(). If the argument isnullor empty, the action taken is the same as if the argument isnull: a subsequent call togetResourceLibraryContractsreturnsnull. This method may only be called during the processing ofViewDeclarationLanguage.createView(jakarta.faces.context.FacesContext, java.lang.String)and during the VDL tag handler for the tag corresponding to an instance ofUIViewRoot. For backward compatibility with implementations of the specification prior to when this method was introduced, an implementation is provided that takes no action. Implementations compliant with the version in which this method was introduced must implement this method as specified.- Parameters:
contracts- The new contracts to be returned, as an immutableList. from a subsequent call togetResourceLibraryContracts().- Throws:
IllegalStateException- if this method is called after this instance has been released- Since:
- 2.2
-
isValidationFailed
public boolean isValidationFailed()Return
trueif thevalidationFailed()method has been called for the current request.- Returns:
- boolean indicating if the
validationFailed()method has been called for the current request - Throws:
IllegalStateException- if this method is called after this instance has been released
-
getResponseStream
Return the
ResponseStreamto which components should direct their binary output. Within a given response, components can use either the ResponseStream or the ResponseWriter, but not both.- Returns:
ResponseStreaminstance.- Throws:
IllegalStateException- if this method is called after this instance has been released
-
setResponseStream
Set the
ResponseStreamto which components should direct their binary output.- Parameters:
responseStream- The new ResponseStream for this response- Throws:
NullPointerException- ifresponseStreamisnullIllegalStateException- if this method is called after this instance has been released
-
getResponseWriter
Return the
ResponseWriterto which components should direct their character-based output. Within a given response, components can use either the ResponseStream or the ResponseWriter, but not both.- Returns:
ResponseWriterinstance.- Throws:
IllegalStateException- if this method is called after this instance has been released
-
setResponseWriter
Set the
ResponseWriterto which components should direct their character-based output.- Parameters:
responseWriter- The new ResponseWriter for this response- Throws:
IllegalStateException- if this method is called after this instance has been releasedNullPointerException- ifresponseWriterisnull
-
getViewRoot
Return the root component that is associated with the this request.
It is valid to call this method during application startup or shutdown. If called during application startup or shutdown, this method returns a new
UIViewRootwith its locale set toLocale.getDefault().- Returns:
UIViewRootinstance.- Throws:
IllegalStateException- if this method is called after this instance has been released
-
setViewRoot
Set the root component that is associated with this request.
This method can be called by the application handler (or a class that the handler calls), during the Invoke Application phase of the request processing lifecycle and during the Restore View phase of the request processing lifecycle (especially when a new root component is created). In the present version of the specification, implementations are not required to enforce this restriction, though a future version of the specification may require enforcement.
If the current
UIViewRootis non-null, and callingequals()on the argumentroot, passing the currentUIViewRootreturnsfalse, theclearmethod must be called on theMapreturned fromUIViewRoot.getViewMap().- Parameters:
root- The new componentUIViewRootcomponent- Throws:
IllegalStateException- if this method is called after this instance has been releasedNullPointerException- ifrootisnull
-
addMessage
Append a
FacesMessageto the set of messages associated with the specified client identifier, ifclientIdis notnull. IfclientIdisnull, thisFacesMessageis assumed to not be associated with any specific component instance.- Parameters:
clientId- The client identifier with which this message is associated (if any)message- The message to be appended- Throws:
IllegalStateException- if this method is called after this instance has been releasedNullPointerException- ifmessageisnull
-
isReleased
public boolean isReleased()Return a flag indicating if the resources associated with this
FacesContextinstance have been released.- Returns:
trueif the resources have been released.- Since:
- 2.1
-
release
public abstract void release()Release any resources associated with this
FacesContextinstance. Faces implementations may choose to pool instances in the associatedFacesContextFactoryto avoid repeated object creation and garbage collection. Afterrelease()is called on aFacesContextinstance (until theFacesContextinstance has been recycled by the implementation for re-use), calling any other methods will cause anIllegalStateExceptionto be thrown.If a call was made to
getAttributes()during the processing for this request, the implementation must callclear()on theMapreturned fromgetAttributes(), and then de-allocate the data-structure behind thatMap.The implementation must call
setCurrentInstance(jakarta.faces.context.FacesContext)passingnullto remove the association between this thread and this deadFacesContextinstance.- Throws:
IllegalStateException- if this method is called after this instance has been released
-
renderResponse
public abstract void renderResponse()Signal the Jakarta Server Faces implementation that, as soon as the current phase of the request processing lifecycle has been completed, control should be passed to the Render Response phase, bypassing any phases that have not been executed yet.
- Throws:
IllegalStateException- if this method is called after this instance has been released
-
isPostback
public boolean isPostback()This utility method simply returns the result of
ResponseStateManager.isPostback(FacesContext).The default implementation throws
UnsupportedOperationExceptionand is provided for the sole purpose of not breaking existing applications that extend this class.- Returns:
- the boolean indicating whether this request is a post one.
- Throws:
IllegalStateException- if this method is called after this instance has been released- Since:
- 2.0
-
responseComplete
public abstract void responseComplete()Signal the Jakarta Server Faces implementation that the HTTP response for this request has already been generated (such as an HTTP redirect), and that the request processing lifecycle should be terminated as soon as the current phase is completed.
- Throws:
IllegalStateException- if this method is called after this instance has been released
-
validationFailed
public void validationFailed()Sets a flag which indicates that a conversion or validation error occurred while processing the inputs. Inputs consist of either page parameters or form bindings. This flag can be read using
isValidationFailed().- Throws:
IllegalStateException- if this method is called after this instance has been released
-
getCurrentPhaseId
Return the value last set on this
FacesContextinstance whensetCurrentPhaseId(jakarta.faces.event.PhaseId)was called.- Returns:
- the current phase id.
- Throws:
IllegalStateException- if this method is called after this instance has been released- Since:
- 2.0
-
setCurrentPhaseId
The implementation must call this method at the earliest possble point in time after entering into a new phase in the request processing lifecycle.
- Parameters:
currentPhaseId- ThePhaseIdfor the current phase.- Throws:
IllegalStateException- if this method is called after this instance has been released- Since:
- 2.0
-
setProcessingEvents
public void setProcessingEvents(boolean processingEvents) Allows control of wheter or not the runtime will publish events when
Application.publishEvent(FacesContext, Class, Object)orApplication.publishEvent(FacesContext, Class, Class, Object)is called.- Parameters:
processingEvents- flag indicating events should be processed or not
-
isProcessingEvents
public boolean isProcessingEvents()Returns a flag indicating whether or not the runtime should publish events when asked to do so.
- Returns:
trueif events should be published, otherwisefalse
-
isProjectStage
Return
trueif the currentProjectStageas returned by theApplicationinstance is equal tostage, otherwise returnfalse- Parameters:
stage- theProjectStageto check- Returns:
- boolean indicating whether the application has the same stage.
- Throws:
IllegalStateException- if this method is called after this instance has been releasedNullPointerException- ifstageisnull
-
getCurrentInstance
Return the
FacesContextinstance for the request that is being processed by the current thread. If called during application initialization or shutdown, any method documented as "valid to call this method during application startup or shutdown" must be supported during application startup or shutdown time. The result of calling a method during application startup or shutdown time that does not have this designation is undefined.- Returns:
- the instance of
FacesContext.
-
setCurrentInstance
Set the
FacesContextinstance for the request that is being processed by the current thread.- Parameters:
context- TheFacesContextinstance for the current thread, ornullif this thread no longer has aFacesContextinstance.
-