I have a <ui:composition>
that contains a few elements with explicit ids and some ajax events which reference these ids for partial processing/updating. I encapsulated this fragment of xhtml inside the composition simply so I could use it in a few different places without having to duplicate the code. However, when I use the composition (with <ui:include>
) more than once inside a page, I get duplicate id exceptions. It seems JSF is not wrapping each composition inside its own naming container (like <ui:component>
does).
Is there a simple way to wrap my composition inside its own naming container? Or do I have to use a composite component every time I want to reuse xhtml fragments inside a common naming container?
Depending on the purpose of the
<ui:include>
template, you've several options:Use
<f:subview>
. It creates anotherNamingContainer
context (like as<h:form>
,<h:dataTable>
, and friends all do):The components definied in
some.xhtml
will end up getting respectivelytop:
andbottom:
prefix in their ID.Turn it into a tagfile which requires an
id
attribute.And use that ID to prefix the ID of the components in the composition.
Turn it into a composite component. Composite components are inherently already a
NamingContainer
, so theirid
attribute is optional. Basically, replaceby
This way you can use it as follows:
The components definied in
<cc:implementation>
will end up getting respectivelytop:
andbottom:
prefix in their ID (note again, the composite component'sid
attribute is optional, JSF will otherwise autogenerate one).See also:
When you include more than once the same
ui:composition
the id are duplicated. The solution is to specifiy a particularui:param
within theui:include
.Assuming you're including mycomposition.xhtml you can do something similar:
Then in the mycomposition.xhtml you should declare the ids as follows (for instance a h:outputText):
Now you can reference the id in the rest of yout page as
#{idPrefix}_text
.