Jsf How to create a Naming Container
Asked Answered
B

2

7

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.

Benford answered 10/8, 2010 at 13:21 Comment(0)
K
12

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" />
Kataway answered 10/8, 2010 at 20:31 Comment(2)
That solves my problem perfectly. Thanks to you Naganalf and BalusC!Benford
sorry for down voting, but f:subview does not seem to be intended to fix the problem described in the posting OP is mentioning. This does something and something might work.Infamous
P
4

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.

Pisano answered 10/8, 2010 at 13:34 Comment(4)
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.Benford
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?Pisano
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.Kataway
there is a fairly easy example at https://mcmap.net/q/18130/-duplicate-id-jsf/… once you got a tag with rendered="#{baluscRox == true}" and once rendered="#{baluscRox == false}" it accesses the same object etcInfamous

© 2022 - 2024 — McMap. All rights reserved.