In JSF a view root is created for every JSF page. When it is constructed?
Does it construct every time when view renders or depends on backing bean initialization?
In JSF a view root is created for every JSF page. When it is constructed?
Does it construct every time when view renders or depends on backing bean initialization?
In JSF a view root is created for every JSF page. When it is constructed?
Basically, when JSF lifecycle calls ViewHandler#createView()
, which will in turn call the ViewDeclarationLanguage#createView()
. That can happen during restore view phase. But that can also happen during render response phase when a navigation has taken place. Or when JSF needs to extract metadata from a given view. It can also happen anytime when custom code explicitly calls ViewHandler#createView()
. Nothing in the JSF specification restricts that to a specific moment.
Does it construct every time when view renders or depends on backing bean initialization?
It can't depend on bean initialization. Without the view, JSF wouldn't have any idea which beans to initialize simply because those beans are declared in the view itself.
When it is constructed?
It's constructed during the RESTORE_VIEW
phase of the lifecycle, i.e. when the page is first requested and it's represented as an instance of UIViewRoot
Does it construct every time when view renders or depends on backing bean initialization?
It's constructed just before a page is rendered. While you'll typically get a new UIViewRoot
for every page request, it's possible that you can reuse the same object from a previous rendering of the view, and the JSF specification supports that. From the spec:
[During the
RESTORE_VIEW
] Examine theFacesContext
instance for the current request. If it already contains aUIViewRoot
:Set the locale on this
UIViewRoot
to the value returned by thegetRequestLocale()
method on theExternalContext
for this request. Take no further action during this phase, and return. The presence of aUIViewRoot
already installed in the FacesContext before the Restore View Phase implementation indicates that the phase should assume the view has already been restored by other means.
There's really not much you can do from within the backing bean proper, to alter the UIViewRoot
, rather, a PhaseListener
or a ViewHandler
is better suited for such interference
Related: