Search code examples
jsfjsf-2facelets

<ui:include> with dynamic src does not invoke setters on backingbean


I have a first.jsf in which I include second.xhtml in the following way:

<ui:include src="#{firstBean.srcForSecond}" />

This works fine and renders the contents of second.jsf. I included it using EL as the included content changes based on some conditions.

My second.xhtml contains a simple input text box:

<h:inputText id="firstname" value="#{secondBean.firstName}" />

When I load the page the value for firstname is rendered properly. However, when I change the value in the text box and submit, the setter on the backing bean is never invoked.

However if I change the ui:include in the following way, it works:

<ui:include src="second.xhtml" />

But, I require to use EL expression as it could be second.xhtml or third.xhtml based on some conditions.

Can anybody explain whats going on and how to fix it?


Solution

  • You need to make the #{firstBean} a @ViewScoped bean instead of a @RequestScoped one. This way you ensure that #{firstBean.srcForSecond} evaluates the same in the subsequent request. It's namely re-evaluated during apply request values phase of the form submit. If it evaluates differently, then the originally included components can't be located and nothing will be set/invoked.