{
throw new FacesException("FacesContext is null");
}
// init the View
Application application = facesContext.getApplication();
ViewHandler viewHandler = application.getViewHandler();
// Call initView() on the ViewHandler. This will set the character encoding properly for this request.
viewHandler.initView(facesContext);
UIViewRoot viewRoot = facesContext.getViewRoot();
RestoreViewSupport restoreViewSupport = getRestoreViewSupport();
// Examine the FacesContext instance for the current request. If it already contains a UIViewRoot
if (viewRoot != null)
{
if (log.isLoggable(Level.FINEST))
log.finest("View already exists in the FacesContext");
// Set the locale on this UIViewRoot to the value returned by the getRequestLocale() method on the
// ExternalContext for this request
viewRoot.setLocale(facesContext.getExternalContext().getRequestLocale());
restoreViewSupport.processComponentBinding(facesContext, viewRoot);
return false;
}
String viewId = restoreViewSupport.calculateViewId(facesContext);
// Determine if the current request is an attempt by the
// servlet container to display an error page.
// If the request is an error page request, the servlet container
// is required to set the request parameter "javax.servlet.error.message".
final boolean errorPageRequest = facesContext.getExternalContext().getRequestMap()
.get("javax.servlet.error.message") != null;
// Determine if this request is a postback or an initial request.
// But if it is an error page request, do not treat it as a postback (since 2.0)
if (!errorPageRequest && restoreViewSupport.isPostback(facesContext))
{ // If the request is a postback
if (log.isLoggable(Level.FINEST))
log.finest("Request is a postback");
try
{
facesContext.setProcessingEvents(false);
// call ViewHandler.restoreView(), passing the FacesContext instance for the current request and the
// view identifier, and returning a UIViewRoot for the restored view.
viewRoot = viewHandler.restoreView(facesContext, viewId);
if (viewRoot == null)
{
// If the return from ViewHandler.restoreView() is null, throw a ViewExpiredException with an
// appropriate error message.
throw new ViewExpiredException("No saved view state could be found for the view identifier: " + viewId,
viewId);
}
// Restore binding
// This code was already called on UIViewRoot.processRestoreState, or if a StateManagementStrategy
// is used, it is called from there.
//restoreViewSupport.processComponentBinding(facesContext, viewRoot);
// Store the restored UIViewRoot in the FacesContext.
facesContext.setViewRoot(viewRoot);
}
finally
{
facesContext.setProcessingEvents(true);
}
}
else
{ // If the request is a non-postback
if (log.isLoggable(Level.FINEST))
log.finest("Request is not a postback. New UIViewRoot will be created");
//viewHandler.deriveViewId(facesContext, viewId)
ViewDeclarationLanguage vdl = viewHandler.getViewDeclarationLanguage(facesContext,
restoreViewSupport.deriveViewId(facesContext, viewId));
if (vdl != null)
{
ViewMetadata metadata = vdl.getViewMetadata(facesContext, viewId);
Collection<UIViewParameter> viewParameters = null;
if (metadata != null)
{
viewRoot = metadata.createMetadataView(facesContext);
if (viewRoot != null)
{
viewParameters = metadata.getViewParameters(viewRoot);
}
}
// If viewParameters is not an empty collection DO NOT call renderResponse
if ( !(viewParameters != null && !viewParameters.isEmpty()) )
{
// Call renderResponse() on the FacesContext.
facesContext.renderResponse();
}
}
else
{
// Call renderResponse
facesContext.renderResponse();
}
// viewRoot can be null here, if ...
// - we don't have a ViewDeclarationLanguage (e.g. when using facelets-1.x)
// - there is no view metadata or metadata.createMetadataView() returned null
if (viewRoot == null)
{
// call ViewHandler.createView(), passing the FacesContext instance for the current request and
// the view identifier
viewRoot = viewHandler.createView(facesContext, viewId);
}
// Subscribe the newly created UIViewRoot instance to the AfterAddToParent event, passing the
// UIViewRoot instance itself as the listener.
// -= Leonardo Uribe =- This line it is not necessary because it was
// removed from jsf 2.0 section 2.2.1 when pass from EDR2 to Public Review
// viewRoot.subscribeToEvent(PostAddToViewEvent.class, viewRoot);
// Store the new UIViewRoot instance in the FacesContext.
facesContext.setViewRoot(viewRoot);
// Publish an AfterAddToParent event with the created UIViewRoot as the event source.
application.publishEvent(facesContext, PostAddToViewEvent.class, viewRoot);
}
// add the ErrorPageBean to the view map to fully support
// facelet error pages, if we are in ProjectStage Development
// and currently generating an error page