Do I have to pass ui:param in template if I am using ui:composite and ui:insert, ui:include?

813 Views Asked by At

I have one page main.xhtml, which contains ui:composition. This composition uses template1.xhtml file. template1.xhtml file contains ui:insert, ui:include tag which are pointing to template2.xhtml file. Now, I want to use parameters that are passed from ui:composition inside template2.xhtml.

main.xhtml

<ui:composition template="template1.xhtml">
    <ui:param name="a1" value="val1"/>
</ui:composition>

template1.xhtml

<ui:insert name="header">
    <ui:include src="template2.xhtml">
</ui:insert>

template2.xhtml

<h:panelGroup rendered="#{not empty a1}" layout="block">
    <h4>#{a1}</h4>
</h:panelGroup>

My question is can I directly access parameter named a1 or I had to again pass ui:param in ui:include?

1

There are 1 best solutions below

3
BalusC On BEST ANSWER

Depends on Facelets version and JSF impl/version.

In original Facelets 1.x, any <ui:param> in parent composition is inherited into all <ui:include> children.

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

In Facelets 2.x as integrated in JSF 2.x, any <ui:param> of a parent composition which ought to be reused in an <ui:include> (or <ui:decorate>) must be explicitly passed through.

<ui:include src="template2.xhtml">
    <ui:param name="a1" value="#{a1}" />
</ui:include>

At least, that's the case when you're using Mojarra 2.x. In MyFaces 2.0 / 2.1, however, it was not necessary as the original Facelets 1.x behavior unintentionally slipped into MyFaces 2.0 / 2.1.

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

They have fixed it in MyFaces 2.2, so you need to explicitly pass them through.

<ui:include src="template2.xhtml">
    <ui:param name="a1" value="#{a1}" />
</ui:include>

In case you're using MyFaces 2.2 or newer, and want to retain the original Facelets 1.x behavior, then you can add the following context param to web.xml:

<context-param>
    <param-name>org.apache.myfaces.STRICT_JSF_2_FACELETS_COMPATIBILITY</param-name>
    <param-value>true</param-value>
</context-param>
<ui:include src="template2.xhtml" />

No such context param exist for Mojarra, so this should not be relied upon in case you want to have your code to work the same across all JSF implementations.