0

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?

Gaurav Jeswani
  • 4,410
  • 6
  • 26
  • 47

2 Answers2

3

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.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
0

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 the FacesContext instance for the current request. If it already contains a UIViewRoot:

Set the locale on this UIViewRoot to the value returned by the getRequestLocale() method on the ExternalContext for this request. Take no further action during this phase, and return. The presence of a UIViewRoot 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:

Community
  • 1
  • 1
kolossus
  • 20,559
  • 3
  • 52
  • 104
  • Restore view phase will indeed always create the view, but the view is not always created during restore view phase. – BalusC Jul 29 '15 at 06:43