What is the difference between FacesContext
and ExternalContext
? When can I use one or other? What has the one and what has the other?
The following sample is from the book JavaServer Faces 3rd edition:
<h:commandButton ... actionListener="#{rushmore.handleMouseClick}" />
Backing bean:
public void handleMouseClick(ActionEvent e) { FacesContext context = FacesContext.getCurrentInstance(); String clientId = e.getComponent().getClientId(context); Map<String, String> requestParams = context.getExternalContext().getRequestParameterMap(); // ... }
Why is request parameter in ExternalContext
? What is clientId
? Is it generated by JSF when the application is started?
The external context is the outside world. In this step we want to invite you to open up to that world with all your senses: for inspiration as well as for focus. In this step you try discover new ideas, trends, problems, frustrations, etc. by studying the outside world.
Class ExternalContext. This class allows the Faces API to be unaware of the nature of its containing application environment. In particular, this class allows JavaServer Faces based appications to run in either a Servlet or a Portlet environment.
getCurrentInstance. Return the FacesContext instance for the request that is being processed by the current thread.
Carefully look in their javadocs to see what methods they offer and what exactly those methods all do.
FacesContext
javadocExternalContext
javadocIf you look closer at those methods listed in the javadoc, you'll notice that the FacesContext
generally offers access to JSF-specific artifacts which are further in no way related to the "underlying" Servlet or Portlet API for which JSF is been designed to run on top of. E.g. creating converters, validators, components, EL expressions, etcetera and obtaining information about view root, supported locales, etcetera and adding phase listeners, system event listeners, etcetera. Everything which is specific to JSF API.
And, ExternalContext
generally offers access to Servlet or Portlet-specific artifacts which JSF is currently using "under the covers". E.g., when running on a Servlet container, the HTTP servlet request, HTTP servlet response, HTTP session and Servlet context and inherently also all of their artifacts. Click those links, you'll see that they in turn offer methods which are also been delegated by the ExternalContext
, such as getRequestParameterMap()
. See also the javadoc. Yes, also click that link, you'll see that it explicitly mentions the servlet request:
Servlet: This must be the set of parameters available via the
javax.servlet.ServletRequest
methodsgetParameter()
andgetParameterNames()
.
There's nothing which can be offered by the both contexts. So there would be absolutely no reason to prefer the one or the other. Just use the right one for the job you need to perform.
As to the client ID, it's indeed generated by JSF, but definitely not on server's startup. It's just generated for every single JSF component on a per-view basis. In case of input components like <h:inputText>
, which generates a HTML <input>
element, it also becomes the name
attribute like so
<input type="text" id="formId:inputId" name="formId:inputId" ... />
The formId:inputId
is exactly the JSF client ID. It becomes the request parameter name. The HTML representation of the command button has also a name
which ends up as request parameter name with the button's value as parameter value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With