views:

201

answers:

2

I have a problem with duplicated ids in my JSF app. I've read in this post that one of the possible solutions is to use Naming Container. Can you give me some example how to use the Naming Container to avoid duplicated ids problem? I use Facelets.

+1  A: 

I suggest to take a step back and investigate why the duplicate ID problem occurs. Once you nailed the root cause down, then just fix it the "usual" way rather than creating your own UINamingContainer component.

There are several possible causes for duplicate ID errors which should help you further nailing it down:

  • The same ID is used on different UIComponents inside the same UINamingContainer component.
  • Physically different components are bound to the same UIComponent property of the same bean.
  • JSP only: the f:subview is been declared in the parent page instead of the include page.
  • The same include page is included multiple times inside the same UINamingContainer component.
  • A component is been dynamically built (e.g. new UIComponent()) without having an ID assigned.

Here, UINamingContainer is under each the <h:form>, <h:dataTable> and <f:subview>.

If the above suggestions doesn't help, then update your question to include the smallest possible code snippet (thus, without all irrelevant code/clutter like unrelated components, libraries, HTML/CSS/JS/etc) which reproduces the exact same problem by just copy'n'paste'n'running it without any changes.

BalusC
This is just like the next to the last scenario. I have a facelets custom component which I include several times inside the same page. My facelets component contains a4j:outputPanel with the problematic id. I need this id so that I can reRender the a4j:outputPanel. Wrapping my component contents inside h:form doesn't seem to me like a good option. I'd rather put it inside some facelets-provided UINamingContainer but I reckon there's no such thing.
mgamer
JSF 1.x or 2.x? Where are you invoking the rerender, inside or outside the custom component? What exactly do you mean with "custom component", just `ui:composition`?
BalusC
I just ran into the same issue while creating my first facelets components. I'll share what worked for me in another answer (when I get back from lunch). You might want to update your question to be more specific.
Naganalf
+2  A: 

This is what worked for me using JSF1.2 and facelets:

I discovered that neither <ui:composition> nor <ui:component> is actually a naming container, so using the same component more than once in the same form would fail with a duplicate ID exception. This seems like a bad design, as the whole point of components is re-usability. To get around this problem I include a <f:subview> within each component and set the id on it as a parameter of my component tag:

myComponent.xhtml:

<ui:component>      
    <f:subview id="#{id}">
        ....component code
    </f:subview>
</ui:component>

and the using it on other pages is simple (after setting up taglib.xml and web.xml correctly):

<myTagLib:myComponent id="myCompA" />
Naganalf
That solves my problem perfectly. Thanks to you Naganalf and BalusC!
mgamer