Since JSF 2.0 when using Facelets view definitions the component tree is built twice on postback requests, once in RESTORE_VIEW phase and once in RENDER_RESPONSE phase. The rebuild in the render phase is necessary in case the page author used conditional includes. On big views several hundred component build the component tree – this may take some 100ms per buildView() execution – saving that once will improve the performance.With conditional includes or templates it is possible the include or template changes after RESTORE_VIEW and before RENDER_RESPONSE phase. Conditional include or conditional template is a result of using:

<c:if test=”…”> around a <ui:include src=”…”>

EL expression in the <ui:include src=”#{expression.to.source}”>

EL expression in <ui:composition template=”#{expression.to.template}”>

This gives us an optimization potential if we know we do not use such conditional includes or templates. In that case we can can use a ViewDeclarationLanguageWrapper and override the method <code>public void buildView(FacesContext context, UIViewRoot root)</code> to suppress conditionally in case of a postback request where buildView() was already executed in RESTORE_VIEW phase.

I created a little project on github – https://github.com/dueni/faces-ext/tree/master/suppress-rebuildview, it contains just a few classes and configuration to get this working.

SuppressRebuildViewViewDeclarationLanguageWrapper – the wrapper to suppress buildView(…) call. SuppressRebuildViewViewDeclarationLanguageFactory – the factory to wrap ViewDeclarationLanguage META-INF/faces-config.xml to configure the view-declaration-language-factory SuppressRebuildViewPhaseListener – mark views that may suppress the second buildView() execution using <f:phaseListener type=”com.csg.jsf.view.SuppressRebuildViewPhaseListener” /> tag on the page or a template used by the page.

Remains one thing to mention – this tweak will work with JSF 2.0 or later. In case you want to use this you might as well get the project from github and build the jar using maven.

Any comments or suggestions on this? Did this help you?